Skip to content

Commit 7314789

Browse files
committed
refactor(follower): stabilize test runner
1 parent f72bc21 commit 7314789

File tree

1 file changed

+13
-34
lines changed

1 file changed

+13
-34
lines changed

test/unit/features/conftest.py

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
import os
2424
import json
2525
import responses
26-
import time
2726

28-
import threading
29-
import queue
27+
from threading import Timer
3028

3129
import itertools
3230

@@ -218,46 +216,27 @@ def prepare_mock_with_error(self, error: str):
218216
json={'error': error},
219217
)
220218

221-
def runner(self, follower, mode, timeout=0, stop_after=0):
219+
def runner(self, follower, mode, timeout=1, stop_after=0):
222220
"""
223221
blocking runner with timeout
224222
"""
225223

226-
class TimeoutError(Exception):
227-
pass
228-
229-
def looper(changes, buf):
224+
def main():
225+
if mode == _Mode.LISTEN:
226+
changes = follower.start()
227+
elif mode == _Mode.FINITE:
228+
changes = follower.start_one_off()
229+
stop_timer = Timer(timeout, follower.stop)
230+
stop_timer.start()
230231
counter = 0
231-
buf.put(counter)
232232
try:
233233
for _ in changes:
234234
counter += 1
235235
if stop_after > 0 and stop_after == counter:
236236
follower.stop()
237-
except Exception as e:
238-
buf.put(e)
239-
return
240-
buf.put(counter)
241-
242-
def main():
243-
if mode == _Mode.LISTEN:
244-
changes = follower.start()
245-
elif mode == _Mode.FINITE:
246-
changes = follower.start_one_off()
247-
buf = queue.Queue()
248-
thread = threading.Thread(target=looper, args=(changes, buf))
249-
thread.start()
250-
thread.join(timeout)
251-
while True:
252-
time.sleep(0.2)
253-
if thread.is_alive():
254-
follower.stop()
255-
else:
256-
break
257-
while not buf.empty():
258-
data = buf.get()
259-
if isinstance(data, Exception):
260-
raise data
261-
return data
237+
break
238+
finally:
239+
stop_timer.cancel()
240+
return counter
262241

263242
return main()

0 commit comments

Comments
 (0)