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.
|
||||
"""
|
||||
|
||||
from twisted.trial import unittest
|
||||
from typing import Optional
|
||||
|
||||
from foolscap.reconnector import ReconnectionInfo, Reconnector
|
||||
from foolscap.info import ConnectionInfo
|
||||
|
||||
from ..util import connection_status
|
||||
from .common import SyncTestCase
|
||||
|
||||
def reconnector(info: ReconnectionInfo) -> Reconnector:
|
||||
rc = Reconnector(None, None, (), {})
|
||||
rc = Reconnector(None, None, (), {}) # type: ignore[no-untyped-call]
|
||||
rc._reconnectionInfo = info
|
||||
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 test_hint_statuses(self):
|
||||
def reconnection_info(
|
||||
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"],
|
||||
{"h1": "hand1", "h4": "hand4"},
|
||||
{"h1": "st1", "h2": "st2",
|
||||
@ -24,15 +46,9 @@ class Status(unittest.TestCase):
|
||||
self.assertEqual(ncs, {"h1 via hand1": "st1",
|
||||
"h2": "st2"})
|
||||
|
||||
def test_reconnector_connected(self):
|
||||
ci = ConnectionInfo()
|
||||
ci.connectorStatuses = {"h1": "st1"}
|
||||
ci.connectionHandlers = {"h1": "hand1"}
|
||||
ci.winningHint = "h1"
|
||||
ci.establishedAt = 120
|
||||
ri = ReconnectionInfo()
|
||||
ri.state = "connected"
|
||||
ri.connectionInfo = ci
|
||||
def test_reconnector_connected(self) -> None:
|
||||
ci = connection_info({"h1": "st1"}, {"h1": "hand1"}, "h1", 120)
|
||||
ri = reconnection_info("connected", ci)
|
||||
rc = reconnector(ri)
|
||||
cs = connection_status.from_foolscap_reconnector(rc, 123)
|
||||
self.assertEqual(cs.connected, True)
|
||||
@ -41,15 +57,9 @@ class Status(unittest.TestCase):
|
||||
self.assertEqual(cs.last_connection_time, 120)
|
||||
self.assertEqual(cs.last_received_time, 123)
|
||||
|
||||
def test_reconnector_connected_others(self):
|
||||
ci = ConnectionInfo()
|
||||
ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
|
||||
ci.connectionHandlers = {"h1": "hand1"}
|
||||
ci.winningHint = "h1"
|
||||
ci.establishedAt = 120
|
||||
ri = ReconnectionInfo()
|
||||
ri.state = "connected"
|
||||
ri.connectionInfo = ci
|
||||
def test_reconnector_connected_others(self) -> None:
|
||||
ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, "h1", 120)
|
||||
ri = reconnection_info("connected", ci)
|
||||
rc = reconnector(ri)
|
||||
cs = connection_status.from_foolscap_reconnector(rc, 123)
|
||||
self.assertEqual(cs.connected, True)
|
||||
@ -58,16 +68,10 @@ class Status(unittest.TestCase):
|
||||
self.assertEqual(cs.last_connection_time, 120)
|
||||
self.assertEqual(cs.last_received_time, 123)
|
||||
|
||||
def test_reconnector_connected_listener(self):
|
||||
ci = ConnectionInfo()
|
||||
ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
|
||||
ci.connectionHandlers = {"h1": "hand1"}
|
||||
def test_reconnector_connected_listener(self) -> None:
|
||||
ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, None, 120)
|
||||
ci.listenerStatus = ("listener1", "successful")
|
||||
ci.winningHint = None
|
||||
ci.establishedAt = 120
|
||||
ri = ReconnectionInfo()
|
||||
ri.state = "connected"
|
||||
ri.connectionInfo = ci
|
||||
ri = reconnection_info("connected", ci)
|
||||
rc = reconnector(ri)
|
||||
cs = connection_status.from_foolscap_reconnector(rc, 123)
|
||||
self.assertEqual(cs.connected, True)
|
||||
@ -77,13 +81,9 @@ class Status(unittest.TestCase):
|
||||
self.assertEqual(cs.last_connection_time, 120)
|
||||
self.assertEqual(cs.last_received_time, 123)
|
||||
|
||||
def test_reconnector_connecting(self):
|
||||
ci = ConnectionInfo()
|
||||
ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
|
||||
ci.connectionHandlers = {"h1": "hand1"}
|
||||
ri = ReconnectionInfo()
|
||||
ri.state = "connecting"
|
||||
ri.connectionInfo = ci
|
||||
def test_reconnector_connecting(self) -> None:
|
||||
ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, None, None)
|
||||
ri = reconnection_info("connecting", ci)
|
||||
rc = reconnector(ri)
|
||||
cs = connection_status.from_foolscap_reconnector(rc, 123)
|
||||
self.assertEqual(cs.connected, False)
|
||||
@ -93,15 +93,11 @@ class Status(unittest.TestCase):
|
||||
self.assertEqual(cs.last_connection_time, None)
|
||||
self.assertEqual(cs.last_received_time, 123)
|
||||
|
||||
def test_reconnector_waiting(self):
|
||||
ci = ConnectionInfo()
|
||||
ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
|
||||
ci.connectionHandlers = {"h1": "hand1"}
|
||||
ri = ReconnectionInfo()
|
||||
ri.state = "waiting"
|
||||
def test_reconnector_waiting(self) -> None:
|
||||
ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, None, None)
|
||||
ri = reconnection_info("waiting", ci)
|
||||
ri.lastAttempt = 10
|
||||
ri.nextAttempt = 20
|
||||
ri.connectionInfo = ci
|
||||
rc = reconnector(ri)
|
||||
cs = connection_status.from_foolscap_reconnector(rc, 5, time=lambda: 12)
|
||||
self.assertEqual(cs.connected, False)
|
||||
|
@ -31,7 +31,7 @@ class ConnectionStatus(object):
|
||||
last_received_time=None,
|
||||
)
|
||||
|
||||
def _hint_statuses(which, handlers, statuses):
|
||||
def _hint_statuses(which, handlers, statuses) -> dict[str, str]:
|
||||
non_connected_statuses = {}
|
||||
for hint in which:
|
||||
handler = handlers.get(hint)
|
||||
|
Loading…
Reference in New Issue
Block a user