本文整理汇总了Python中kafka.client_async.KafkaClient._maybe_connect方法的典型用法代码示例。如果您正苦于以下问题:Python KafkaClient._maybe_connect方法的具体用法?Python KafkaClient._maybe_connect怎么用?Python KafkaClient._maybe_connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kafka.client_async.KafkaClient
的用法示例。
在下文中一共展示了KafkaClient._maybe_connect方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_is_ready
# 需要导入模块: from kafka.client_async import KafkaClient [as 别名]
# 或者: from kafka.client_async.KafkaClient import _maybe_connect [as 别名]
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)
示例2: test_send
# 需要导入模块: from kafka.client_async import KafkaClient [as 别名]
# 或者: from kafka.client_async.KafkaClient import _maybe_connect [as 别名]
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)
示例3: test_maybe_connect
# 需要导入模块: from kafka.client_async import KafkaClient [as 别名]
# 或者: from kafka.client_async.KafkaClient import _maybe_connect [as 别名]
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
示例4: test_is_disconnected
# 需要导入模块: from kafka.client_async import KafkaClient [as 别名]
# 或者: from kafka.client_async.KafkaClient import _maybe_connect [as 别名]
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)
示例5: test_maybe_connect
# 需要导入模块: from kafka.client_async import KafkaClient [as 别名]
# 或者: from kafka.client_async.KafkaClient import _maybe_connect [as 别名]
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
示例6: test_close
# 需要导入模块: from kafka.client_async import KafkaClient [as 别名]
# 或者: from kafka.client_async.KafkaClient import _maybe_connect [as 别名]
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
示例7: test_close
# 需要导入模块: from kafka.client_async import KafkaClient [as 别名]
# 或者: from kafka.client_async.KafkaClient import _maybe_connect [as 别名]
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
示例8: test_close
# 需要导入模块: from kafka.client_async import KafkaClient [as 别名]
# 或者: from kafka.client_async.KafkaClient import _maybe_connect [as 别名]
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
示例9: test_can_connect
# 需要导入模块: from kafka.client_async import KafkaClient [as 别名]
# 或者: from kafka.client_async.KafkaClient import _maybe_connect [as 别名]
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)