Skip to content

Commit 748ac58

Browse files
committed
fix: read all chunks in queue since we have interval in listener
1 parent 0b20d32 commit 748ac58

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

pytest-embedded/pytest_embedded/dut_factory.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,16 @@ def msg_queue_gn() -> MessageQueue:
5252
def _listen(q: MessageQueue, filepath: str, with_timestamp: bool = True, count: int = 1, total: int = 1) -> None:
5353
_added_prefix = False
5454
while True:
55-
msg = q.get()
56-
if not msg:
55+
msgs = q.get_all()
56+
if not msgs:
5757
continue
5858

59+
msg_b = b''.join(msgs)
5960
with open(filepath, 'ab') as fw:
60-
fw.write(msg)
61+
fw.write(msg_b)
6162
fw.flush()
6263

63-
_s = to_str(msg)
64+
_s = to_str(msg_b)
6465
if not _s:
6566
continue
6667

@@ -87,7 +88,7 @@ def _listen(q: MessageQueue, filepath: str, with_timestamp: bool = True, count:
8788

8889
_stdout.write(_s)
8990
_stdout.flush()
90-
time.sleep(0.1)
91+
time.sleep(0.05)
9192

9293

9394
def _listener_gn(msg_queue, _pexpect_logfile, with_timestamp, dut_index, dut_total) -> multiprocessing.Process:

pytest-embedded/pytest_embedded/log.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import textwrap
99
import uuid
1010
from multiprocessing import queues
11+
from queue import Empty
1112
from typing import AnyStr, List, Optional, Union
1213

1314
import pexpect.fdpexpect
@@ -26,6 +27,8 @@ class MessageQueue(queues.Queue):
2627
def __init__(self, *args, **kwargs):
2728
if 'ctx' not in kwargs:
2829
kwargs['ctx'] = _ctx
30+
31+
self.lock = _ctx.Lock()
2932
super().__init__(*args, **kwargs)
3033

3134
def put(self, obj, **kwargs):
@@ -42,6 +45,17 @@ def put(self, obj, **kwargs):
4245
except: # noqa # queue might be closed
4346
pass
4447

48+
def get_all(self) -> List[bytes]:
49+
res = []
50+
with self.lock:
51+
while True:
52+
try:
53+
res.append(self.get_nowait())
54+
except Empty:
55+
break
56+
57+
return res
58+
4559
def write(self, s: AnyStr):
4660
self.put(s)
4761

0 commit comments

Comments
 (0)