Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
class MockHandler(BaseHandler):
def create_request(self):
return MetadataRequest[0]([])
mock_handler = MockHandler(sender)
mock_handler.handle_response = mock.Mock(return_value=0.1)
success = await mock_handler.do(node_id=0)
self.assertFalse(success)
MockHandler.return_value = None
mock_handler.handle_response = mock.Mock(return_value=None)
success = await mock_handler.do(node_id=0)
self.assertTrue(success)
time = self.loop.time()
sender.client.send = mock.Mock(side_effect=UnknownError())
success = await mock_handler.do(node_id=0)
self.assertFalse(success)
self.assertAlmostEqual(self.loop.time() - time, 0.1, 1)
async def test_consumer_propagates_heartbeat_errors(self):
consumer = AIOKafkaConsumer(
loop=self.loop,
enable_auto_commit=False,
auto_offset_reset="earliest",
group_id="group-" + self.id(),
bootstrap_servers=self.hosts)
await consumer.start()
self.add_cleanup(consumer.stop)
with mock.patch.object(consumer._coordinator, "_do_heartbeat") as m:
m.side_effect = UnknownError
consumer.subscribe([self.topic]) # Force join error
with self.assertRaises(KafkaError):
await consumer.getone()
# This time we won't kill the fetch waiter, we will check errors
# before waiting
with self.assertRaises(KafkaError):
await consumer.getone()
# Not coordination retriable errors
for error_cls in [
CoordinatorLoadInProgressError, UnknownTopicOrPartitionError]:
resp = create_response(error_cls)
backoff = add_handler.handle_response(resp)
self.assertEqual(backoff, 0.1)
tm.offset_committed.assert_not_called()
# ProducerFenced case
resp = create_response(InvalidProducerEpoch)
with self.assertRaises(ProducerFenced):
add_handler.handle_response(resp)
tm.offset_committed.assert_not_called()
# Handle unknown error
resp = create_response(UnknownError)
with self.assertRaises(UnknownError):
add_handler.handle_response(resp)
tm.offset_committed.assert_not_called()
supported_mechanisms = ["GSSAPI"]
with self.assertRaises(UnsupportedSaslMechanismError):
await conn._do_sasl_handshake()
self.assertTrue(close_mock.call_count)
supported_mechanisms = ["PLAIN"]
auth_error_class = IllegalSaslStateError
close_mock.reset()
with self.assertRaises(IllegalSaslStateError):
await conn._do_sasl_handshake()
self.assertTrue(close_mock.call_count)
auth_error_class = NoError
error_class = UnknownError
close_mock.reset()
with self.assertRaises(UnknownError):
await conn._do_sasl_handshake()
self.assertTrue(close_mock.call_count)
conn._send_sasl_token = mock.Mock(side_effect=mock_sasl_send)
conn._version_info = VersionInfo({
SaslHandShakeRequest[0].API_KEY: [0, 0]
})
await conn._do_sasl_handshake()
supported_mechanisms = ["GSSAPI"]
with self.assertRaises(UnsupportedSaslMechanismError):
await conn._do_sasl_handshake()
self.assertTrue(close_mock.call_count)
error_class = UnknownError
close_mock.reset()
with self.assertRaises(UnknownError):
await conn._do_sasl_handshake()
self.assertTrue(close_mock.call_count)
return b""
conn.send = mock.Mock(side_effect=mock_send)
conn._send_sasl_token = mock.Mock(side_effect=mock_sasl_send)
conn._version_info = VersionInfo({
SaslHandShakeRequest[0].API_KEY: [0, 0]
})
await conn._do_sasl_handshake()
supported_mechanisms = ["GSSAPI"]
with self.assertRaises(UnsupportedSaslMechanismError):
await conn._do_sasl_handshake()
self.assertTrue(close_mock.call_count)
error_class = UnknownError
close_mock.reset()
with self.assertRaises(UnknownError):
await conn._do_sasl_handshake()
self.assertTrue(close_mock.call_count)
self.assertEqual(fetcher._records, {})
# CASE: set error if _default_reset_strategy = OffsetResetStrategy.NONE
assignment, tp_state = reset_assignment()
self.loop.call_later(
0.01, tp_state.update_committed, OffsetAndMetadata(-1, ""))
fetcher._default_reset_strategy = OffsetResetStrategy.NONE
needs_wakeup = await fetcher._update_fetch_positions(
assignment, 0, [partition])
self.assertTrue(needs_wakeup)
self.assertIsNone(tp_state._position)
self.assertIsInstance(fetcher._records[partition], FetchError)
fetcher._records.clear()
# CASE: if _proc_offset_request errored, we will retry on another spin
fetcher._proc_offset_request.side_effect = UnknownError()
assignment, tp_state = reset_assignment()
tp_state.await_reset(OffsetResetStrategy.LATEST)
await fetcher._update_fetch_positions(assignment, 0, [partition])
self.assertIsNone(tp_state._position)
self.assertTrue(tp_state.awaiting_reset)
# CASE: reset 2 partitions separately, 1 will raise, 1 will get
# committed
fetcher._proc_offset_request.side_effect = _proc_offset_request
partition2 = TopicPartition('test', 1)
subscriptions.assign_from_user({partition, partition2})
assignment = subscriptions.subscription.assignment
tp_state = assignment.state_value(partition)
tp_state2 = assignment.state_value(partition2)
tp_state.await_reset(OffsetResetStrategy.LATEST)
self.loop.call_later(
tm.partition_added.assert_not_called()
# ProducerFenced case
resp = create_response(InvalidProducerEpoch)
with self.assertRaises(ProducerFenced):
add_handler.handle_response(resp)
tm.partition_added.assert_not_called()
for error_type in [InvalidProducerIdMapping, InvalidTxnState]:
resp = create_response(error_type)
with self.assertRaises(error_type):
add_handler.handle_response(resp)
tm.partition_added.assert_not_called()
# Handle unknown error
resp = create_response(UnknownError)
with self.assertRaises(UnknownError):
add_handler.handle_response(resp)
tm.partition_added.assert_not_called()
for error_cls in [
CoordinatorLoadInProgressError, UnknownTopicOrPartitionError]:
resp = create_response(error_cls)
backoff = add_handler.handle_response(resp)
self.assertEqual(backoff, 0.1)
tm.offset_committed.assert_not_called()
# ProducerFenced case
resp = create_response(InvalidProducerEpoch)
with self.assertRaises(ProducerFenced):
add_handler.handle_response(resp)
tm.offset_committed.assert_not_called()
# Handle unknown error
resp = create_response(UnknownError)
with self.assertRaises(UnknownError):
add_handler.handle_response(resp)
tm.offset_committed.assert_not_called()