1
1
"""
2
2
Unit tests for mDNS listener component.
3
3
"""
4
+
4
5
import socket
5
- import pytest
6
+
6
7
from zeroconf import ServiceInfo , Zeroconf
7
8
9
+ from libp2p .abc import Multiaddr
8
10
from libp2p .discovery .mdns .listener import PeerListener
9
11
from libp2p .peer .id import ID
10
12
from libp2p .peer .peerstore import PeerStore
11
- from libp2p .abc import Multiaddr
12
13
13
14
14
15
class TestPeerListener :
15
- """Basic unit tests for PeerListener."""
16
+ """Unit tests for PeerListener."""
16
17
17
18
def test_listener_initialization (self ):
18
19
"""Test that listener initializes correctly."""
@@ -33,7 +34,7 @@ def test_listener_initialization(self):
33
34
assert listener .service_type == service_type
34
35
assert listener .service_name == service_name
35
36
assert listener .discovered_services == {}
36
-
37
+
37
38
# Clean up
38
39
listener .stop ()
39
40
zeroconf .close ()
@@ -42,7 +43,7 @@ def test_listener_extract_peer_info_success(self):
42
43
"""Test successful PeerInfo extraction from ServiceInfo."""
43
44
peerstore = PeerStore ()
44
45
zeroconf = Zeroconf ()
45
-
46
+
46
47
listener = PeerListener (
47
48
peerstore = peerstore ,
48
49
zeroconf = zeroconf ,
@@ -51,10 +52,12 @@ def test_listener_extract_peer_info_success(self):
51
52
)
52
53
53
54
# Create sample service info
54
- sample_peer_id = ID .from_base58 ("QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN" )
55
+ sample_peer_id = ID .from_base58 (
56
+ "QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN"
57
+ )
55
58
hostname = socket .gethostname ()
56
59
local_ip = "192.168.1.100"
57
-
60
+
58
61
sample_service_info = ServiceInfo (
59
62
type_ = "_p2p._udp.local." ,
60
63
name = "test-peer._p2p._udp.local." ,
@@ -70,10 +73,10 @@ def test_listener_extract_peer_info_success(self):
70
73
assert isinstance (peer_info .peer_id , ID )
71
74
assert len (peer_info .addrs ) > 0
72
75
assert all (isinstance (addr , Multiaddr ) for addr in peer_info .addrs )
73
-
76
+
74
77
# Check that protocol is TCP since we always use TCP
75
78
assert "/tcp/" in str (peer_info .addrs [0 ])
76
-
79
+
77
80
# Clean up
78
81
listener .stop ()
79
82
zeroconf .close ()
@@ -82,7 +85,7 @@ def test_listener_extract_peer_info_invalid_id(self):
82
85
"""Test PeerInfo extraction fails with invalid peer ID."""
83
86
peerstore = PeerStore ()
84
87
zeroconf = Zeroconf ()
85
-
88
+
86
89
listener = PeerListener (
87
90
peerstore = peerstore ,
88
91
zeroconf = zeroconf ,
@@ -93,7 +96,7 @@ def test_listener_extract_peer_info_invalid_id(self):
93
96
# Create service info with invalid peer ID
94
97
hostname = socket .gethostname ()
95
98
local_ip = "192.168.1.100"
96
-
99
+
97
100
service_info = ServiceInfo (
98
101
type_ = "_p2p._udp.local." ,
99
102
name = "invalid-peer._p2p._udp.local." ,
@@ -105,7 +108,7 @@ def test_listener_extract_peer_info_invalid_id(self):
105
108
106
109
peer_info = listener ._extract_peer_info (service_info )
107
110
assert peer_info is None
108
-
111
+
109
112
# Clean up
110
113
listener .stop ()
111
114
zeroconf .close ()
0 commit comments