Skip to content

Commit 9adf9aa

Browse files
committed
refactor: improve test structure in mDNS tests
1 parent dcc8bbb commit 9adf9aa

File tree

5 files changed

+89
-244
lines changed

5 files changed

+89
-244
lines changed

tests/discovery/mdns/test_broadcaster.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
"""
22
Unit tests for mDNS broadcaster component.
33
"""
4-
import socket
5-
import pytest
6-
from zeroconf import ServiceInfo, Zeroconf
4+
5+
from zeroconf import Zeroconf
76

87
from libp2p.discovery.mdns.broadcaster import PeerBroadcaster
98
from libp2p.peer.id import ID
109

1110

1211
class TestPeerBroadcaster:
13-
"""Basic unit tests for PeerBroadcaster."""
12+
"""Unit tests for PeerBroadcaster."""
1413

1514
def test_broadcaster_initialization(self):
1615
"""Test that broadcaster initializes correctly."""
1716
zeroconf = Zeroconf()
1817
service_type = "_p2p._udp.local."
1918
service_name = "test-peer._p2p._udp.local."
20-
peer_id = "QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN" # String, not ID object
19+
peer_id = (
20+
"QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN" # String, not ID object
21+
)
2122
port = 8000
2223

2324
broadcaster = PeerBroadcaster(
2425
zeroconf=zeroconf,
2526
service_type=service_type,
2627
service_name=service_name,
2728
peer_id=peer_id,
28-
port=port
29+
port=port,
2930
)
3031

3132
assert broadcaster.zeroconf == zeroconf
3233
assert broadcaster.service_type == service_type
3334
assert broadcaster.service_name == service_name
3435
assert broadcaster.peer_id == peer_id
3536
assert broadcaster.port == port
36-
37+
3738
# Clean up
3839
zeroconf.close()
3940

@@ -51,7 +52,7 @@ def test_broadcaster_service_creation(self):
5152
service_type=service_type,
5253
service_name=service_name,
5354
peer_id=peer_id,
54-
port=port
55+
port=port,
5556
)
5657

5758
# Verify service was created and registered
@@ -62,7 +63,7 @@ def test_broadcaster_service_creation(self):
6263
assert service_info.port == port
6364
assert b"id" in service_info.properties
6465
assert service_info.properties[b"id"] == peer_id.encode()
65-
66+
6667
# Clean up
6768
zeroconf.close()
6869

@@ -80,11 +81,11 @@ def test_broadcaster_start_stop(self):
8081
service_type=service_type,
8182
service_name=service_name,
8283
peer_id=peer_id,
83-
port=port
84+
port=port,
8485
)
8586

8687
# Service should be registered
8788
assert broadcaster.service_info is not None
88-
89+
8990
# Clean up
9091
zeroconf.close()

tests/discovery/mdns/test_integration.py

Lines changed: 0 additions & 128 deletions
This file was deleted.

tests/discovery/mdns/test_listener.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
"""
22
Unit tests for mDNS listener component.
33
"""
4+
45
import socket
5-
import pytest
6+
67
from zeroconf import ServiceInfo, Zeroconf
78

9+
from libp2p.abc import Multiaddr
810
from libp2p.discovery.mdns.listener import PeerListener
911
from libp2p.peer.id import ID
1012
from libp2p.peer.peerstore import PeerStore
11-
from libp2p.abc import Multiaddr
1213

1314

1415
class TestPeerListener:
15-
"""Basic unit tests for PeerListener."""
16+
"""Unit tests for PeerListener."""
1617

1718
def test_listener_initialization(self):
1819
"""Test that listener initializes correctly."""
@@ -33,7 +34,7 @@ def test_listener_initialization(self):
3334
assert listener.service_type == service_type
3435
assert listener.service_name == service_name
3536
assert listener.discovered_services == {}
36-
37+
3738
# Clean up
3839
listener.stop()
3940
zeroconf.close()
@@ -42,7 +43,7 @@ def test_listener_extract_peer_info_success(self):
4243
"""Test successful PeerInfo extraction from ServiceInfo."""
4344
peerstore = PeerStore()
4445
zeroconf = Zeroconf()
45-
46+
4647
listener = PeerListener(
4748
peerstore=peerstore,
4849
zeroconf=zeroconf,
@@ -51,10 +52,12 @@ def test_listener_extract_peer_info_success(self):
5152
)
5253

5354
# Create sample service info
54-
sample_peer_id = ID.from_base58("QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN")
55+
sample_peer_id = ID.from_base58(
56+
"QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN"
57+
)
5558
hostname = socket.gethostname()
5659
local_ip = "192.168.1.100"
57-
60+
5861
sample_service_info = ServiceInfo(
5962
type_="_p2p._udp.local.",
6063
name="test-peer._p2p._udp.local.",
@@ -70,10 +73,10 @@ def test_listener_extract_peer_info_success(self):
7073
assert isinstance(peer_info.peer_id, ID)
7174
assert len(peer_info.addrs) > 0
7275
assert all(isinstance(addr, Multiaddr) for addr in peer_info.addrs)
73-
76+
7477
# Check that protocol is TCP since we always use TCP
7578
assert "/tcp/" in str(peer_info.addrs[0])
76-
79+
7780
# Clean up
7881
listener.stop()
7982
zeroconf.close()
@@ -82,7 +85,7 @@ def test_listener_extract_peer_info_invalid_id(self):
8285
"""Test PeerInfo extraction fails with invalid peer ID."""
8386
peerstore = PeerStore()
8487
zeroconf = Zeroconf()
85-
88+
8689
listener = PeerListener(
8790
peerstore=peerstore,
8891
zeroconf=zeroconf,
@@ -93,7 +96,7 @@ def test_listener_extract_peer_info_invalid_id(self):
9396
# Create service info with invalid peer ID
9497
hostname = socket.gethostname()
9598
local_ip = "192.168.1.100"
96-
99+
97100
service_info = ServiceInfo(
98101
type_="_p2p._udp.local.",
99102
name="invalid-peer._p2p._udp.local.",
@@ -105,7 +108,7 @@ def test_listener_extract_peer_info_invalid_id(self):
105108

106109
peer_info = listener._extract_peer_info(service_info)
107110
assert peer_info is None
108-
111+
109112
# Clean up
110113
listener.stop()
111114
zeroconf.close()

0 commit comments

Comments
 (0)