@@ -547,6 +547,18 @@ async def node_motion_state(
547547 )
548548 )
549549
550+ async def _wait_for_scan (self , stick , timeout : float = 10.0 ) -> None :
551+ """Wait for scan completion with timeout."""
552+
553+ async def wait_scan_completed ():
554+ while not stick ._network ._register .scan_completed :
555+ await asyncio .sleep (0.1 )
556+
557+ try :
558+ await asyncio .wait_for (wait_scan_completed (), timeout = timeout )
559+ except TimeoutError :
560+ pytest .fail (f"Scan did not complete within { timeout } seconds" )
561+
550562 @pytest .mark .asyncio
551563 async def test_stick_node_discovered_subscription ( # noqa: PLR0915
552564 self , monkeypatch : pytest .MonkeyPatch
@@ -564,8 +576,7 @@ async def test_stick_node_discovered_subscription( # noqa: PLR0915
564576 await stick .connect ()
565577 await stick .initialize ()
566578 await stick .discover_nodes (load = False )
567- while not stick ._network ._register .scan_completed :
568- await asyncio .sleep (0.1 )
579+ await self ._wait_for_scan (stick )
569580 self .test_node_awake = asyncio .Future ()
570581 unsub_awake = stick .subscribe_to_node_events (
571582 node_event_callback = self .node_awake ,
@@ -691,8 +702,7 @@ async def test_node_discovery(self, monkeypatch: pytest.MonkeyPatch) -> None:
691702 await stick .connect ()
692703 await stick .initialize ()
693704 await stick .discover_nodes (load = False )
694- while not stick ._network ._register .scan_completed :
695- await asyncio .sleep (0.1 )
705+ await self ._wait_for_scan (stick )
696706 assert stick .joined_nodes == 9
697707 assert stick .nodes .get ("0098765432101234" ) is not None
698708 assert len (stick .nodes ) == 7 # Discovered nodes
@@ -767,8 +777,7 @@ async def test_node_relay_and_power(self, monkeypatch: pytest.MonkeyPatch) -> No
767777 await stick .connect ()
768778 await stick .initialize ()
769779 await stick .discover_nodes (load = False )
770- while not stick ._network ._register .scan_completed :
771- await asyncio .sleep (0.1 )
780+ await self ._wait_for_scan (stick )
772781
773782 # Validate if NodeError is raised when device is not loaded
774783 with pytest .raises (pw_exceptions .NodeError ):
@@ -906,8 +915,7 @@ async def fake_get_missing_energy_logs(address: int) -> None:
906915 await stick .connect ()
907916 await stick .initialize ()
908917 await stick .discover_nodes (load = False )
909- while not stick ._network ._register .scan_completed :
910- await asyncio .sleep (0.1 )
918+ await self ._wait_for_scan (stick )
911919
912920 # Check calibration in unloaded state
913921 assert not stick .nodes ["0098765432101234" ].calibrated
@@ -2437,8 +2445,7 @@ def fake_cache(dummy: object, setting: str) -> str | bool | None: # noqa: PLR09
24372445 with patch ("aiofiles.threadpool.sync_open" , return_value = mock_file_stream ):
24382446 await stick .initialize ()
24392447 await stick .discover_nodes (load = True )
2440- while not stick ._network ._register .scan_completed :
2441- await asyncio .sleep (0.1 )
2448+ await self ._wait_for_scan (stick )
24422449 assert len (stick .nodes ) == 7
24432450
24442451 assert stick .nodes ["0098765432101234" ].is_loaded
0 commit comments