Skip to content

Commit d9c64ca

Browse files
committed
speed up test_comm.py and test_nxscope.py
Signed-off-by: raiden00pl <raiden00@railab.me>
1 parent 4c0eb97 commit d9c64ca

5 files changed

Lines changed: 143 additions & 92 deletions

File tree

src/nxslib/comm.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,26 @@ class DCommChannelsData:
4444
class CommHandler:
4545
"""A class implementing the Nxslib communication glue logic."""
4646

47-
def __init__(self, intf: "ICommInterface", parse: ICommParse):
47+
def __init__(
48+
self,
49+
intf: "ICommInterface",
50+
parse: ICommParse,
51+
drop_timeout: float = 0.1,
52+
stream_data_timeout: float = 1.0,
53+
):
4854
"""Initialize communication glue logic.
4955
5056
:param intf: instance of a communication interface
5157
:param parse: instance of a parser class
58+
:param drop_timeout: timeout used in _drop_all_frames queue drains
59+
:param stream_data_timeout: timeout used in stream_data() frame wait
5260
"""
5361
# started flag
5462
self._started = False
5563

64+
self._drop_timeout = drop_timeout
65+
self._stream_data_timeout = stream_data_timeout
66+
5667
self._thrd = ThreadCommon(self._recv_thread, name="recv")
5768

5869
self._intf = intf
@@ -198,12 +209,12 @@ def _read_frame(self) -> DParseFrame | None:
198209
def _drop_all_frames(self) -> None:
199210
cntr = 4
200211
while cntr > 0:
201-
ret = self._get_frame(timeout=0.1)
212+
ret = self._get_frame(timeout=self._drop_timeout)
202213
if not ret: # pragma: no cover
203214
cntr -= 1
204215
cntr = 4
205216
while cntr > 0:
206-
ret = self._get_stream_frame(timeout=0.1)
217+
ret = self._get_stream_frame(timeout=self._drop_timeout)
207218
if not ret: # pragma: no cover
208219
cntr -= 1
209220

@@ -466,7 +477,7 @@ def stream_data(self) -> DParseStream | None:
466477
assert self.dev
467478

468479
# separate queue for stream frames
469-
frame = self._get_stream_frame()
480+
frame = self._get_stream_frame(timeout=self._stream_data_timeout)
470481
if not frame:
471482
return None
472483

src/nxslib/intf/dummy.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ def __init__(
292292
rxpadding: int = 16,
293293
stream_sleep: float = 0.001,
294294
stream_snum: int = 100,
295+
thread_timeout: float = 1.0,
295296
) -> None:
296297
"""Intitialize a dummy NxScope interface.
297298
@@ -301,6 +302,7 @@ def __init__(
301302
:param rxpadding: rxpadding - doesn't matter here
302303
:param stream_sleep: samples thread parameter
303304
:param stream_snum: samples thread parameter
305+
:param thread_timeout: timeout for blocking thread operations
304306
"""
305307
super().__init__()
306308
self._thrd_stream = ThreadCommon(
@@ -318,6 +320,7 @@ def __init__(
318320
self._dummydev_lock = Lock()
319321
self._stream_sleep = stream_sleep
320322
self._stream_snum = stream_snum
323+
self._thread_timeout = thread_timeout
321324
self._qwrite: queue.Queue[bytes] = queue.Queue()
322325
self._qread: queue.Queue[bytes] = queue.Queue()
323326

@@ -428,7 +431,7 @@ def _stream_data_get(self, snum: int) -> list[DParseStreamData]:
428431

429432
def _thread_stream(self) -> None:
430433
assert self._parse
431-
if self._stream_started.wait(timeout=1.0):
434+
if self._stream_started.wait(timeout=self._thread_timeout):
432435
samples = self._stream_data_get(self._stream_snum)
433436
frame = self._parse.frame_stream_encode(samples)
434437
if frame is not None: # pragma: no cover
@@ -442,7 +445,7 @@ def _thread_recv(self) -> None:
442445
try:
443446
# NOTE: timeout must be not zero otherwise we have
444447
# deadlock when thread stop is requested
445-
data = self._qwrite.get(block=True, timeout=1.0)
448+
data = self._qwrite.get(block=True, timeout=self._thread_timeout)
446449
except queue.Empty:
447450
pass
448451

@@ -498,7 +501,7 @@ def _read(self) -> bytes:
498501
"""Interface specific read method."""
499502
data = b""
500503
try:
501-
data = self._qread.get(block=True, timeout=1)
504+
data = self._qread.get(block=True, timeout=self._thread_timeout)
502505
except queue.Empty:
503506
pass
504507

src/nxslib/nxscope.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,25 @@ def __init__(
152152
intf: "ICommInterface",
153153
parse: "ICommParse",
154154
enable_bitrate_tracking: bool = False,
155+
drop_timeout: float = 0.1,
156+
stream_data_timeout: float = 1.0,
155157
) -> None:
156158
"""Initialize the Nxslib handler.
157159
158160
:param intf: Communication interface
159161
:param parse: Protocol parser
160162
:param enable_bitrate_tracking: Enable bitrate tracking
161163
(default: False)
164+
:param drop_timeout: timeout used in _drop_all_frames queue drains
165+
:param stream_data_timeout: timeout used in stream_data() frame wait
162166
"""
163167
self._connected: bool = False
164-
self._comm = CommHandler(intf, parse)
168+
self._comm = CommHandler(
169+
intf,
170+
parse,
171+
drop_timeout=drop_timeout,
172+
stream_data_timeout=stream_data_timeout,
173+
)
165174

166175
self._thrd = ThreadCommon(self._stream_thread, name="stream")
167176

tests/test_comm.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88

99

1010
def test_nxslib_init():
11-
i = DummyDev()
11+
i = DummyDev(thread_timeout=0.05)
1212
p = Parser()
1313

14-
comm = CommHandler(i, p)
14+
comm = CommHandler(i, p, drop_timeout=0.01, stream_data_timeout=0.05)
1515
assert isinstance(comm, CommHandler)
1616

1717

1818
@pytest.fixture
1919
def comm():
20-
i = DummyDev()
20+
i = DummyDev(thread_timeout=0.05)
2121
p = Parser()
22-
return CommHandler(i, p)
22+
return CommHandler(i, p, drop_timeout=0.01, stream_data_timeout=0.05)
2323

2424

2525
def test_nxslib_connect(comm):
@@ -326,9 +326,9 @@ def test_nxslib_stream_ch8(comm):
326326

327327

328328
def test_nxslib_nodiv(comm):
329-
i = DummyDev(flags=EDeviceFlags.ACK_SUPPORT.value)
329+
i = DummyDev(flags=EDeviceFlags.ACK_SUPPORT.value, thread_timeout=0.05)
330330
p = Parser()
331-
comm = CommHandler(i, p)
331+
comm = CommHandler(i, p, drop_timeout=0.01, stream_data_timeout=0.05)
332332

333333
# connect
334334
comm.connect()
@@ -348,9 +348,9 @@ def test_nxslib_nodiv(comm):
348348

349349

350350
def test_nxslib_noack(comm):
351-
i = DummyDev(flags=EDeviceFlags.DIVIDER_SUPPORT.value)
351+
i = DummyDev(flags=EDeviceFlags.DIVIDER_SUPPORT.value, thread_timeout=0.05)
352352
p = Parser()
353-
comm = CommHandler(i, p)
353+
comm = CommHandler(i, p, drop_timeout=0.01, stream_data_timeout=0.05)
354354

355355
# connect
356356
comm.connect()
@@ -371,9 +371,9 @@ def test_nxslib_noack(comm):
371371

372372
def test_comm_get_enabled_channels():
373373
"""Test get_enabled_channels method."""
374-
i = DummyDev()
374+
i = DummyDev(thread_timeout=0.05)
375375
p = Parser()
376-
comm = CommHandler(i, p)
376+
comm = CommHandler(i, p, drop_timeout=0.01, stream_data_timeout=0.05)
377377

378378
# connect
379379
comm.connect()

0 commit comments

Comments
 (0)