Ignore:
Timestamp:
Feb 25, 2012, 8:27:56 PM (10 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Let wokkel.component.Component reconnect if first attempt fails.

Author: ralphm.
Fixes #75.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/test/test_component.py

    r165 r169  
    33
    44"""
    5 Tests for L{wokkel.component}
     5Tests for L{wokkel.component}.
    66"""
    77
    88from zope.interface.verify import verifyObject
    99
     10from twisted.internet.base import BaseConnector
     11from twisted.internet.error import ConnectionRefusedError
     12from twisted.internet.task import Clock
    1013from twisted.python import failure
    1114from twisted.trial import unittest
     
    1821from wokkel import component
    1922from wokkel.generic import XmlPipe
     23
     24class FakeConnector(BaseConnector):
     25    """
     26    Fake connector that counts connection attempts.
     27    """
     28    connects = 0
     29
     30    def connect(self):
     31        self.connects += 1
     32        BaseConnector.connect(self)
     33
     34
     35    def _makeTransport(self):
     36        return None
     37
     38
     39
     40class TestableComponent(component.Component):
     41    """
     42    Testable component.
     43
     44    This component provides the created factory with a L{Clock}
     45    instead of the regular reactor and uses L{FakeConnector} for testing
     46    connects and reconnects.
     47    """
     48
     49    def __init__(self, *args, **kwargs):
     50        component.Component.__init__(self, *args, **kwargs)
     51        self.factory.clock = Clock()
     52
     53
     54    def _getConnection(self):
     55        c = FakeConnector(self.factory, None, None)
     56        c.connect()
     57        return c
     58
     59
     60
     61class ComponentTest(unittest.TestCase):
     62    """
     63    Tests for L{component.Component}.
     64    """
     65    def test_startServiceReconnectAfterFailure(self):
     66        """
     67        When the first connection attempt fails, retry.
     68        """
     69        comp = TestableComponent('example.org', 5347,
     70                                 'test.example.org', 'secret')
     71
     72        # Starting the service initiates a connection attempt.
     73        comp.startService()
     74        connector = comp._connection
     75        self.assertEqual(1, connector.connects)
     76
     77        # Fail the connection.
     78        connector.connectionFailed(ConnectionRefusedError())
     79
     80        # After a back-off delay, a new connection is attempted.
     81        comp.factory.clock.advance(5)
     82        self.assertEqual(2, connector.connects)
     83
     84
     85    def test_stopServiceNoReconnect(self):
     86        """
     87        When the service is stopped, no reconnect is attempted.
     88        """
     89        comp = TestableComponent('example.org', 5347,
     90                                 'test.example.org', 'secret')
     91
     92        # Starting the service initiates a connection attempt.
     93        comp.startService()
     94        connector = comp._connection
     95
     96        # Fail the connection.
     97        connector.connectionFailed(ConnectionRefusedError())
     98
     99        # If the service is stopped before the back-off delay expires,
     100        # no new connection is attempted.
     101        comp.factory.clock.advance(1)
     102        comp.stopService()
     103        comp.factory.clock.advance(4)
     104        self.assertEqual(1, connector.connects)
     105
     106
    20107
    21108class InternalComponentTest(unittest.TestCase):
Note: See TracChangeset for help on using the changeset viewer.