Skip to content

Commit 86771c6

Browse files
committed
refactor: drop redundant rebuild return value; trim duplicate rebuild test
1 parent d05d3e2 commit 86771c6

2 files changed

Lines changed: 7 additions & 19 deletions

File tree

src/zeroconf/_engine.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,9 @@ async def async_update_interfaces(
225225
if listen_socket is not None and any(
226226
not _listen_socket_supports(listen_socket, interface) for interface in desired.values()
227227
):
228-
listen_socket = await self._async_rebuild_listen_socket(apple_p2p, desired, current)
228+
await self._async_rebuild_listen_socket(apple_p2p, desired, current)
229229
listen_transport = self._listen_transport
230+
listen_socket = listen_transport.sock if listen_transport is not None else None
230231

231232
for bind_address, wrapped in current.items():
232233
if bind_address in desired:
@@ -310,7 +311,7 @@ async def _async_rebuild_listen_socket(
310311
apple_p2p: bool,
311312
desired: dict[tuple[str, int], str | tuple[tuple[str, int, int], int]],
312313
current: dict[tuple[str, int], _WrappedTransport],
313-
) -> socket.socket:
314+
) -> None:
314315
"""Replace the listen socket with one whose family covers the desired set.
315316
316317
The listen socket's family is otherwise fixed at construction; this
@@ -351,7 +352,6 @@ async def _async_rebuild_listen_socket(
351352
old_transport = old_listen_transport.transport
352353
self._async_remove_transport(old_transport)
353354
old_transport.close()
354-
return new_listen
355355

356356
def _async_cache_cleanup(self) -> None:
357357
"""Periodic cache cleanup."""

tests/test_interface_update.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -490,16 +490,10 @@ async def test_update_interfaces_does_not_rebuild_when_family_supported(
490490

491491

492492
@pytest.mark.asyncio
493-
async def test_update_interfaces_rebuilds_listen_socket_for_new_family(
494-
aiozc_loopback: AsyncZeroconf,
495-
) -> None:
496-
"""Adding an interface of an unsupported family rebuilds the listen socket once."""
493+
async def test_update_interfaces_rebuild_rejoins_kept_interfaces(aiozc_loopback: AsyncZeroconf) -> None:
494+
"""On a family-change rebuild, interfaces that stay are re-joined on the new listen socket."""
497495
engine = aiozc_loopback.zeroconf.engine
498496
await aiozc_loopback.zeroconf.async_wait_for_start()
499-
old_listen = engine._listen_transport
500-
assert old_listen is not None
501-
assert old_listen.sock.family == socket.AF_INET # V4Only loopback instance
502-
old_underlying = old_listen.transport
503497

504498
# Keep the existing IPv4 interface and add an IPv6 one (which the IPv4
505499
# listen socket can't join, forcing a rebuild).
@@ -514,25 +508,19 @@ async def fake_wrap(sock: object, is_sender: bool) -> _WrappedTransport:
514508

515509
with (
516510
patch.object(_engine, "normalize_interface_choice", return_value=["127.0.0.1", v6]),
517-
patch.object(_engine, "new_socket", return_value=new_listen_sock) as mock_new_socket,
511+
patch.object(_engine, "new_socket", return_value=new_listen_sock),
518512
patch.object(_engine, "add_multicast_member", return_value=True) as mock_add,
519513
patch.object(_engine, "new_respond_socket", return_value=Mock()),
520514
patch.object(_engine, "drop_multicast_member"),
521515
patch.object(_engine.AsyncEngine, "_async_wrap_socket", new=AsyncMock(side_effect=fake_wrap)),
522516
):
523517
added = await engine.async_update_interfaces(["unused"], IPVersion.All, False)
524518

525-
# Rebuilt exactly once, the v6 sender added, the listen transport swapped,
526-
mock_new_socket.assert_called_once()
527519
assert added is True
528-
assert engine._listen_transport is not old_listen
529-
# the kept IPv4 interface was re-joined on the new listen socket,
520+
# The kept IPv4 interface was re-joined on the new listen socket.
530521
assert any(
531522
call.args[0] is new_listen_sock and call.args[1] == "127.0.0.1" for call in mock_add.call_args_list
532523
)
533-
# and the old listen socket closed and removed (no duplicate left behind).
534-
assert old_underlying.is_closing()
535-
assert old_listen not in engine.readers
536524

537525

538526
@pytest.mark.asyncio

0 commit comments

Comments
 (0)