def setUp(self): """Initialize this test instance.""" yield super(PortDetectFactoryTestCase, self).setUp() self.factory = PortDetectFactory()
class PortDetectFactoryTestCase(TestCase): """Tests for the PortDetectFactory.""" timeout = 2 @defer.inlineCallbacks def setUp(self): """Initialize this test instance.""" yield super(PortDetectFactoryTestCase, self).setUp() self.factory = PortDetectFactory() @defer.inlineCallbacks def test_is_listening(self): """Test that the deferred returns True when something is listening.""" addr = (tcpactivation.LOCALHOST, SAMPLE_CLIENT_DESCRIPTION) self.factory.buildProtocol(addr) is_listening = yield self.factory.is_listening() self.assertTrue(is_listening) @defer.inlineCallbacks def test_connection_lost(self): """Test that the deferred returns False when the connection is lost.""" self.factory.clientConnectionLost(None, "test reason") is_listening = yield self.factory.is_listening() self.assertFalse(is_listening) @defer.inlineCallbacks def test_connection_failed(self): """Test that the deferred returns False when the connection fails.""" self.factory.clientConnectionFailed(None, "test reason") is_listening = yield self.factory.is_listening() self.assertFalse(is_listening) @defer.inlineCallbacks def test_connection_failed_then_lost(self): """It's not an error if two events happen.""" self.factory.clientConnectionFailed(None, "test reason") self.factory.clientConnectionLost(None, "test reason") is_listening = yield self.factory.is_listening() self.assertFalse(is_listening) @defer.inlineCallbacks def test_connection_works_then_lost(self): """It's not an error if two events happen.""" addr = (tcpactivation.LOCALHOST, SAMPLE_CLIENT_DESCRIPTION) self.factory.buildProtocol(addr) d = self.factory.is_listening() self.factory.clientConnectionLost(None, "test reason") is_listening = yield d self.assertTrue(is_listening)