Skip to content

Commit e7613cc

Browse files
tests: preserve prompt handler expectations
Use the original 100 ms expectation for single handler operations. Reserve a 500 ms batch deadline for five-item tests, including the case observed exceeding 100 ms in CI. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent cd1125b commit e7613cc

3 files changed

Lines changed: 32 additions & 30 deletions

File tree

‎tests/unit/helpers.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
# Maximum time allowed for an operation expected to complete promptly.
88
PROMPT_TIMEOUT = 0.1
99

10-
# Maximum time allowed for asynchronous work crossing threads or event loops.
11-
EVENTUAL_TIMEOUT = 1
10+
# Maximum time allowed for a prompt batch of asynchronous operations.
11+
BATCH_COMPLETION_TIMEOUT = 0.5

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from azure.iot.device.iothub.sync_handler_manager import MESSAGE, METHOD, TWIN_DP_PATCH
1919
from azure.iot.device.iothub.inbox_manager import InboxManager
2020
from azure.iot.device.iothub.aio.async_inbox import AsyncClientInbox
21-
from tests.unit.helpers import EVENTUAL_TIMEOUT
21+
from tests.unit.helpers import BATCH_COMPLETION_TIMEOUT, PROMPT_TIMEOUT
2222

2323
logging.basicConfig(level=logging.DEBUG)
2424

@@ -414,7 +414,7 @@ async def test_handler_invoked(
414414
mock_obj = mocker.MagicMock()
415415
inbox.put(mock_obj)
416416
# Wait for the handler invocation to complete
417-
await async_poll_until(lambda: handler_checker.handler_called, timeout=EVENTUAL_TIMEOUT)
417+
await async_poll_until(lambda: handler_checker.handler_called, timeout=PROMPT_TIMEOUT)
418418

419419
# Handler has been called with the item from the inbox
420420
assert handler_checker.handler_called is True
@@ -443,7 +443,8 @@ async def test_handler_invoked_multiple(
443443
inbox.put(mocker.MagicMock())
444444
# Wait for all handler invocations to complete
445445
await async_poll_until(
446-
lambda: handler_checker.handler_call_count >= 5, timeout=EVENTUAL_TIMEOUT
446+
lambda: handler_checker.handler_call_count >= 5,
447+
timeout=BATCH_COMPLETION_TIMEOUT,
447448
)
448449

449450
# Handler has been called 5 times
@@ -553,7 +554,7 @@ async def coro_handler(arg):
553554
# Add an item to corresponding inbox, triggering the handler
554555
inbox.put(mocker.MagicMock())
555556
# Wait for the background exception handler to be called
556-
await async_poll_until(lambda: background_exc_spy.call_count >= 1, timeout=EVENTUAL_TIMEOUT)
557+
await async_poll_until(lambda: background_exc_spy.call_count >= 1, timeout=PROMPT_TIMEOUT)
557558
# Background exception handler was called
558559
assert background_exc_spy.call_count == 1
559560
e = background_exc_spy.call_args[0][0]
@@ -570,7 +571,7 @@ async def coro_handler(arg):
570571
# Add an item to corresponding inbox, triggering the handler
571572
inbox.put(mocker.MagicMock())
572573
# Wait for the background exception handler to be called
573-
await async_poll_until(lambda: background_exc_spy.call_count >= 1, timeout=EVENTUAL_TIMEOUT)
574+
await async_poll_until(lambda: background_exc_spy.call_count >= 1, timeout=PROMPT_TIMEOUT)
574575
# Background exception handler was called
575576
assert background_exc_spy.call_count == 1
576577
e = background_exc_spy.call_args[0][0]
@@ -600,7 +601,7 @@ def handler1(arg):
600601
# Wait for handler1 to replace itself with handler2
601602
await async_poll_until(
602603
lambda: getattr(handler_manager, handler_name) is handler2,
603-
timeout=EVENTUAL_TIMEOUT,
604+
timeout=PROMPT_TIMEOUT,
604605
)
605606
# The set handler (handler1) has been replaced with a new handler (handler2)
606607
assert getattr(handler_manager, handler_name) is not handler1
@@ -610,15 +611,15 @@ def handler1(arg):
610611
# Wait for handler2 to replace itself with the mock
611612
await async_poll_until(
612613
lambda: getattr(handler_manager, handler_name) is mock_handler,
613-
timeout=EVENTUAL_TIMEOUT,
614+
timeout=PROMPT_TIMEOUT,
614615
)
615616
# The set handler (handler2) has now been replaced by a mock handler
616617
assert getattr(handler_manager, handler_name) is not handler2
617618
assert getattr(handler_manager, handler_name) is mock_handler
618619
# Add a new item to the inbox
619620
inbox.put(mocker.MagicMock())
620621
# Wait for the mock handler invocation to complete
621-
await async_poll_until(lambda: mock_handler.call_count >= 1, timeout=EVENTUAL_TIMEOUT)
622+
await async_poll_until(lambda: mock_handler.call_count >= 1, timeout=PROMPT_TIMEOUT)
622623
# The mock was now called
623624
assert getattr(handler_manager, handler_name).call_count == 1
624625

@@ -712,7 +713,7 @@ async def test_handler_invoked(
712713
inbox.put(event)
713714
# Wait for the handler invocation to complete
714715
await async_poll_until(
715-
lambda: handler_checker.handler_call_count >= 1, timeout=EVENTUAL_TIMEOUT
716+
lambda: handler_checker.handler_call_count >= 1, timeout=PROMPT_TIMEOUT
716717
)
717718

718719
# Handler has been called with the arguments from the event
@@ -723,7 +724,7 @@ async def test_handler_invoked(
723724
non_matching_event = client_event.ClientEvent("NON_MATCHING_EVENT")
724725
inbox.put(non_matching_event)
725726
# Wait for the runner to consume the non-matching event
726-
await async_poll_until(inbox.empty, timeout=EVENTUAL_TIMEOUT)
727+
await async_poll_until(inbox.empty, timeout=PROMPT_TIMEOUT)
727728

728729
# Handler has not been called again
729730
assert handler_checker.handler_call_count == 1
@@ -751,7 +752,8 @@ async def test_handler_invoked_multiple(
751752
inbox.put(event)
752753
# Wait for all handler invocations to complete
753754
await async_poll_until(
754-
lambda: handler_checker.handler_call_count >= 5, timeout=EVENTUAL_TIMEOUT
755+
lambda: handler_checker.handler_call_count >= 5,
756+
timeout=BATCH_COMPLETION_TIMEOUT,
755757
)
756758

757759
# Handler has been called 5 times
@@ -786,7 +788,7 @@ async def coro_handler(*args):
786788
# Add an item to corresponding inbox, triggering the handler
787789
inbox.put(event)
788790
# Wait for the background exception handler to be called
789-
await async_poll_until(lambda: background_exc_spy.call_count >= 1, timeout=EVENTUAL_TIMEOUT)
791+
await async_poll_until(lambda: background_exc_spy.call_count >= 1, timeout=PROMPT_TIMEOUT)
790792
# Background exception handler was called
791793
assert background_exc_spy.call_count == 1
792794
e = background_exc_spy.call_args[0][0]
@@ -803,7 +805,7 @@ async def coro_handler(*args):
803805
# Add an item to corresponding inbox, triggering the handler
804806
inbox.put(event)
805807
# Wait for the background exception handler to be called
806-
await async_poll_until(lambda: background_exc_spy.call_count >= 1, timeout=EVENTUAL_TIMEOUT)
808+
await async_poll_until(lambda: background_exc_spy.call_count >= 1, timeout=PROMPT_TIMEOUT)
807809
# Background exception handler was called
808810
assert background_exc_spy.call_count == 1
809811
e = background_exc_spy.call_args[0][0]
@@ -833,7 +835,7 @@ def handler1(*args):
833835
# Wait for handler1 to replace itself with handler2
834836
await async_poll_until(
835837
lambda: getattr(handler_manager, handler_name) is handler2,
836-
timeout=EVENTUAL_TIMEOUT,
838+
timeout=PROMPT_TIMEOUT,
837839
)
838840
# The set handler (handler1) has been replaced with a new handler (handler2)
839841
assert getattr(handler_manager, handler_name) is not handler1
@@ -843,15 +845,15 @@ def handler1(*args):
843845
# Wait for handler2 to replace itself with the mock
844846
await async_poll_until(
845847
lambda: getattr(handler_manager, handler_name) is mock_handler,
846-
timeout=EVENTUAL_TIMEOUT,
848+
timeout=PROMPT_TIMEOUT,
847849
)
848850
# The set handler (handler2) has now been replaced by a mock handler
849851
assert getattr(handler_manager, handler_name) is not handler2
850852
assert getattr(handler_manager, handler_name) is mock_handler
851853
# Add a new item to the inbox
852854
inbox.put(event)
853855
# Wait for the mock handler invocation to complete
854-
await async_poll_until(lambda: mock_handler.call_count >= 1, timeout=EVENTUAL_TIMEOUT)
856+
await async_poll_until(lambda: mock_handler.call_count >= 1, timeout=PROMPT_TIMEOUT)
855857
# The mock was now called
856858
assert getattr(handler_manager, handler_name).call_count == 1
857859

‎tests/unit/iothub/test_sync_handler_manager.py‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from azure.iot.device.iothub.sync_handler_manager import MESSAGE, METHOD, TWIN_DP_PATCH
1818
from azure.iot.device.iothub.inbox_manager import InboxManager
1919
from azure.iot.device.iothub.sync_inbox import SyncClientInbox
20-
from tests.unit.helpers import EVENTUAL_TIMEOUT
20+
from tests.unit.helpers import BATCH_COMPLETION_TIMEOUT, PROMPT_TIMEOUT
2121

2222
logging.basicConfig(level=logging.DEBUG)
2323

@@ -364,7 +364,7 @@ def test_handler_invoked(self, mocker, handler_name, handler_manager, inbox, pol
364364
mock_obj = mocker.MagicMock()
365365
inbox.put(mock_obj)
366366
# Wait for the handler invocation to complete
367-
poll_until(lambda: mock_handler.call_count >= 1, timeout=EVENTUAL_TIMEOUT)
367+
poll_until(lambda: mock_handler.call_count >= 1, timeout=PROMPT_TIMEOUT)
368368

369369
# Handler has been called with the item from the inbox
370370
assert mock_handler.call_count == 1
@@ -386,7 +386,7 @@ def test_handler_invoked_multiple(
386386
for _ in range(5):
387387
inbox.put(mocker.MagicMock())
388388
# Wait for all handler invocations to complete
389-
poll_until(lambda: mock_handler.call_count >= 5, timeout=EVENTUAL_TIMEOUT)
389+
poll_until(lambda: mock_handler.call_count >= 5, timeout=BATCH_COMPLETION_TIMEOUT)
390390

391391
# Handler has been called 5 times
392392
assert mock_handler.call_count == 5
@@ -486,7 +486,7 @@ def test_exception_in_handler(
486486
# Add an item to corresponding inbox, triggering the handler
487487
inbox.put(mocker.MagicMock())
488488
# Wait for the background exception handler to be called
489-
poll_until(lambda: background_exc_spy.call_count >= 1, timeout=EVENTUAL_TIMEOUT)
489+
poll_until(lambda: background_exc_spy.call_count >= 1, timeout=PROMPT_TIMEOUT)
490490
# Handler has now been called
491491
assert mock_handler.call_count == 1
492492
# Background exception handler was called
@@ -509,7 +509,7 @@ def handler(arg):
509509
# Wait for the handler to replace itself with the mock
510510
poll_until(
511511
lambda: getattr(handler_manager, handler_name) is not handler,
512-
timeout=EVENTUAL_TIMEOUT,
512+
timeout=PROMPT_TIMEOUT,
513513
)
514514
# Handler has been replaced with a mock, but the mock has not been invoked
515515
assert getattr(handler_manager, handler_name) is not handler
@@ -519,7 +519,7 @@ def handler(arg):
519519
# Wait for the mock handler invocation to complete
520520
poll_until(
521521
lambda: getattr(handler_manager, handler_name).call_count >= 1,
522-
timeout=EVENTUAL_TIMEOUT,
522+
timeout=PROMPT_TIMEOUT,
523523
)
524524
# The mock was now called
525525
assert getattr(handler_manager, handler_name).call_count == 1
@@ -604,7 +604,7 @@ def test_handler_invoked(self, mocker, handler_name, handler_manager, inbox, eve
604604
# Add the event to the client event inbox
605605
inbox.put(event)
606606
# Wait for the handler invocation to complete
607-
poll_until(lambda: mock_handler.call_count >= 1, timeout=EVENTUAL_TIMEOUT)
607+
poll_until(lambda: mock_handler.call_count >= 1, timeout=PROMPT_TIMEOUT)
608608

609609
# Handler has been called with the arguments from the event
610610
assert mock_handler.call_count == 1
@@ -614,7 +614,7 @@ def test_handler_invoked(self, mocker, handler_name, handler_manager, inbox, eve
614614
non_matching_event = client_event.ClientEvent("NON_MATCHING_EVENT")
615615
inbox.put(non_matching_event)
616616
# Wait for the runner to consume the non-matching event
617-
poll_until(inbox.empty, timeout=EVENTUAL_TIMEOUT)
617+
poll_until(inbox.empty, timeout=PROMPT_TIMEOUT)
618618

619619
# Handler has not been called again
620620
assert mock_handler.call_count == 1
@@ -635,7 +635,7 @@ def test_handler_invoked_multiple(
635635
for _ in range(5):
636636
inbox.put(event)
637637
# Wait for all handler invocations to complete
638-
poll_until(lambda: mock_handler.call_count >= 5, timeout=EVENTUAL_TIMEOUT)
638+
poll_until(lambda: mock_handler.call_count >= 5, timeout=BATCH_COMPLETION_TIMEOUT)
639639

640640
# Handler has been called 5 times
641641
assert mock_handler.call_count == 5
@@ -659,7 +659,7 @@ def test_exception_in_handler(
659659
# Add the event to the client event inbox, triggering the handler
660660
inbox.put(event)
661661
# Wait for the background exception handler to be called
662-
poll_until(lambda: background_exc_spy.call_count >= 1, timeout=EVENTUAL_TIMEOUT)
662+
poll_until(lambda: background_exc_spy.call_count >= 1, timeout=PROMPT_TIMEOUT)
663663
# Handler has now been called
664664
assert mock_handler.call_count == 1
665665
# Background exception handler was called
@@ -682,7 +682,7 @@ def handler(*args):
682682
# Wait for the handler to replace itself with the mock
683683
poll_until(
684684
lambda: getattr(handler_manager, handler_name) is not handler,
685-
timeout=EVENTUAL_TIMEOUT,
685+
timeout=PROMPT_TIMEOUT,
686686
)
687687
# Handler has been replaced with a mock, but the mock has not been invoked
688688
assert getattr(handler_manager, handler_name) is not handler
@@ -692,7 +692,7 @@ def handler(*args):
692692
# Wait for the mock handler invocation to complete
693693
poll_until(
694694
lambda: getattr(handler_manager, handler_name).call_count >= 1,
695-
timeout=EVENTUAL_TIMEOUT,
695+
timeout=PROMPT_TIMEOUT,
696696
)
697697
# The mock was now called
698698
assert getattr(handler_manager, handler_name).call_count == 1

0 commit comments

Comments
 (0)