def test_is_ready(mocker, conn): cli = KafkaClient() cli._maybe_connect(0) cli._maybe_connect(1) # metadata refresh blocks ready nodes assert cli.is_ready(0) assert cli.is_ready(1) cli._metadata_refresh_in_progress = True assert not cli.is_ready(0) assert not cli.is_ready(1) # requesting metadata update also blocks ready nodes cli._metadata_refresh_in_progress = False assert cli.is_ready(0) assert cli.is_ready(1) cli.cluster.request_update() cli.cluster.config['retry_backoff_ms'] = 0 assert not cli._metadata_refresh_in_progress assert not cli.is_ready(0) assert not cli.is_ready(1) cli.cluster._need_update = False # if connection can't send more, not ready assert cli.is_ready(0) conn.can_send_more.return_value = False assert not cli.is_ready(0) conn.can_send_more.return_value = True # disconnected nodes, not ready assert cli.is_ready(0) conn.state = ConnectionStates.DISCONNECTED assert not cli.is_ready(0)
def test_send(conn): cli = KafkaClient() # Send to unknown node => raises AssertionError try: cli.send(2, None) assert False, 'Exception not raised' except AssertionError: pass # Send to disconnected node => NodeNotReady conn.state = ConnectionStates.DISCONNECTED f = cli.send(0, None) assert f.failed() assert isinstance(f.exception, Errors.NodeNotReadyError) conn.state = ConnectionStates.CONNECTED cli._maybe_connect(0) # ProduceRequest w/ 0 required_acks -> no response request = ProduceRequest[0](0, 0, []) ret = cli.send(0, request) assert conn.send.called_with(request, expect_response=False) assert isinstance(ret, Future) request = MetadataRequest[0]([]) cli.send(0, request) assert conn.send.called_with(request, expect_response=True)
def test_maybe_connect(conn): cli = KafkaClient() try: # Node not in metadata, raises AssertionError cli._maybe_connect(2) except AssertionError: pass else: assert False, 'Exception not raised' assert 0 not in cli._conns conn.state = ConnectionStates.DISCONNECTED conn.connect.side_effect = lambda: ConnectionStates.CONNECTING assert cli._maybe_connect(0) is False assert cli._conns[0] is conn assert 0 in cli._connecting conn.state = ConnectionStates.CONNECTING conn.connect.side_effect = lambda: ConnectionStates.CONNECTED assert cli._maybe_connect(0) is True assert 0 not in cli._connecting # Failure to connect should trigger metadata update assert cli.cluster._need_update is False cli._connecting.add(0) conn.state = ConnectionStates.CONNECTING conn.connect.side_effect = lambda: ConnectionStates.DISCONNECTED assert cli._maybe_connect(0) is False assert 0 not in cli._connecting assert cli.cluster._need_update is True
def test_is_disconnected(conn): cli = KafkaClient() # False if not connected yet conn.state = ConnectionStates.DISCONNECTED assert not cli.is_disconnected(0) cli._maybe_connect(0) assert cli.is_disconnected(0) conn.state = ConnectionStates.CONNECTING assert not cli.is_disconnected(0) conn.state = ConnectionStates.CONNECTED assert not cli.is_disconnected(0)
def test_maybe_connect(conn): cli = KafkaClient() try: # Node not in metadata, raises AssertionError cli._maybe_connect(2) except AssertionError: pass else: assert False, 'Exception not raised' # New node_id creates a conn object assert 0 not in cli._conns conn.state = ConnectionStates.DISCONNECTED conn.connect.side_effect = lambda: conn._set_conn_state(ConnectionStates.CONNECTING) assert cli._maybe_connect(0) is False assert cli._conns[0] is conn
def test_close(conn): cli = KafkaClient() # Unknown node - silent cli.close(2) # Single node close cli._maybe_connect(0) assert not conn.close.call_count cli.close(0) assert conn.close.call_count == 1 # All node close cli._maybe_connect(1) cli.close() assert conn.close.call_count == 3
def test_close(mocker, conn): cli = KafkaClient() mocker.patch.object(cli, '_selector') # Unknown node - silent cli.close(2) # Single node close cli._maybe_connect(0) assert not conn.close.call_count cli.close(0) assert conn.close.call_count == 1 # All node close cli._maybe_connect(1) cli.close() assert conn.close.call_count == 3
def test_close(mocker, conn): cli = KafkaClient() mocker.patch.object(cli, '_selector') # bootstrap connection should have been closed assert conn.close.call_count == 1 # Unknown node - silent cli.close(2) # Single node close cli._maybe_connect(0) assert conn.close.call_count == 1 cli.close(0) assert conn.close.call_count == 2 # All node close cli._maybe_connect(1) cli.close() assert conn.close.call_count == 4
def test_can_connect(conn): cli = KafkaClient() # Node is not in broker metadata - cant connect assert not cli._can_connect(2) # Node is in broker metadata but not in _conns assert 0 not in cli._conns assert cli._can_connect(0) # Node is connected, can't reconnect assert cli._maybe_connect(0) is True assert not cli._can_connect(0) # Node is disconnected, can connect cli._conns[0].state = ConnectionStates.DISCONNECTED assert cli._can_connect(0) # Node is disconnected, but blacked out conn.blacked_out.return_value = True assert not cli._can_connect(0)