def test_closeNotConnected(self): reactor = MemoryReactorClock() c = KafkaBrokerClient('test_closeNotConnected', reactor=reactor) c._connect() # Force a connection attempt c.connector.factory = c # MemoryReactor doesn't make this connection. d = c.close() self.assertIsInstance(d, Deferred)
def test_connect(self): reactor = MemoryReactorClock() reactor.running = True c = KafkaBrokerClient('test_connect', reactor=reactor) c._connect() # Force a connection attempt c.connector.factory = c # MemoryReactor doesn't make this connection. # Let's pretend we've connected, which will schedule the firing c.buildProtocol(None) reactor.advance(1.0)
def test_connect(self): reactor = MemoryReactorClock() reactor.running = True c = KafkaBrokerClient('test_connect', reactor=reactor) c._connect() # Force a connection attempt c.connector.factory = c # MemoryReactor doesn't make this connection. # Build the protocol, like a real connector would c.buildProtocol(None) reactor.advance(1.0) # Trigger the DelayedCall to _notify
def test_connectNotify(self): from afkak.protocol import KafkaProtocol reactor = MemoryReactorClock() c = KafkaBrokerClient('test_connectNotify', reactor=reactor) 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.assertFalse(c.clock.getDelayedCalls())
def test_close_disconnected(self): reactor = MemoryReactorClock() c = KafkaBrokerClient('test_close', reactor=reactor) 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)
def test_disconnect(self): reactor = MemoryReactorClock() c = KafkaBrokerClient('test_close', reactor=reactor) 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)
def test_closeNotifyDuringConnect(self): reactor = MemoryReactorClock() c = KafkaBrokerClient('test_closeNotify', reactor=reactor) c._connect() # Force a connection attempt c.connector.factory = c # MemoryReactor doesn't make this connection. reactor.advance(1.0) self.assertFalse(c.clock.getDelayedCalls()) c.close() c.clientConnectionFailed(c.connector, Failure(UserError())) reactor.advance(1.0) self.assertFalse(c.clock.getDelayedCalls())
def test_closeNotify(self): from twisted.internet.error import ConnectionDone reactor = MemoryReactorClock() c = KafkaBrokerClient('test_closeNotify', reactor=reactor) c._connect() # Force a connection attempt c.connector.factory = c # MemoryReactor doesn't make this connection. c.buildProtocol(None) reactor.advance(1.0) self.assertFalse(c.clock.getDelayedCalls()) c.continueTrying = False c.close() c.clientConnectionLost(c.connector, Failure(ConnectionDone())) reactor.advance(1.0) self.assertFalse(c.clock.getDelayedCalls())
def test_close(self): reactor = MemoryReactorClock() c = KafkaBrokerClient('test_close', reactor=reactor) 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)
def test_connectFailNotify(self, ccf): """ test_connectFailNotify Check that if the connection fails to come up that the brokerclient errback's the deferred returned from the '_connect' call. """ c = KafkaBrokerClient('test_connectFailNotify', reactor=MemoryReactorClock()) # 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)
def test_cancelRequestNoReply(self): id2 = 87654 reactor = MemoryReactorClock() c = KafkaBrokerClient('test_connect', reactor=reactor) 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('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)
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('test_connect', reactor=reactor) 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('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])
def test_delay_reset(self): """test_delay_reset 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('test_delay_reset', reactor=reactor, 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)
def test_connectTwice(self): reactor = MemoryReactorClock() c = KafkaBrokerClient('test_connectTwice', reactor=reactor) c._connect() # Force a connection attempt c.connector.factory = c # MemoryReactor doesn't make this connection. self.assertRaises(ClientError, c._connect)