Robust thread termination in `test_watch` and `test_watch_requires_lock_to_run`
@@ -198,12 +198,15 @@ def test_watch():
stop_called.set()
return time() > start + 0.500
+ try:
log = watch(interval="10ms", cycle="50ms", stop=stop)
stop_called.wait(2)
sleep(0.5)
assert 1 < len(log) < 10
- watch_thread.join(2)
+ finally:
+ stop_called.wait()
+ watch_thread.join()
def test_watch_requires_lock_to_run():
@@ -232,6 +235,8 @@ def test_watch_requires_lock_to_run():
# Block the lock over the entire duration of watch
blocking_thread = threading.Thread(target=block_lock, name="Block Lock")
blocking_thread.daemon = True
+
+ try:
blocking_thread.start()
log = watch(interval="10ms", cycle="50ms", stop=stop_profiling)
@@ -244,9 +249,11 @@ def test_watch_requires_lock_to_run():
sleep(0.5)
assert len(log) == 0
release_lock.set()
-
- profiling_thread.join(2)
- blocking_thread.join(2)
+ finally:
+ release_lock.set()
+ stop_profiling_called.wait()
+ blocking_thread.join()
+ profiling_thread.join()
@dataclasses.dataclass(frozen=True)
