1 | """ |
---|
2 | Tests for allmydata.util.connection_status. |
---|
3 | """ |
---|
4 | |
---|
5 | from __future__ import annotations |
---|
6 | |
---|
7 | from typing import Optional |
---|
8 | |
---|
9 | from foolscap.reconnector import ReconnectionInfo, Reconnector |
---|
10 | from foolscap.info import ConnectionInfo |
---|
11 | |
---|
12 | from ..util import connection_status |
---|
13 | from .common import SyncTestCase |
---|
14 | |
---|
15 | def reconnector(info: ReconnectionInfo) -> Reconnector: |
---|
16 | rc = Reconnector(None, None, (), {}) # type: ignore[no-untyped-call] |
---|
17 | rc._reconnectionInfo = info |
---|
18 | return rc |
---|
19 | |
---|
20 | def connection_info( |
---|
21 | statuses: dict[str, str], |
---|
22 | handlers: dict[str, str], |
---|
23 | winningHint: Optional[str], |
---|
24 | establishedAt: Optional[int], |
---|
25 | ) -> ConnectionInfo: |
---|
26 | ci = ConnectionInfo() # type: ignore[no-untyped-call] |
---|
27 | ci.connectorStatuses = statuses |
---|
28 | ci.connectionHandlers = handlers |
---|
29 | ci.winningHint = winningHint |
---|
30 | ci.establishedAt = establishedAt |
---|
31 | return ci |
---|
32 | |
---|
33 | def reconnection_info( |
---|
34 | state: str, |
---|
35 | connection_info: ConnectionInfo, |
---|
36 | ) -> ReconnectionInfo: |
---|
37 | ri = ReconnectionInfo() # type: ignore[no-untyped-call] |
---|
38 | ri.state = state |
---|
39 | ri.connectionInfo = connection_info |
---|
40 | return ri |
---|
41 | |
---|
42 | class Status(SyncTestCase): |
---|
43 | def test_hint_statuses(self) -> None: |
---|
44 | ncs = connection_status._hint_statuses(["h2","h1"], |
---|
45 | {"h1": "hand1", "h4": "hand4"}, |
---|
46 | {"h1": "st1", "h2": "st2", |
---|
47 | "h3": "st3"}) |
---|
48 | self.assertEqual(ncs, {"h1 via hand1": "st1", |
---|
49 | "h2": "st2"}) |
---|
50 | |
---|
51 | def test_reconnector_connected(self) -> None: |
---|
52 | ci = connection_info({"h1": "st1"}, {"h1": "hand1"}, "h1", 120) |
---|
53 | ri = reconnection_info("connected", ci) |
---|
54 | rc = reconnector(ri) |
---|
55 | cs = connection_status.from_foolscap_reconnector(rc, 123) |
---|
56 | self.assertEqual(cs.connected, True) |
---|
57 | self.assertEqual(cs.summary, "Connected to h1 via hand1") |
---|
58 | self.assertEqual(cs.non_connected_statuses, {}) |
---|
59 | self.assertEqual(cs.last_connection_time, 120) |
---|
60 | self.assertEqual(cs.last_received_time, 123) |
---|
61 | |
---|
62 | def test_reconnector_connected_others(self) -> None: |
---|
63 | ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, "h1", 120) |
---|
64 | ri = reconnection_info("connected", ci) |
---|
65 | rc = reconnector(ri) |
---|
66 | cs = connection_status.from_foolscap_reconnector(rc, 123) |
---|
67 | self.assertEqual(cs.connected, True) |
---|
68 | self.assertEqual(cs.summary, "Connected to h1 via hand1") |
---|
69 | self.assertEqual(cs.non_connected_statuses, {"h2": "st2"}) |
---|
70 | self.assertEqual(cs.last_connection_time, 120) |
---|
71 | self.assertEqual(cs.last_received_time, 123) |
---|
72 | |
---|
73 | def test_reconnector_connected_listener(self) -> None: |
---|
74 | ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, None, 120) |
---|
75 | ci.listenerStatus = ("listener1", "successful") |
---|
76 | ri = reconnection_info("connected", ci) |
---|
77 | rc = reconnector(ri) |
---|
78 | cs = connection_status.from_foolscap_reconnector(rc, 123) |
---|
79 | self.assertEqual(cs.connected, True) |
---|
80 | self.assertEqual(cs.summary, "Connected via listener (listener1)") |
---|
81 | self.assertEqual(cs.non_connected_statuses, |
---|
82 | {"h1 via hand1": "st1", "h2": "st2"}) |
---|
83 | self.assertEqual(cs.last_connection_time, 120) |
---|
84 | self.assertEqual(cs.last_received_time, 123) |
---|
85 | |
---|
86 | def test_reconnector_connecting(self) -> None: |
---|
87 | ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, None, None) |
---|
88 | ri = reconnection_info("connecting", ci) |
---|
89 | rc = reconnector(ri) |
---|
90 | cs = connection_status.from_foolscap_reconnector(rc, 123) |
---|
91 | self.assertEqual(cs.connected, False) |
---|
92 | self.assertEqual(cs.summary, "Trying to connect") |
---|
93 | self.assertEqual(cs.non_connected_statuses, |
---|
94 | {"h1 via hand1": "st1", "h2": "st2"}) |
---|
95 | self.assertEqual(cs.last_connection_time, None) |
---|
96 | self.assertEqual(cs.last_received_time, 123) |
---|
97 | |
---|
98 | def test_reconnector_waiting(self) -> None: |
---|
99 | ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, None, None) |
---|
100 | ri = reconnection_info("waiting", ci) |
---|
101 | ri.lastAttempt = 10 |
---|
102 | ri.nextAttempt = 20 |
---|
103 | rc = reconnector(ri) |
---|
104 | cs = connection_status.from_foolscap_reconnector(rc, 5, time=lambda: 12) |
---|
105 | self.assertEqual(cs.connected, False) |
---|
106 | self.assertEqual(cs.summary, |
---|
107 | "Reconnecting in 8 seconds (last attempt 2s ago)") |
---|
108 | self.assertEqual(cs.non_connected_statuses, |
---|
109 | {"h1 via hand1": "st1", "h2": "st2"}) |
---|
110 | self.assertEqual(cs.last_connection_time, None) |
---|
111 | self.assertEqual(cs.last_received_time, 5) |
---|