2424class FakeServiceInfo (AsyncServiceInfo ):
2525 """An AsyncServiceInfo with a controllable cache and plug response.
2626
27- Keyword arguments override the plug's record, as passed to ServiceInfo.
27+ If gate is given, the plug only answers once the gate is set. Keyword
28+ arguments override the plug's record, as passed to ServiceInfo.
2829 """
2930
30- def __init__ (self , cached : bool , answers : bool , ** record : Any ) -> None :
31+ def __init__ (
32+ self , cached : bool , answers : bool , gate : asyncio .Event | None = None ,
33+ ** record : Any ,
34+ ) -> None :
3135 super ().__init__ (TYPE , NAME , ** {
3236 'port' : 49476 ,
3337 'properties' : {b'id' : b'aabbccddeeff' },
@@ -36,7 +40,9 @@ def __init__(self, cached: bool, answers: bool, **record: Any) -> None:
3640 })
3741 self .cached = cached
3842 self .answers = answers
43+ self .gate = gate
3944 self .requested = False
45+ self .requests = 0
4046
4147 def load_from_cache (self , zc : zeroconf .Zeroconf , now : float | None = None ) -> bool :
4248 return self .cached
@@ -45,6 +51,9 @@ async def async_request(
4551 self , zc : zeroconf .Zeroconf , timeout : float , * args : Any , ** kwargs : Any ,
4652 ) -> bool :
4753 self .requested = True
54+ self .requests += 1
55+ if self .gate is not None :
56+ await self .gate .wait ()
4857 return self .answers
4958
5059
@@ -54,7 +63,13 @@ async def settle(devices: PowersensorZeroconfDevices) -> None:
5463 await asyncio .sleep (0 )
5564 if not devices ._internal_callbacks :
5665 return
57- await asyncio .gather (* list (devices ._internal_callbacks ))
66+ await asyncio .gather (* list (devices ._internal_callbacks ), return_exceptions = True )
67+
68+
69+ async def let_run () -> None :
70+ """Let scheduled tasks run up to their next wait, without waiting for them."""
71+ for _ in range (5 ):
72+ await asyncio .sleep (0 )
5873
5974
6075@pytest .mark .asyncio
@@ -105,6 +120,95 @@ async def test_unanswered_request_is_logged(caplog: pytest.LogCaptureFixture) ->
105120 assert f'No response from { NAME } ' in caplog .text
106121
107122
123+ @pytest .mark .asyncio
124+ async def test_removal_before_answer_cancels_lookup (caplog : pytest .LogCaptureFixture ) -> None :
125+ zc = MagicMock ()
126+ devices = PowersensorZeroconfDevices (
127+ zeroconf_instance = zc , logger = logging .getLogger ('test' ),
128+ )
129+ gate = asyncio .Event ()
130+ info = FakeServiceInfo (cached = False , answers = True , gate = gate )
131+ with caplog .at_level (logging .DEBUG ), \
132+ patch .object (zd , 'AsyncServiceInfo' , return_value = info ), \
133+ patch .object (devices , '_on_zc_add' , AsyncMock ()) as on_add , \
134+ patch .object (devices , '_on_zc_remove' , AsyncMock ()) as on_remove :
135+ listener = zd ._Listener (devices )
136+ listener .add_service (zc , TYPE , NAME )
137+ await let_run ()
138+ lookup = listener ._lookups [NAME ]
139+ listener .remove_service (zc , TYPE , NAME )
140+ gate .set ()
141+ await settle (devices )
142+ assert lookup .cancelled ()
143+ assert 'removed before it answered' in caplog .text
144+ # The plug was never dispatched, so there is nothing to remove either.
145+ on_add .assert_not_awaited ()
146+ on_remove .assert_not_awaited ()
147+ assert 'removal ignored' not in caplog .text
148+
149+
150+ @pytest .mark .asyncio
151+ async def test_removal_during_update_lookup_removes_known_plug () -> None :
152+ zc = MagicMock ()
153+ devices = PowersensorZeroconfDevices (zeroconf_instance = zc )
154+ gate = asyncio .Event ()
155+ cached = FakeServiceInfo (cached = True , answers = False )
156+ uncached = FakeServiceInfo (cached = False , answers = True , gate = gate )
157+ with patch .object (zd , 'AsyncServiceInfo' , side_effect = [cached , uncached ]), \
158+ patch .object (devices , '_on_zc_add' , AsyncMock ()) as on_add , \
159+ patch .object (devices , '_on_zc_update' , AsyncMock ()) as on_update , \
160+ patch .object (devices , '_on_zc_remove' , AsyncMock ()) as on_remove :
161+ listener = zd ._Listener (devices )
162+ listener .add_service (zc , TYPE , NAME )
163+ await settle (devices )
164+ listener .update_service (zc , TYPE , NAME )
165+ await let_run ()
166+ listener .remove_service (zc , TYPE , NAME )
167+ gate .set ()
168+ await settle (devices )
169+ # The update's lookup is cancelled, but the plug was already dispatched by
170+ # the add, so it must still be removed.
171+ on_add .assert_awaited_once_with ('aabbccddeeff' , '192.0.2.10' , 49476 )
172+ on_update .assert_not_awaited ()
173+ on_remove .assert_awaited_once_with ('aabbccddeeff' )
174+
175+
176+ @pytest .mark .asyncio
177+ async def test_one_lookup_per_plug_at_a_time () -> None :
178+ zc = MagicMock ()
179+ devices = PowersensorZeroconfDevices (zeroconf_instance = zc )
180+ gate = asyncio .Event ()
181+ info = FakeServiceInfo (cached = False , answers = True , gate = gate )
182+ with patch .object (zd , 'AsyncServiceInfo' , return_value = info ), \
183+ patch .object (devices , '_on_zc_add' , AsyncMock ()) as on_add , \
184+ patch .object (devices , '_on_zc_update' , AsyncMock ()) as on_update :
185+ listener = zd ._Listener (devices )
186+ listener .add_service (zc , TYPE , NAME )
187+ await let_run ()
188+ listener .update_service (zc , TYPE , NAME )
189+ gate .set ()
190+ await settle (devices )
191+ assert info .requests == 1
192+ on_add .assert_awaited_once_with ('aabbccddeeff' , '192.0.2.10' , 49476 )
193+ on_update .assert_not_awaited ()
194+
195+
196+ @pytest .mark .asyncio
197+ async def test_removal_after_answer_is_debounced () -> None :
198+ zc = MagicMock ()
199+ devices = PowersensorZeroconfDevices (zeroconf_instance = zc )
200+ info = FakeServiceInfo (cached = False , answers = True )
201+ with patch .object (zd , 'AsyncServiceInfo' , return_value = info ), \
202+ patch .object (devices , '_on_zc_add' , AsyncMock ()), \
203+ patch .object (devices , '_on_zc_remove' , AsyncMock ()) as on_remove :
204+ listener = zd ._Listener (devices )
205+ listener .add_service (zc , TYPE , NAME )
206+ await settle (devices )
207+ listener .remove_service (zc , TYPE , NAME )
208+ await settle (devices )
209+ on_remove .assert_awaited_once_with ('aabbccddeeff' )
210+
211+
108212@pytest .mark .asyncio
109213@pytest .mark .parametrize ('cached' , [True , False ], ids = ['cached' , 'requested' ])
110214@pytest .mark .parametrize (
0 commit comments