|
24 | 24 | MAC = "a4cf1218f158" |
25 | 25 |
|
26 | 26 |
|
27 | | -logging.getLogger().setLevel(logging.CRITICAL) |
28 | | - |
29 | | - |
30 | 27 | @pytest.fixture |
31 | 28 | def mock_service_info(): |
32 | 29 | """Create a mock service info.""" |
@@ -108,7 +105,6 @@ async def test_discovery_add_and_remove( |
108 | 105 |
|
109 | 106 | service.remove_service(mock_zc, zc_info.type, zc_info.name) |
110 | 107 |
|
111 | | - # mock_zc.get_service_info.assert_called_once_with(zc_info.type, zc_info.name) |
112 | 108 | mock_zc.get_service_info.assert_called_once_with(zc_info.type, zc_info.name) |
113 | 109 | mock_send.assert_not_called() |
114 | 110 | await asyncio.sleep(service._debounce_seconds + 1) |
@@ -178,20 +174,30 @@ async def test_discovery_remove_cancel( |
178 | 174 | # reset mock_send |
179 | 175 | mock_send = Mock() |
180 | 176 | monkeypatch.setattr(PowersensorServiceListener, "dispatch", mock_send) |
181 | | - # cache plug data for checking |
| 177 | + |
| 178 | + # ensure we start from a known state |
| 179 | + assert len(service._pending_removals) == 0 |
182 | 180 |
|
183 | 181 | service.remove_service(mock_zc, zc_info.type, zc_info.name) |
184 | 182 |
|
185 | | - # mock_zc.get_service_info.assert_called_once_with(zc_info.type, zc_info.name) |
186 | 183 | mock_zc.get_service_info.assert_called_once_with(zc_info.type, zc_info.name) |
187 | 184 | mock_send.assert_not_called() |
| 185 | + assert len(service._pending_removals) == 1 |
188 | 186 |
|
| 187 | + # give the remove a head start before triggering the cancellation |
| 188 | + await asyncio.sleep(0.5) |
| 189 | + |
| 190 | + # re-add the service, which should cancel the pending remove |
189 | 191 | service.add_service(mock_zc, zc_info.type, zc_info.name) |
190 | 192 | assert mock_zc.get_service_info.call_count == 2 |
191 | 193 | mock_zc.get_service_info.assert_has_calls( |
192 | 194 | [call(zc_info.type, zc_info.name), call(zc_info.type, zc_info.name)] |
193 | 195 | ) |
194 | 196 |
|
| 197 | + # let the removal task do its cancellation stuff |
| 198 | + await asyncio.sleep(0.5) |
| 199 | + assert len(service._pending_removals) == 0 |
| 200 | + |
195 | 201 |
|
196 | 202 | @pytest.mark.asyncio |
197 | 203 | async def test_discovery_add_and_two_remove_calls( |
|
0 commit comments