コード例 #1
0
ファイル: test_beta.py プロジェクト: MartysTardis/bravo
class TestBetaServerProtocol(TestCase):

    def setUp(self):
        self.p = BetaServerProtocol()
        self.p.factory = FakeFactory()
        self.p.transport = FakeTransport()

    def tearDown(self):
        # Stop the connection timeout.
        self.p.setTimeout(None)

    def test_trivial(self):
        pass

    def test_health_initial(self):
        """
        The client's health should start at 20.
        """

        self.assertEqual(self.p.health, 20)

    def test_health_invalid(self):
        """
        An error is raised when an invalid value is assigned for health.
        """

        self.assertRaises(BetaClientError, setattr, self.p, "health", -1)
        self.assertRaises(BetaClientError, setattr, self.p, "health", 21)

    def test_health_update(self):
        """
        The protocol should emit a health update when its health changes.
        """

        self.p.transport.data = []
        self.p.health = 19
        self.assertEqual(len(self.p.transport.data), 1)
        self.assertTrue(self.p.transport.data[0].startswith("\x08"))

    def test_health_no_change(self):
        """
        If health is assigned to but not changed, no health update should be
        issued.
        """

        self.p.transport.data = []
        self.p.health = 20
        self.assertFalse(self.p.transport.data)

    def test_connection_timeout(self):
        """
        Connections should time out after 30 seconds.
        """

        def cb():
            self.assertTrue(self.p.transport.lost)

        d = deferLater(reactor, 31, cb)
        return d

    def test_latency_overflow(self):
        """
        Massive latencies should not cause exceptions to be raised.
        """

        # Set the username to avoid a packet generation problem.
        self.p.username = "******"

        # Turn on warning context and warning->error filter; otherwise, only a
        # warning will be emitted on Python 2.6 and older, and we want the
        # test to always fail in that case.
        with warnings.catch_warnings():
            warnings.simplefilter("error")
            self.p.latency = 70000
コード例 #2
0
class TestBetaServerProtocol(TestCase):
    def setUp(self):
        self.p = BetaServerProtocol()
        self.p.factory = FakeFactory()
        self.p.transport = FakeTransport()

    def tearDown(self):
        # Stop the connection timeout.
        self.p.setTimeout(None)

    def test_trivial(self):
        pass

    def test_health_initial(self):
        """
        The client's health should start at 20.
        """

        self.assertEqual(self.p.health, 20)

    def test_health_invalid(self):
        """
        An error is raised when an invalid value is assigned for health.
        """

        self.assertRaises(BetaClientError, setattr, self.p, "health", -1)
        self.assertRaises(BetaClientError, setattr, self.p, "health", 21)

    def test_health_update(self):
        """
        The protocol should emit a health update when its health changes.
        """

        self.p.transport.data = []
        self.p.health = 19
        self.assertEqual(len(self.p.transport.data), 1)
        self.assertTrue(self.p.transport.data[0].startswith("\x08"))

    def test_health_no_change(self):
        """
        If health is assigned to but not changed, no health update should be
        issued.
        """

        self.p.transport.data = []
        self.p.health = 20
        self.assertFalse(self.p.transport.data)

    def test_connection_timeout(self):
        """
        Connections should time out after 30 seconds.
        """
        def cb():
            self.assertTrue(self.p.transport.lost)

        d = deferLater(reactor, 31, cb)
        return d

    def test_latency_overflow(self):
        """
        Massive latencies should not cause exceptions to be raised.
        """

        # Set the username to avoid a packet generation problem.
        self.p.username = "******"

        # Turn on warning context and warning->error filter; otherwise, only a
        # warning will be emitted on Python 2.6 and older, and we want the
        # test to always fail in that case.
        with warnings.catch_warnings():
            warnings.simplefilter("error")
            self.p.latency = 70000