Skip to content

Commit 047e0cd

Browse files
cartertinneyCopilot
andcommitted
tests: clarify polling helper names
Rename predicate-polling fixtures to poll_until and async_poll_until so they are clearly distinct from asyncio.wait_for, which applies a deadline to an awaitable. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 46ac769 commit 047e0cd

4 files changed

Lines changed: 62 additions & 58 deletions

File tree

‎tests/conftest.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@
1313

1414

1515
@pytest.fixture
16-
def wait_for():
17-
def wait_for_condition(condition, timeout=5, interval=0.01):
16+
def poll_until():
17+
def poll_until_condition(condition, timeout=5, interval=0.01):
1818
deadline = time.monotonic() + timeout
1919
while not condition():
2020
remaining = deadline - time.monotonic()
2121
if remaining <= 0:
2222
pytest.fail("Timed out waiting for condition")
2323
time.sleep(min(interval, remaining))
2424

25-
return wait_for_condition
25+
return poll_until_condition
2626

2727

2828
@pytest.fixture
29-
def async_wait_for():
30-
async def wait_for_condition(condition, timeout=5, interval=0.01):
29+
def async_poll_until():
30+
async def poll_until_condition(condition, timeout=5, interval=0.01):
3131
async def poll_condition():
3232
while not condition():
3333
await asyncio.sleep(interval)
@@ -37,7 +37,7 @@ async def poll_condition():
3737
except asyncio.TimeoutError:
3838
pytest.fail("Timed out waiting for condition")
3939

40-
return wait_for_condition
40+
return poll_until_condition
4141

4242

4343
@pytest.fixture

‎tests/unit/common/pipeline/test_pipeline_stages_base.py‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,11 @@ def event(self):
183183
return pipeline_events_base.ConnectedEvent()
184184

185185
@pytest.mark.it("Invokes the 'on_connected_handler' handler function, if set")
186-
def test_invoke_handler(self, mocker, stage, event, wait_for):
186+
def test_invoke_handler(self, mocker, stage, event, poll_until):
187187
mock_handler = mocker.MagicMock()
188188
stage.on_connected_handler = mock_handler
189189
stage.handle_pipeline_event(event)
190-
wait_for(lambda: mock_handler.call_count >= 1)
190+
poll_until(lambda: mock_handler.call_count >= 1)
191191
assert mock_handler.call_count == 1
192192
assert mock_handler.call_args == mocker.call()
193193

@@ -203,11 +203,11 @@ def event(self):
203203
return pipeline_events_base.DisconnectedEvent()
204204

205205
@pytest.mark.it("Invokes the 'on_disconnected_handler' handler function, if set")
206-
def test_invoke_handler(self, mocker, stage, event, wait_for):
206+
def test_invoke_handler(self, mocker, stage, event, poll_until):
207207
mock_handler = mocker.MagicMock()
208208
stage.on_disconnected_handler = mock_handler
209209
stage.handle_pipeline_event(event)
210-
wait_for(lambda: mock_handler.call_count >= 1)
210+
poll_until(lambda: mock_handler.call_count >= 1)
211211
assert mock_handler.call_count == 1
212212
assert mock_handler.call_args == mocker.call()
213213

@@ -223,11 +223,11 @@ def event(self):
223223
return pipeline_events_base.NewSasTokenRequiredEvent()
224224

225225
@pytest.mark.it("Invokes the 'on_new_sastoken_required_handler' handler function, if set")
226-
def test_invoke_handler(self, mocker, stage, event, wait_for):
226+
def test_invoke_handler(self, mocker, stage, event, poll_until):
227227
mock_handler = mocker.MagicMock()
228228
stage.on_new_sastoken_required_handler = mock_handler
229229
stage.handle_pipeline_event(event)
230-
wait_for(lambda: mock_handler.call_count >= 1)
230+
poll_until(lambda: mock_handler.call_count >= 1)
231231
assert mock_handler.call_count == 1
232232
assert mock_handler.call_args == mocker.call()
233233

@@ -245,11 +245,11 @@ def event(self, arbitrary_exception):
245245
@pytest.mark.it(
246246
"Invokes the 'on_background_exception_handler' handler function, passing the exception object, if set"
247247
)
248-
def test_invoke_handler(self, mocker, stage, event, wait_for):
248+
def test_invoke_handler(self, mocker, stage, event, poll_until):
249249
mock_handler = mocker.MagicMock()
250250
stage.on_background_exception_handler = mock_handler
251251
stage.handle_pipeline_event(event)
252-
wait_for(lambda: mock_handler.call_count >= 1)
252+
poll_until(lambda: mock_handler.call_count >= 1)
253253
assert mock_handler.call_count == 1
254254
assert mock_handler.call_args == mocker.call(event.e)
255255

@@ -265,11 +265,11 @@ def event(self, arbitrary_event):
265265
return arbitrary_event
266266

267267
@pytest.mark.it("Invokes the 'on_pipeline_event_handler' handler function, if set")
268-
def test_invoke_handler(self, mocker, stage, event, wait_for):
268+
def test_invoke_handler(self, mocker, stage, event, poll_until):
269269
mock_handler = mocker.MagicMock()
270270
stage.on_pipeline_event_handler = mock_handler
271271
stage.handle_pipeline_event(event)
272-
wait_for(lambda: mock_handler.call_count >= 1)
272+
poll_until(lambda: mock_handler.call_count >= 1)
273273
assert mock_handler.call_count == 1
274274
assert mock_handler.call_args == mocker.call(event)
275275

‎tests/unit/iothub/aio/test_async_handler_manager.py‎

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ async def test_handler_invoked(
401401
handler,
402402
handler_checker,
403403
inbox,
404-
async_wait_for,
404+
async_poll_until,
405405
):
406406
# Set the handler
407407
setattr(handler_manager, handler_name, handler)
@@ -412,7 +412,7 @@ async def test_handler_invoked(
412412
# Add an item to the associated inbox, triggering the handler
413413
mock_obj = mocker.MagicMock()
414414
inbox.put(mock_obj)
415-
await async_wait_for(lambda: handler_checker.handler_called)
415+
await async_poll_until(lambda: handler_checker.handler_called)
416416

417417
# Handler has been called with the item from the inbox
418418
assert handler_checker.handler_called is True
@@ -429,7 +429,7 @@ async def test_handler_invoked_multiple(
429429
handler,
430430
handler_checker,
431431
inbox,
432-
async_wait_for,
432+
async_poll_until,
433433
):
434434
# Set the handler
435435
setattr(handler_manager, handler_name, handler)
@@ -439,7 +439,7 @@ async def test_handler_invoked_multiple(
439439
# Add 5 items to the associated inbox, triggering the handler
440440
for _ in range(5):
441441
inbox.put(mocker.MagicMock())
442-
await async_wait_for(lambda: handler_checker.handler_call_count >= 5)
442+
await async_poll_until(lambda: handler_checker.handler_call_count >= 5)
443443

444444
# Handler has been called 5 times
445445
assert handler_checker.handler_call_count == 5
@@ -525,7 +525,7 @@ def tracked_put(item):
525525
"Sends a HandlerManagerException to the background exception handler if any exception is raised during its invocation"
526526
)
527527
async def test_exception_in_handler(
528-
self, mocker, handler_name, handler_manager, inbox, arbitrary_exception, async_wait_for
528+
self, mocker, handler_name, handler_manager, inbox, arbitrary_exception, async_poll_until
529529
):
530530
# NOTE: this test tests both coroutines and functions without the need for parametrization
531531
background_exc_spy = mocker.spy(handle_exceptions, "handle_background_exception")
@@ -542,7 +542,7 @@ async def coro_handler(arg):
542542
assert background_exc_spy.call_count == 0
543543
# Add an item to corresponding inbox, triggering the handler
544544
inbox.put(mocker.MagicMock())
545-
await async_wait_for(lambda: background_exc_spy.call_count >= 1)
545+
await async_poll_until(lambda: background_exc_spy.call_count >= 1)
546546
# Background exception handler was called
547547
assert background_exc_spy.call_count == 1
548548
e = background_exc_spy.call_args[0][0]
@@ -558,7 +558,7 @@ async def coro_handler(arg):
558558
assert background_exc_spy.call_count == 0
559559
# Add an item to corresponding inbox, triggering the handler
560560
inbox.put(mocker.MagicMock())
561-
await async_wait_for(lambda: background_exc_spy.call_count >= 1)
561+
await async_poll_until(lambda: background_exc_spy.call_count >= 1)
562562
# Background exception handler was called
563563
assert background_exc_spy.call_count == 1
564564
e = background_exc_spy.call_args[0][0]
@@ -569,7 +569,7 @@ async def coro_handler(arg):
569569
"Can be updated with a new value that the corresponding handler runner will immediately begin using for handler invocations instead"
570570
)
571571
async def test_handler_update_handler(
572-
self, mocker, handler_name, handler_manager, inbox, async_wait_for
572+
self, mocker, handler_name, handler_manager, inbox, async_poll_until
573573
):
574574
# NOTE: this test tests both coroutines and functions without the need for parametrization
575575
mock_handler = mocker.MagicMock()
@@ -585,19 +585,19 @@ def handler1(arg):
585585
setattr(handler_manager, handler_name, handler1)
586586

587587
inbox.put(mocker.MagicMock())
588-
await async_wait_for(lambda: getattr(handler_manager, handler_name) is handler2)
588+
await async_poll_until(lambda: getattr(handler_manager, handler_name) is handler2)
589589
# The set handler (handler1) has been replaced with a new handler (handler2)
590590
assert getattr(handler_manager, handler_name) is not handler1
591591
assert getattr(handler_manager, handler_name) is handler2
592592
# Add a new item to the inbox
593593
inbox.put(mocker.MagicMock())
594-
await async_wait_for(lambda: getattr(handler_manager, handler_name) is mock_handler)
594+
await async_poll_until(lambda: getattr(handler_manager, handler_name) is mock_handler)
595595
# The set handler (handler2) has now been replaced by a mock handler
596596
assert getattr(handler_manager, handler_name) is not handler2
597597
assert getattr(handler_manager, handler_name) is mock_handler
598598
# Add a new item to the inbox
599599
inbox.put(mocker.MagicMock())
600-
await async_wait_for(lambda: mock_handler.call_count >= 1)
600+
await async_poll_until(lambda: mock_handler.call_count >= 1)
601601
# The mock was now called
602602
assert getattr(handler_manager, handler_name).call_count == 1
603603

@@ -680,7 +680,7 @@ async def test_handler_invoked(
680680
handler,
681681
inbox,
682682
event,
683-
async_wait_for,
683+
async_poll_until,
684684
):
685685
# Set the handler
686686
setattr(handler_manager, handler_name, handler)
@@ -689,7 +689,7 @@ async def test_handler_invoked(
689689

690690
# Add the event to the client inbox
691691
inbox.put(event)
692-
await async_wait_for(lambda: handler_checker.handler_call_count >= 1)
692+
await async_poll_until(lambda: handler_checker.handler_call_count >= 1)
693693

694694
# Handler has been called with the arguments from the event
695695
assert handler_checker.handler_call_count == 1
@@ -698,7 +698,7 @@ async def test_handler_invoked(
698698
# Add a non-matching event ot the client event inbox
699699
non_matching_event = client_event.ClientEvent("NON_MATCHING_EVENT")
700700
inbox.put(non_matching_event)
701-
await async_wait_for(inbox.empty)
701+
await async_poll_until(inbox.empty)
702702

703703
# Handler has not been called again
704704
assert handler_checker.handler_call_count == 1
@@ -714,7 +714,7 @@ async def test_handler_invoked_multiple(
714714
handler_checker,
715715
inbox,
716716
event,
717-
async_wait_for,
717+
async_poll_until,
718718
):
719719
# Set the handler
720720
setattr(handler_manager, handler_name, handler)
@@ -724,7 +724,7 @@ async def test_handler_invoked_multiple(
724724
# Add 5 items to the corresponding inbox, triggering the handler
725725
for _ in range(5):
726726
inbox.put(event)
727-
await async_wait_for(lambda: handler_checker.handler_call_count >= 5)
727+
await async_poll_until(lambda: handler_checker.handler_call_count >= 5)
728728

729729
# Handler has been called 5 times
730730
assert handler_checker.handler_call_count == 5
@@ -740,7 +740,7 @@ async def test_exception_in_handler(
740740
inbox,
741741
event,
742742
arbitrary_exception,
743-
async_wait_for,
743+
async_poll_until,
744744
):
745745
# NOTE: this test tests both coroutines and functions without the need for parametrization
746746
background_exc_spy = mocker.spy(handle_exceptions, "handle_background_exception")
@@ -757,7 +757,7 @@ async def coro_handler(*args):
757757
assert background_exc_spy.call_count == 0
758758
# Add an item to corresponding inbox, triggering the handler
759759
inbox.put(event)
760-
await async_wait_for(lambda: background_exc_spy.call_count >= 1)
760+
await async_poll_until(lambda: background_exc_spy.call_count >= 1)
761761
# Background exception handler was called
762762
assert background_exc_spy.call_count == 1
763763
e = background_exc_spy.call_args[0][0]
@@ -773,7 +773,7 @@ async def coro_handler(*args):
773773
assert background_exc_spy.call_count == 0
774774
# Add an item to corresponding inbox, triggering the handler
775775
inbox.put(event)
776-
await async_wait_for(lambda: background_exc_spy.call_count >= 1)
776+
await async_poll_until(lambda: background_exc_spy.call_count >= 1)
777777
# Background exception handler was called
778778
assert background_exc_spy.call_count == 1
779779
e = background_exc_spy.call_args[0][0]
@@ -784,7 +784,7 @@ async def coro_handler(*args):
784784
"Can be updated with a new value that the corresponding handler runner will immediately begin using for handler invocations instead"
785785
)
786786
async def test_handler_update_handler(
787-
self, mocker, handler_name, handler_manager, inbox, event, async_wait_for
787+
self, mocker, handler_name, handler_manager, inbox, event, async_poll_until
788788
):
789789
# NOTE: this test tests both coroutines and functions without the need for parametrization
790790
mock_handler = mocker.MagicMock()
@@ -800,19 +800,19 @@ def handler1(*args):
800800
setattr(handler_manager, handler_name, handler1)
801801

802802
inbox.put(event)
803-
await async_wait_for(lambda: getattr(handler_manager, handler_name) is handler2)
803+
await async_poll_until(lambda: getattr(handler_manager, handler_name) is handler2)
804804
# The set handler (handler1) has been replaced with a new handler (handler2)
805805
assert getattr(handler_manager, handler_name) is not handler1
806806
assert getattr(handler_manager, handler_name) is handler2
807807
# Add a new item to the inbox
808808
inbox.put(event)
809-
await async_wait_for(lambda: getattr(handler_manager, handler_name) is mock_handler)
809+
await async_poll_until(lambda: getattr(handler_manager, handler_name) is mock_handler)
810810
# The set handler (handler2) has now been replaced by a mock handler
811811
assert getattr(handler_manager, handler_name) is not handler2
812812
assert getattr(handler_manager, handler_name) is mock_handler
813813
# Add a new item to the inbox
814814
inbox.put(event)
815-
await async_wait_for(lambda: mock_handler.call_count >= 1)
815+
await async_poll_until(lambda: mock_handler.call_count >= 1)
816816
# The mock was now called
817817
assert getattr(handler_manager, handler_name).call_count == 1
818818

0 commit comments

Comments
 (0)