@@ -328,3 +328,68 @@ def send_burst(self) -> List[int]:
328
328
self .input_queue_size = 0
329
329
330
330
return list (acks )
331
+
332
+
333
+ RECORDED_TRAFFIC = iter ([])
334
+ """An iterator returning (request, response) pairs.
335
+
336
+ The request is checked against data written to the dummy serial port, and if it matches
337
+ the response can be read back. Both request and response should be bytes-like.
338
+
339
+ Intended to be monkey-patched by the calling test module.
340
+ """
341
+
342
+
343
+ class MockHandler (Handler ):
344
+ """Mock implementation of :class:`Handler` for testing.
345
+
346
+ Parameters
347
+ ----------
348
+ See :meth:`connect. <PSL.packet_handler.Handler.connect>`.
349
+ """
350
+
351
+ VERSION = "PSLab vMOCK"
352
+
353
+ def __init__ (
354
+ self , port : str = None , baudrate : int = 1000000 , timeout : float = 1.0 ,
355
+ ):
356
+ self ._in_buffer = b""
357
+ super ().__init__ (port , baudrate , timeout )
358
+
359
+ def connect (
360
+ self , port : str = None , baudrate : int = 1000000 , timeout : float = 1.0 ,
361
+ ):
362
+ self .version = self .get_version ()
363
+
364
+ def disconnect ():
365
+ pass
366
+
367
+ def reconnect ():
368
+ pass
369
+
370
+ def get_version (self ):
371
+ return self .VERSION
372
+
373
+ def read (self , number_of_bytes : int ) -> bytes :
374
+ read_bytes = self ._in_buffer [:number_of_bytes ]
375
+ self ._in_buffer = self ._in_buffer [number_of_bytes :]
376
+ return read_bytes
377
+
378
+ def write (self , data : bytes ):
379
+ try :
380
+ tx , rx = next (RECORDED_TRAFFIC )
381
+ if tx == data :
382
+ self ._in_buffer += rx
383
+ except StopIteration :
384
+ # No more recorded traffic, calling method should timeout.
385
+ raise RuntimeError
386
+
387
+ def wait_for_data (self , timeout : float = 0.2 ) -> bool :
388
+ start_time = time .time ()
389
+
390
+ while time .time () - start_time < timeout :
391
+ if self ._in_buffer :
392
+ return True
393
+ time .sleep (0.02 )
394
+
395
+ return False
0 commit comments