From acebcafe0e4e7b30789f5d7e929b0fea151e39d5 Mon Sep 17 00:00:00 2001 From: Scott K Logan Date: Wed, 9 Aug 2023 19:09:52 -0500 Subject: [PATCH] Avoid race condition in event_reactor test These tests essentially queue an event, wait for the event reactor to process the event, and check that an appropriate function was or was not called. The way we were previously waiting for the event reactor thread to do the work left a window between popping the work from the queue and actually passing the event to the observers. If the flush loop finished early in this window, there was a chance that the test's assert would beat the event reactor, which hadn't yet invoked the observers. Rather than change the behavior of the flush function to also ensure that the popped work had completed processing, we can simply use the queue's integrated work tracking, which won't unblock until there is no work in the queue OR processing. --- test/test_event_reactor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_event_reactor.py b/test/test_event_reactor.py index 81c62f81..ab1620fd 100644 --- a/test/test_event_reactor.py +++ b/test/test_event_reactor.py @@ -66,15 +66,15 @@ def test_create_event_reactor(): assert error.call_count == 0 queue.put(('first', None)) - event_reactor.flush() + queue.join() assert error.call_count == 1 queue.put(('second', None)) - event_reactor.flush() + queue.join() assert error.call_count == 1 queue.put(('third', None)) - event_reactor.flush() + queue.join() assert error.call_count == 2 # 1 timer event, 3 mock string events