Ejemplo n.º 1
0
 def test_closeNotConnected(self):
     reactor = MemoryReactorClock()
     c = KafkaBrokerClient(reactor, 'test_closeNotConnected', 9092,
                           'clientId')
     c._connect()  # Force a connection attempt
     c.connector.factory = c  # MemoryReactor doesn't make this connection.
     d = c.close()
     self.assertIsInstance(d, Deferred)
Ejemplo n.º 2
0
 def test_close_disconnected(self):
     reactor = MemoryReactorClock()
     c = KafkaBrokerClient(reactor, 'test_close', 9092, 'clientId')
     c._connect()  # Force a connection attempt
     c.connector.factory = c  # MemoryReactor doesn't make this connection.
     c.connector.state = 'disconnected'  # set connector's state for test
     dd = c.close()
     self.assertIsInstance(dd, Deferred)
     r = self.successResultOf(dd)
     self.assertIs(r, None)
Ejemplo n.º 3
0
 def test_connectNotify(self):
     from afkak.protocol import KafkaProtocol
     reactor = MemoryReactorClock()
     c = KafkaBrokerClient(reactor, 'test_connectNotify', 9092, 'clientId')
     c._connect()  # Force a connection attempt
     c.connector.factory = c  # MemoryReactor doesn't make this connection.
     proto = c.buildProtocol(None)
     self.assertIsInstance(proto, KafkaProtocol)
     reactor.advance(1.0)
     self.assertEqual([], reactor.getDelayedCalls())
Ejemplo n.º 4
0
 def test_disconnect(self):
     reactor = MemoryReactorClock()
     c = KafkaBrokerClient(reactor, 'test_close', 9092, 'clientId')
     c._connect()  # Force a connection attempt
     conn = c.connector
     conn.factory = c  # MemoryReactor doesn't make this connection.
     conn.state = 'connected'  # set the connector to connected state
     self.assertIs(conn._disconnected, False)
     c.disconnect()
     self.assertIs(conn._disconnected, True)
Ejemplo n.º 5
0
 def test_closeNotifyDuringConnect(self):
     reactor = MemoryReactorClock()
     c = KafkaBrokerClient(reactor, 'test_closeNotify', 9092, 'clientId')
     c._connect()  # Force a connection attempt
     c.connector.factory = c  # MemoryReactor doesn't make this connection.
     reactor.advance(1.0)
     self.assertEqual([], reactor.getDelayedCalls())
     c.close()
     c.clientConnectionFailed(c.connector, Failure(UserError()))
     reactor.advance(1.0)
     self.assertEqual([], reactor.getDelayedCalls())
Ejemplo n.º 6
0
 def test_connected(self):
     reactor = MemoryReactorClock()
     reactor.running = True
     c = KafkaBrokerClient(reactor, 'test_connect', 9092, 'clientId')
     c._connect()  # Force a connection attempt
     c.connector.factory = c  # MemoryReactor doesn't make this connection.
     self.assertFalse(c.connected())
     # Build the protocol, like a real connector would
     c.buildProtocol(None)
     self.assertTrue(c.connected())
     reactor.advance(1.0)  # Trigger the DelayedCall to _notify
Ejemplo n.º 7
0
    def test_connect(self):
        reactor = MemoryReactorClock()
        reactor.running = True
        c = KafkaBrokerClient(reactor, 'kafka.example.com', 9092, 'clientId')
        c._connect()  # Force a connection attempt
        c.connector.factory = c  # MemoryReactor doesn't make this connection.
        # Build the protocol, like a real connector would
        addr = IPv4Address('TCP', '1.2.3.4', 9092)
        c.buildProtocol(addr)

        self.assertEqual(("<KafkaBrokerClient kafka.example.com:9092 "
                          "clientId='clientId' connected>"), repr(c))
Ejemplo n.º 8
0
 def test_closeNotify(self):
     from twisted.internet.error import ConnectionDone
     reactor = MemoryReactorClock()
     c = KafkaBrokerClient(reactor, 'test_closeNotify', 9092, 'clientId')
     c._connect()  # Force a connection attempt
     c.connector.factory = c  # MemoryReactor doesn't make this connection.
     c.buildProtocol(None)
     reactor.advance(1.0)
     self.assertEqual([], reactor.getDelayedCalls())
     c.continueTrying = False
     c.close()
     c.clientConnectionLost(c.connector, Failure(ConnectionDone()))
     reactor.advance(1.0)
     self.assertEqual([], reactor.getDelayedCalls())
Ejemplo n.º 9
0
 def test_close(self):
     reactor = MemoryReactorClock()
     c = KafkaBrokerClient(reactor, 'test_close', 9092, 'clientId')
     c._connect()  # Force a connection attempt
     c.connector.factory = c  # MemoryReactor doesn't make this connection.
     c.connector.state = 'connected'  # set the connector to connected state
     dd = c.close()
     self.assertIsInstance(dd, Deferred)
     self.assertNoResult(dd)
     f = Failure(ConnectionDone('test_close'))
     c.clientConnectionLost(c.connector, f)
     self.assertNoResult(dd)
     # Advance the clock so the notify() call fires
     reactor.advance(0.1)
     r = self.successResultOf(dd)
     self.assertIs(r, None)
Ejemplo n.º 10
0
 def test_cancelRequestNoReply(self):
     id2 = 87654
     reactor = MemoryReactorClock()
     c = KafkaBrokerClient(reactor, 'test_connect', 9092, 'clientId')
     c._connect()  # Force a connection attempt
     c.connector.factory = c  # MemoryReactor doesn't make this connection.
     # Fake a protocol
     c.proto = Mock()
     # now call with 'expectReply=False'
     c.proto = Mock()
     request = KafkaCodec.encode_fetch_request(b'testcancelRequest2', id2)
     d2 = c.makeRequest(id2, request, expectResponse=False)
     self.assertIsInstance(d2, Deferred)
     c.proto.sendString.assert_called_once_with(request)
     # This one we cancel by ID. It should fail due to the
     # expectResponse=False, since we don't keep the requestID after send
     self.assertRaises(KeyError, c.cancelRequest, id2)
Ejemplo n.º 11
0
 def test_connectFailNotify(self, ccf):
     """
     Check that if the connection fails to come up that the brokerclient
     errback's the deferred returned from the '_connect' call.
     """
     reactor = MemoryReactorClock()
     c = KafkaBrokerClient(reactor, 'test_connectFailNotify', 9092,
                           'clientId')
     # attempt connection
     c._connect()  # Force a connection attempt
     c.connector.factory = c  # MemoryReactor doesn't make this connection.
     conn = c.connector
     # Claim the connection failed
     e = ConnectionRefusedError()
     conn.connectionFailed(e)
     # Check that the brokerclient called super to reconnect
     ccf.assert_called_once_with(c, conn, e)
Ejemplo n.º 12
0
    def test_cancelRequest(self):
        errBackCalled = [False]

        def _handleCancelErrback(reason):
            log.debug("_handleCancelErrback: %r", reason)
            reason.trap(CancelledError)
            errBackCalled[0] = True

        id1 = 65432
        reactor = MemoryReactorClock()
        c = KafkaBrokerClient(reactor, 'test_connect', 9092, 'clientId')
        c._connect()  # Force a connection attempt
        c.connector.factory = c  # MemoryReactor doesn't make this connection.
        # Fake a protocol
        c.proto = Mock()
        request = KafkaCodec.encode_fetch_request(b'testcancelRequest', id1)
        d = c.makeRequest(id1, request)
        self.assertIsInstance(d, Deferred)
        d.addErrback(_handleCancelErrback)
        c.proto.sendString.assert_called_once_with(request)
        # Now try to cancel the request
        d.cancel()
        self.assertTrue(errBackCalled[0])
Ejemplo n.º 13
0
 def test_delay_reset(self):
     """
     Test that reconnect delay is handled correctly:
     1) That initializer values are respected
     2) That delay maximum is respected
     3) That delay is reset to initial delay on successful connection
     """
     init_delay = last_delay = 0.025
     max_delay = 14
     reactor = MemoryReactorClock()
     c = KafkaBrokerClient(reactor,
                           'test_delay_reset',
                           9092,
                           'clientId',
                           initDelay=init_delay,
                           maxDelay=max_delay)
     c.jitter = 0  # Eliminate randomness for test
     # Ensure KBC was initialized correctly
     self.assertEqual(c.retries, 0)
     self.assertEqual(c.delay, init_delay)
     self.assertEqual(c.maxDelay, max_delay)
     self.assertTrue(c.continueTrying)
     c._connect()  # Force a connection attempt
     c.connector.factory = c  # MemoryReactor doesn't make this connection.
     self.assertTrue(c.connector.connectCalled)
     # Reset it so we can track future calls
     c.connector.connectCalled = False
     # Build the protocol, like a real connector would on successful connect
     c.buildProtocol(None)
     # Fake server connection close
     f = Failure(ConnectionDone('test_delay_reset'))
     c.clientConnectionLost(c.connector, f)
     # Now loop failing connection attempts until we get to the max
     while c.delay < max_delay:
         # Assert a reconnect wasn't immediately attempted
         self.assertFalse(c.connector.connectCalled)
         # Assert the new delay was calculated correctly
         self.assertEqual(last_delay * c.factor, c.delay)
         last_delay = c.delay
         # advance the reactor, but not enough to connect
         reactor.advance(0.1 * c.delay)
         # Still no connection
         self.assertFalse(c.connector.connectCalled)
         # Should see a connection attempt after this
         reactor.advance(c.delay)
         self.assertTrue(c.connector.connectCalled)
         c.connector.connectCalled = False  # Reset again
         # Claim the connection failed
         e = ConnectionRefusedError()
         c.connector.connectionFailed(e)
     # Assert the delay was calculated correctly
     self.assertEqual(max_delay, c.delay)
     self.assertFalse(c.connector.connectCalled)
     # "run" the reactor, but not enough to connect
     reactor.advance(0.1 * c.delay)
     # Still no connection
     self.assertFalse(c.connector.connectCalled)
     # Should see a connection attempt after this
     reactor.advance(c.delay)
     self.assertTrue(c.connector.connectCalled)
     # Build the protocol, like a real connector would on successful connect
     c.buildProtocol(None)
     # Assert that the delay and retry count were reset
     self.assertEqual(init_delay, c.delay)
     self.assertEqual(c.retries, 0)
Ejemplo n.º 14
0
 def test_connectTwice(self):
     reactor = MemoryReactorClock()
     c = KafkaBrokerClient(reactor, 'test_connectTwice', 9092, 'clientId')
     c._connect()  # Force a connection attempt
     c.connector.factory = c  # MemoryReactor doesn't make this connection.
     self.assertRaises(ClientError, c._connect)