Changeset 8b35fe9 in trunk


Ignore:
Timestamp:
2016-12-09T19:46:26Z (8 years ago)
Author:
Brian Warner <warner@…>
Branches:
master
Children:
74209e64, 7d9b76f4
Parents:
70a6168
Message:

tests: exercise connection_status.py

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/allmydata/test/test_connections.py

    r70a6168 r8b35fe9  
    88from foolscap.connections import tcp
    99from ..node import Node, PrivacyError
     10from ..util import connection_status
    1011
    1112class FakeNode(Node):
     
    344345        self.assertEqual(str(e), "tub.location includes tcp: hint")
    345346
    346 
     347class Status(unittest.TestCase):
     348    def test_describe(self):
     349        t = connection_status._describe_statuses(["h2","h1"],
     350                                                 {"h1": "hand1"},
     351                                                 {"h1": "st1", "h2": "st2"})
     352        self.assertEqual(t, " h1 via hand1: st1\n h2: st2\n")
     353
     354    def test_reconnector_connected(self):
     355        ci = mock.Mock()
     356        ci.connectorStatuses = {"h1": "st1"}
     357        ci.connectionHandlers = {"h1": "hand1"}
     358        ci.winningHint = "h1"
     359        ci.establishedAt = 120
     360        ri = mock.Mock()
     361        ri.state = "connected"
     362        ri.connectionInfo = ci
     363        rc = mock.Mock
     364        rc.getReconnectionInfo = mock.Mock(return_value=ri)
     365        cs = connection_status.from_foolscap_reconnector(rc, 123)
     366        self.assertEqual(cs.connected, True)
     367        self.assertEqual(cs.last_connection_summary,
     368                         "Connected to h1 via hand1")
     369        self.assertEqual(cs.last_connection_description,
     370                         "Connection successful to h1 via hand1")
     371        self.assertEqual(cs.last_connection_time, 120)
     372        self.assertEqual(cs.last_received_time, 123)
     373
     374    def test_reconnector_connected_others(self):
     375        ci = mock.Mock()
     376        ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
     377        ci.connectionHandlers = {"h1": "hand1"}
     378        ci.winningHint = "h1"
     379        ci.establishedAt = 120
     380        ri = mock.Mock()
     381        ri.state = "connected"
     382        ri.connectionInfo = ci
     383        rc = mock.Mock
     384        rc.getReconnectionInfo = mock.Mock(return_value=ri)
     385        cs = connection_status.from_foolscap_reconnector(rc, 123)
     386        self.assertEqual(cs.connected, True)
     387        self.assertEqual(cs.last_connection_summary,
     388                         "Connected to h1 via hand1")
     389        self.assertEqual(cs.last_connection_description,
     390                         "Connection successful to h1 via hand1\n"
     391                         "other hints:\n"
     392                         " h2: st2\n")
     393        self.assertEqual(cs.last_connection_time, 120)
     394        self.assertEqual(cs.last_received_time, 123)
     395
     396    def test_reconnector_connected_listener(self):
     397        ci = mock.Mock()
     398        ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
     399        ci.connectionHandlers = {"h1": "hand1"}
     400        ci.listenerStatus = ("listener1", "successful")
     401        ci.winningHint = None
     402        ci.establishedAt = 120
     403        ri = mock.Mock()
     404        ri.state = "connected"
     405        ri.connectionInfo = ci
     406        rc = mock.Mock
     407        rc.getReconnectionInfo = mock.Mock(return_value=ri)
     408        cs = connection_status.from_foolscap_reconnector(rc, 123)
     409        self.assertEqual(cs.connected, True)
     410        self.assertEqual(cs.last_connection_summary,
     411                         "Connected via listener (listener1)")
     412        self.assertEqual(cs.last_connection_description,
     413                         "Connection successful via listener (listener1)\n"
     414                         "other hints:\n"
     415                         " h1 via hand1: st1\n"
     416                         " h2: st2\n")
     417        self.assertEqual(cs.last_connection_time, 120)
     418        self.assertEqual(cs.last_received_time, 123)
     419
     420    def test_reconnector_connecting(self):
     421        ci = mock.Mock()
     422        ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
     423        ci.connectionHandlers = {"h1": "hand1"}
     424        ri = mock.Mock()
     425        ri.state = "connecting"
     426        ri.connectionInfo = ci
     427        rc = mock.Mock
     428        rc.getReconnectionInfo = mock.Mock(return_value=ri)
     429        cs = connection_status.from_foolscap_reconnector(rc, 123)
     430        self.assertEqual(cs.connected, False)
     431        self.assertEqual(cs.last_connection_summary,
     432                         "Trying to connect")
     433        self.assertEqual(cs.last_connection_description,
     434                         "Trying to connect:\n"
     435                         " h1 via hand1: st1\n"
     436                         " h2: st2\n")
     437        self.assertEqual(cs.last_connection_time, None)
     438        self.assertEqual(cs.last_received_time, 123)
     439
     440    def test_reconnector_waiting(self):
     441        ci = mock.Mock()
     442        ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
     443        ci.connectionHandlers = {"h1": "hand1"}
     444        ri = mock.Mock()
     445        ri.state = "waiting"
     446        ri.lastAttempt = 10
     447        ri.nextAttempt = 20
     448        ri.connectionInfo = ci
     449        rc = mock.Mock
     450        rc.getReconnectionInfo = mock.Mock(return_value=ri)
     451        with mock.patch("time.time", return_value=12):
     452            cs = connection_status.from_foolscap_reconnector(rc, 5)
     453        self.assertEqual(cs.connected, False)
     454        self.assertEqual(cs.last_connection_summary,
     455                         "Reconnecting in 8 seconds")
     456        self.assertEqual(cs.last_connection_description,
     457                         "Reconnecting in 8 seconds\n"
     458                         "Last attempt 2s ago:\n"
     459                         " h1 via hand1: st1\n"
     460                         " h2: st2\n")
     461        self.assertEqual(cs.last_connection_time, None)
     462        self.assertEqual(cs.last_received_time, 5)
     463
Note: See TracChangeset for help on using the changeset viewer.