Exemplo n.º 1
0
 def setUp(self):
     self.factory = GatewayClientFactory('pub', __file__)
 def setUp(self):
     self.factory = GatewayClientFactory('pub', __file__)
Exemplo n.º 3
0
class GatewayClientFactoryTestCase(TestCase):
    CLASS = MODULE + 'GatewayClientFactory.'

    @patch(MODULE + 'ssl.PrivateCertificate.loadPEM', Mock())
    @patch(MODULE + 'GatewayClientFactory.ENDPOINTS', {'pub': ('foo', 'bar')})
    def setUp(self):
        self.factory = GatewayClientFactory('pub', __file__)

    def test_connection_made(self):
        client = Mock()
        event = self.factory.EVENT_CONNECTION_MADE
        callback = Mock()
        self.factory.listen(event, callback)

        self.factory.connectionMade(client)

        self.assertEqual(self.factory.client, client)
        callback.assert_called_once_with(event, self.factory)

    @patch(CLASS + '_onConnectionLost')
    @patch(MODULE + 'ReconnectingClientFactory.clientConnectionFailed')
    def test_client_connection_failed(self, client_connection_failed_mock,
                                      on_connection_lost_mock):
        connector = Mock()
        reason = Mock()
        self.factory.clientConnectionFailed(connector, reason)

        on_connection_lost_mock.assert_called_once_with()
        client_connection_failed_mock.assert_called_once_with(self.factory,
                                                              connector,
                                                              reason)

    @patch(CLASS + '_onConnectionLost')
    @patch(MODULE + 'ReconnectingClientFactory.clientConnectionLost')
    def test_client_connection_lost(self, client_connection_lost_mock,
                                    on_connection_lost_mock):
        connector = Mock()
        reason = Mock()
        self.factory.clientConnectionLost(connector, reason)

        on_connection_lost_mock.assert_called_once_with()
        client_connection_lost_mock.assert_called_once_with(self.factory,
                                                            connector,
                                                            reason)

    def test_connected(self):
        self.assertFalse(self.factory.connected)
        self.factory.client = Mock()
        self.assertTrue(self.factory.connected)
        self.factory.client = None
        self.assertFalse(self.factory.connected)

    @defer.inlineCallbacks
    def test_send_client_not_set(self):
        with self.assertRaises(GatewayClientNotSetError):
            yield self.factory.send(Mock())

    def test_send_client_set(self):
        notification = Mock()
        client = Mock()
        self.factory.client = client

        self.factory.send(notification)

        self.factory.client.send.assert_called_once_with(notification)

    def test_on_connection_lost(self):
        self.factory.client = Mock()
        event = self.factory.EVENT_CONNECTION_LOST
        callback = Mock()
        self.factory.listen(event, callback)

        self.factory._onConnectionLost()

        self.assertIsNone(self.factory.client)

        callback.assert_called_once_with(event, self.factory)

    def test_error_received(self):
        error = Mock()
        event = self.factory.EVENT_ERROR_RECEIVED
        callback = Mock()
        self.factory.listen(event, callback)

        self.factory.errorReceived(error)

        callback.assert_called_once_with(event, self.factory, error)
class GatewayClientFactoryTestCase(TestCase):
    CLASS = MODULE + 'GatewayClientFactory.'

    @patch(MODULE + 'ssl.PrivateCertificate.loadPEM', Mock())
    @patch(MODULE + 'GatewayClientFactory.ENDPOINTS', {'pub': ('foo', 'bar')})
    def setUp(self):
        self.factory = GatewayClientFactory('pub', __file__)

    def test_connection_made(self):
        client = Mock()
        event = self.factory.EVENT_CONNECTION_MADE
        callback = Mock()
        self.factory.listen(event, callback)

        self.factory.connectionMade(client)

        self.assertEqual(self.factory.client, client)
        callback.assert_called_once_with(event, self.factory)

    @patch(CLASS + '_onConnectionLost')
    @patch(MODULE + 'ReconnectingClientFactory.clientConnectionFailed')
    def test_client_connection_failed(self, client_connection_failed_mock,
                                      on_connection_lost_mock):
        connector = Mock()
        reason = Mock()
        self.factory.clientConnectionFailed(connector, reason)

        on_connection_lost_mock.assert_called_once_with()
        client_connection_failed_mock.assert_called_once_with(
            self.factory, connector, reason)

    @patch(CLASS + '_onConnectionLost')
    @patch(MODULE + 'ReconnectingClientFactory.clientConnectionLost')
    def test_client_connection_lost(self, client_connection_lost_mock,
                                    on_connection_lost_mock):
        connector = Mock()
        reason = Mock()
        self.factory.clientConnectionLost(connector, reason)

        on_connection_lost_mock.assert_called_once_with()
        client_connection_lost_mock.assert_called_once_with(
            self.factory, connector, reason)

    def test_connected(self):
        self.assertFalse(self.factory.connected)
        self.factory.client = Mock()
        self.assertTrue(self.factory.connected)
        self.factory.client = None
        self.assertFalse(self.factory.connected)

    @defer.inlineCallbacks
    def test_send_client_not_set(self):
        with self.assertRaises(GatewayClientNotSetError):
            yield self.factory.send(Mock())

    def test_send_client_set(self):
        notification = Mock()
        client = Mock()
        self.factory.client = client

        self.factory.send(notification)

        self.factory.client.send.assert_called_once_with(notification)

    def test_on_connection_lost(self):
        self.factory.client = Mock()
        event = self.factory.EVENT_CONNECTION_LOST
        callback = Mock()
        self.factory.listen(event, callback)

        self.factory._onConnectionLost()

        self.assertIsNone(self.factory.client)

        callback.assert_called_once_with(event, self.factory)

    def test_error_received(self):
        error = Mock()
        event = self.factory.EVENT_ERROR_RECEIVED
        callback = Mock()
        self.factory.listen(event, callback)

        self.factory.errorReceived(error)

        callback.assert_called_once_with(event, self.factory, error)