mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-01-19 11:16:24 +00:00
Factor some repetition out of the tests
This commit is contained in:
parent
8e63fe2fdd
commit
6d4278b465
@ -2,21 +2,43 @@
|
|||||||
Tests for allmydata.util.connection_status.
|
Tests for allmydata.util.connection_status.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from twisted.trial import unittest
|
from typing import Optional
|
||||||
|
|
||||||
from foolscap.reconnector import ReconnectionInfo, Reconnector
|
from foolscap.reconnector import ReconnectionInfo, Reconnector
|
||||||
from foolscap.info import ConnectionInfo
|
from foolscap.info import ConnectionInfo
|
||||||
|
|
||||||
from ..util import connection_status
|
from ..util import connection_status
|
||||||
|
from .common import SyncTestCase
|
||||||
|
|
||||||
def reconnector(info: ReconnectionInfo) -> Reconnector:
|
def reconnector(info: ReconnectionInfo) -> Reconnector:
|
||||||
rc = Reconnector(None, None, (), {})
|
rc = Reconnector(None, None, (), {}) # type: ignore[no-untyped-call]
|
||||||
rc._reconnectionInfo = info
|
rc._reconnectionInfo = info
|
||||||
return rc
|
return rc
|
||||||
|
|
||||||
|
def connection_info(
|
||||||
|
statuses: dict[str, str],
|
||||||
|
handlers: dict[str, str],
|
||||||
|
winningHint: Optional[str],
|
||||||
|
establishedAt: Optional[int],
|
||||||
|
) -> ConnectionInfo:
|
||||||
|
ci = ConnectionInfo() # type: ignore[no-untyped-call]
|
||||||
|
ci.connectorStatuses = statuses
|
||||||
|
ci.connectionHandlers = handlers
|
||||||
|
ci.winningHint = winningHint
|
||||||
|
ci.establishedAt = establishedAt
|
||||||
|
return ci
|
||||||
|
|
||||||
class Status(unittest.TestCase):
|
def reconnection_info(
|
||||||
def test_hint_statuses(self):
|
state: str,
|
||||||
|
connection_info: ConnectionInfo,
|
||||||
|
) -> ReconnectionInfo:
|
||||||
|
ri = ReconnectionInfo() # type: ignore[no-untyped-call]
|
||||||
|
ri.state = state
|
||||||
|
ri.connectionInfo = connection_info
|
||||||
|
return ri
|
||||||
|
|
||||||
|
class Status(SyncTestCase):
|
||||||
|
def test_hint_statuses(self) -> None:
|
||||||
ncs = connection_status._hint_statuses(["h2","h1"],
|
ncs = connection_status._hint_statuses(["h2","h1"],
|
||||||
{"h1": "hand1", "h4": "hand4"},
|
{"h1": "hand1", "h4": "hand4"},
|
||||||
{"h1": "st1", "h2": "st2",
|
{"h1": "st1", "h2": "st2",
|
||||||
@ -24,15 +46,9 @@ class Status(unittest.TestCase):
|
|||||||
self.assertEqual(ncs, {"h1 via hand1": "st1",
|
self.assertEqual(ncs, {"h1 via hand1": "st1",
|
||||||
"h2": "st2"})
|
"h2": "st2"})
|
||||||
|
|
||||||
def test_reconnector_connected(self):
|
def test_reconnector_connected(self) -> None:
|
||||||
ci = ConnectionInfo()
|
ci = connection_info({"h1": "st1"}, {"h1": "hand1"}, "h1", 120)
|
||||||
ci.connectorStatuses = {"h1": "st1"}
|
ri = reconnection_info("connected", ci)
|
||||||
ci.connectionHandlers = {"h1": "hand1"}
|
|
||||||
ci.winningHint = "h1"
|
|
||||||
ci.establishedAt = 120
|
|
||||||
ri = ReconnectionInfo()
|
|
||||||
ri.state = "connected"
|
|
||||||
ri.connectionInfo = ci
|
|
||||||
rc = reconnector(ri)
|
rc = reconnector(ri)
|
||||||
cs = connection_status.from_foolscap_reconnector(rc, 123)
|
cs = connection_status.from_foolscap_reconnector(rc, 123)
|
||||||
self.assertEqual(cs.connected, True)
|
self.assertEqual(cs.connected, True)
|
||||||
@ -41,15 +57,9 @@ class Status(unittest.TestCase):
|
|||||||
self.assertEqual(cs.last_connection_time, 120)
|
self.assertEqual(cs.last_connection_time, 120)
|
||||||
self.assertEqual(cs.last_received_time, 123)
|
self.assertEqual(cs.last_received_time, 123)
|
||||||
|
|
||||||
def test_reconnector_connected_others(self):
|
def test_reconnector_connected_others(self) -> None:
|
||||||
ci = ConnectionInfo()
|
ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, "h1", 120)
|
||||||
ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
|
ri = reconnection_info("connected", ci)
|
||||||
ci.connectionHandlers = {"h1": "hand1"}
|
|
||||||
ci.winningHint = "h1"
|
|
||||||
ci.establishedAt = 120
|
|
||||||
ri = ReconnectionInfo()
|
|
||||||
ri.state = "connected"
|
|
||||||
ri.connectionInfo = ci
|
|
||||||
rc = reconnector(ri)
|
rc = reconnector(ri)
|
||||||
cs = connection_status.from_foolscap_reconnector(rc, 123)
|
cs = connection_status.from_foolscap_reconnector(rc, 123)
|
||||||
self.assertEqual(cs.connected, True)
|
self.assertEqual(cs.connected, True)
|
||||||
@ -58,16 +68,10 @@ class Status(unittest.TestCase):
|
|||||||
self.assertEqual(cs.last_connection_time, 120)
|
self.assertEqual(cs.last_connection_time, 120)
|
||||||
self.assertEqual(cs.last_received_time, 123)
|
self.assertEqual(cs.last_received_time, 123)
|
||||||
|
|
||||||
def test_reconnector_connected_listener(self):
|
def test_reconnector_connected_listener(self) -> None:
|
||||||
ci = ConnectionInfo()
|
ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, None, 120)
|
||||||
ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
|
|
||||||
ci.connectionHandlers = {"h1": "hand1"}
|
|
||||||
ci.listenerStatus = ("listener1", "successful")
|
ci.listenerStatus = ("listener1", "successful")
|
||||||
ci.winningHint = None
|
ri = reconnection_info("connected", ci)
|
||||||
ci.establishedAt = 120
|
|
||||||
ri = ReconnectionInfo()
|
|
||||||
ri.state = "connected"
|
|
||||||
ri.connectionInfo = ci
|
|
||||||
rc = reconnector(ri)
|
rc = reconnector(ri)
|
||||||
cs = connection_status.from_foolscap_reconnector(rc, 123)
|
cs = connection_status.from_foolscap_reconnector(rc, 123)
|
||||||
self.assertEqual(cs.connected, True)
|
self.assertEqual(cs.connected, True)
|
||||||
@ -77,13 +81,9 @@ class Status(unittest.TestCase):
|
|||||||
self.assertEqual(cs.last_connection_time, 120)
|
self.assertEqual(cs.last_connection_time, 120)
|
||||||
self.assertEqual(cs.last_received_time, 123)
|
self.assertEqual(cs.last_received_time, 123)
|
||||||
|
|
||||||
def test_reconnector_connecting(self):
|
def test_reconnector_connecting(self) -> None:
|
||||||
ci = ConnectionInfo()
|
ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, None, None)
|
||||||
ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
|
ri = reconnection_info("connecting", ci)
|
||||||
ci.connectionHandlers = {"h1": "hand1"}
|
|
||||||
ri = ReconnectionInfo()
|
|
||||||
ri.state = "connecting"
|
|
||||||
ri.connectionInfo = ci
|
|
||||||
rc = reconnector(ri)
|
rc = reconnector(ri)
|
||||||
cs = connection_status.from_foolscap_reconnector(rc, 123)
|
cs = connection_status.from_foolscap_reconnector(rc, 123)
|
||||||
self.assertEqual(cs.connected, False)
|
self.assertEqual(cs.connected, False)
|
||||||
@ -93,15 +93,11 @@ class Status(unittest.TestCase):
|
|||||||
self.assertEqual(cs.last_connection_time, None)
|
self.assertEqual(cs.last_connection_time, None)
|
||||||
self.assertEqual(cs.last_received_time, 123)
|
self.assertEqual(cs.last_received_time, 123)
|
||||||
|
|
||||||
def test_reconnector_waiting(self):
|
def test_reconnector_waiting(self) -> None:
|
||||||
ci = ConnectionInfo()
|
ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, None, None)
|
||||||
ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
|
ri = reconnection_info("waiting", ci)
|
||||||
ci.connectionHandlers = {"h1": "hand1"}
|
|
||||||
ri = ReconnectionInfo()
|
|
||||||
ri.state = "waiting"
|
|
||||||
ri.lastAttempt = 10
|
ri.lastAttempt = 10
|
||||||
ri.nextAttempt = 20
|
ri.nextAttempt = 20
|
||||||
ri.connectionInfo = ci
|
|
||||||
rc = reconnector(ri)
|
rc = reconnector(ri)
|
||||||
cs = connection_status.from_foolscap_reconnector(rc, 5, time=lambda: 12)
|
cs = connection_status.from_foolscap_reconnector(rc, 5, time=lambda: 12)
|
||||||
self.assertEqual(cs.connected, False)
|
self.assertEqual(cs.connected, False)
|
||||||
|
@ -31,7 +31,7 @@ class ConnectionStatus(object):
|
|||||||
last_received_time=None,
|
last_received_time=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
def _hint_statuses(which, handlers, statuses):
|
def _hint_statuses(which, handlers, statuses) -> dict[str, str]:
|
||||||
non_connected_statuses = {}
|
non_connected_statuses = {}
|
||||||
for hint in which:
|
for hint in which:
|
||||||
handler = handlers.get(hint)
|
handler = handlers.get(hint)
|
||||||
|
Loading…
Reference in New Issue
Block a user