-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathtest_network.py
More file actions
96 lines (79 loc) · 2.88 KB
/
test_network.py
File metadata and controls
96 lines (79 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""Test network module"""
import threading
import time
from .common import skip_python3
from .partial import TestPartialRun
skip_python3()
class TestNetwork(TestPartialRun):
"""A test case for running the network subsystem"""
@classmethod
def setUpClass(cls):
super(TestNetwork, cls).setUpClass()
cls.state.maximumNumberOfHalfOpenConnections = 4
cls.config.set('bitmessagesettings', 'sendoutgoingconnections', 'True')
cls.config.set('bitmessagesettings', 'udp', 'True')
# config variable is still used inside of the network ):
import network
from network import connectionpool, stats
# beware of singleton
connectionpool.config = cls.config
cls.pool = connectionpool.pool
cls.stats = stats
network.start(cls.config, cls.state)
def test_threads(self):
"""Ensure all the network threads started"""
threads = {
"AddrBroadcaster", "Announcer", "Asyncore", "Downloader",
"InvBroadcaster", "Uploader"}
extra = self.config.getint('threads', 'receive')
for thread in threading.enumerate():
try:
threads.remove(thread.name)
except KeyError:
extra -= thread.name.startswith("ReceiveQueue_")
self.assertEqual(len(threads), 0)
self.assertEqual(extra, 0)
def test_stats(self):
"""Check that network starts connections and updates stats"""
pl = 0
for _ in range(30):
if pl == 0:
pl = len(self.pool)
if (
self.stats.receivedBytes() > 0 and self.stats.sentBytes() > 0
and pl > 0
# and len(self.stats.connectedHostsList()) > 0
):
break
time.sleep(1)
else:
self.fail('Have not started any connection in 30 sec')
def test_udp(self):
"""Invoke AnnounceThread.announceSelf() and check discovered peers"""
for _ in range(20):
if self.pool.udpSockets:
break
time.sleep(1)
else:
self.fail('No UDP sockets found in 20 sec')
for _ in range(10):
try:
self.state.announceThread.announceSelf()
except AttributeError:
self.fail('state.announceThread is not set properly')
time.sleep(1)
try:
peer = self.state.discoveredPeers.popitem()[0]
except KeyError:
continue
else:
self.assertEqual(peer.port, 8444)
break
else:
self.fail('No self in discovered peers')
@classmethod
def tearDownClass(cls):
super(TestNetwork, cls).tearDownClass()
for thread in threading.enumerate():
if thread.name == "Asyncore":
thread.stopThread()