Skip to content

Commit a8070b5

Browse files
tests: remove redundant handler sleeps
After handler removal returns, directly assert that the handler and runner have been cleared before queuing new items. This proves no worker can invoke the removed handler without an observation sleep. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e7613cc commit a8070b5

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ async def test_handler_invoked_multiple(
427427
self,
428428
mocker,
429429
handler_name,
430+
handler_name_internal,
430431
handler_manager,
431432
handler,
432433
handler_checker,
@@ -457,6 +458,7 @@ async def test_handler_resolve_pending_items_before_handler_removal(
457458
self,
458459
mocker,
459460
handler_name,
461+
handler_name_internal,
460462
handler_manager,
461463
handler,
462464
handler_checker,
@@ -521,12 +523,12 @@ def tracked_put(item):
521523
# time of the removal
522524
assert handler_checker.handler_call_count == 100
523525
assert inbox.empty()
526+
assert getattr(handler_manager, handler_name) is None
527+
assert handler_manager._receiver_handler_runners[handler_name_internal] is None
524528

525529
# Add some more items
526530
for _ in range(100):
527531
inbox.put(mocker.MagicMock())
528-
# Give any incorrectly retained runner an opportunity to invoke the handler
529-
await asyncio.sleep(0.1)
530532
# Despite more items added to inbox, no further handler calls have been made beyond the
531533
# initial calls that were made when the original items were added
532534
assert handler_checker.handler_call_count == 100

‎tests/unit/iothub/test_sync_handler_manager.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import logging
77
import pytest
88
import threading
9-
import time
109
from azure.iot.device.common import handle_exceptions
1110
from azure.iot.device.iothub import client_event
1211
from azure.iot.device.iothub.sync_handler_manager import (
@@ -395,7 +394,13 @@ def test_handler_invoked_multiple(
395394
"Is invoked for every item already in the corresponding Inbox at the moment of handler removal"
396395
)
397396
def test_handler_resolve_pending_items_before_handler_removal(
398-
self, mocker, handler_name, handler_manager, inbox, run_in_daemon_thread
397+
self,
398+
mocker,
399+
handler_name,
400+
handler_name_internal,
401+
handler_manager,
402+
inbox,
403+
run_in_daemon_thread,
399404
):
400405
# Intercept inbox operations so the runner can be paused with work remaining
401406
original_get = inbox.get
@@ -456,12 +461,12 @@ def tracked_put(item):
456461
# time of the removal
457462
assert mock_handler.call_count == 100
458463
assert inbox.empty()
464+
assert getattr(handler_manager, handler_name) is None
465+
assert handler_manager._receiver_handler_runners[handler_name_internal] is None
459466

460467
# Add some more items
461468
for _ in range(100):
462469
inbox.put(mocker.MagicMock())
463-
# Give any incorrectly retained runner an opportunity to invoke the handler
464-
time.sleep(0.2)
465470
# Despite more items added to inbox, no further handler calls have been made beyond the
466471
# initial calls that were made when the original items were added
467472
assert mock_handler.call_count == 100

0 commit comments

Comments
 (0)