Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_manual_subscribe_pattern(self):
msgs1 = await self.send_messages(0, range(0, 10))
msgs2 = await self.send_messages(1, range(10, 20))
available_msgs = msgs1 + msgs2
consumer = AIOKafkaConsumer(
loop=self.loop, group_id='test-group',
bootstrap_servers=self.hosts, auto_offset_reset='earliest',
enable_auto_commit=False)
consumer.subscribe(pattern="topic-test_manual_subs*")
await consumer.start()
self.add_cleanup(consumer.stop)
await consumer.seek_to_committed()
result = []
for i in range(20):
msg = await consumer.getone()
result.append(msg.value)
self.assertEqual(set(available_msgs), set(result))
await consumer.commit(
{TopicPartition(self.topic, 0): OffsetAndMetadata(9, '')})
await consumer.seek_to_committed(TopicPartition(self.topic, 0))
for i in range(20):
msg = await consumer.getone()
result.append(msg.value)
self.assertEqual(set(available_msgs), set(result))
await consumer.commit(
{TopicPartition(self.topic, 0): OffsetAndMetadata(9, '')})
await consumer.seek_to_committed(TopicPartition(self.topic, 0))
msg = await consumer.getone(TopicPartition(self.topic, 0))
self.assertEqual(msg.value, b'9')
await consumer.commit(
{TopicPartition(self.topic, 0): OffsetAndMetadata(10, '')})
await consumer.stop()
# subscribe by topic
consumer = AIOKafkaConsumer(
loop=self.loop, group_id='test-group',
bootstrap_servers=self.hosts, auto_offset_reset='earliest',
enable_auto_commit=False)
consumer.subscribe(topics=(self.topic,))
await consumer.start()
self.add_cleanup(consumer.stop)
await consumer.seek_to_committed()
result = []
for i in range(10):
msg = await consumer.getone()
result.append(msg.value)
self.assertEqual(set(msgs2), set(result))
self.assertEqual(consumer.subscription(), set([self.topic]))
async def test_consumer_propagates_commit_refresh_errors(self):
consumer = AIOKafkaConsumer(
loop=self.loop,
enable_auto_commit=False,
auto_offset_reset="earliest",
group_id="group-" + self.id(),
bootstrap_servers=self.hosts,
metadata_max_age_ms=500)
await consumer.start()
self.add_cleanup(consumer.stop)
with mock.patch.object(
consumer._coordinator, "_do_fetch_commit_offsets") as m:
m.side_effect = UnknownError
consumer.subscribe([self.topic]) # Force join error
subscription = consumer._subscription.subscription
with self.assertRaises(KafkaError):
self.topic, loop=self.loop,
bootstrap_servers=self.hosts,
security_protocol="SSL", ssl_context=None)
with self.assertRaisesRegex(
ValueError, "Incorrect isolation level READ_CCC"):
consumer = AIOKafkaConsumer(
self.topic, loop=self.loop,
bootstrap_servers=self.hosts,
isolation_level="READ_CCC")
self.add_cleanup(consumer.stop)
await consumer.start()
with self.assertRaisesRegex(
ValueError,
"sasl_plain_username and sasl_plain_password required for "
"PLAIN sasl"):
consumer = AIOKafkaConsumer(
self.topic, loop=self.loop,
bootstrap_servers=self.hosts,
security_protocol="SASL_PLAINTEXT")
bootstrap_servers=self.hosts, enable_auto_commit=False,
auto_offset_reset="earliest")
listener1 = SimpleRebalanceListener(consumer1)
consumer1.subscribe([self.topic], listener=listener1)
await consumer1.start()
self.add_cleanup(consumer1.stop)
msg = await consumer1.getone(tp0)
self.assertEqual(msg.value, b"0")
msg = await consumer1.getone(tp1)
self.assertEqual(msg.value, b"10")
listener1.revoke_mock.assert_called_with(set([]))
listener1.assign_mock.assert_called_with(set([tp0, tp1]))
# By adding a 2nd consumer we trigger rebalance
consumer2 = AIOKafkaConsumer(
loop=self.loop, group_id="test_rebalance_listener_with_coroutines",
bootstrap_servers=self.hosts, enable_auto_commit=False,
auto_offset_reset="earliest")
listener2 = SimpleRebalanceListener(consumer2)
consumer2.subscribe([self.topic], listener=listener2)
await consumer2.start()
self.add_cleanup(consumer2.stop)
msg1 = await consumer1.getone()
msg2 = await consumer2.getone()
# We can't predict the assignment in test
if consumer1.assignment() == set([tp1]):
msg1, msg2 = msg2, msg1
c1_assignment = set([tp1])
c2_assignment = set([tp0])
else:
async def on_partitions_revoked(self, revoked):
pass
async def on_partitions_assigned(self, assigned):
self.seek_task = self.consumer._loop.create_task(
self._super_reseek())
await self.seek_task
async def _super_reseek(self):
committed = await self.consumer.committed(tp0)
position = await self.consumer.position(tp0)
await self.consumer.seek_to_end(tp0)
position2 = await self.consumer.position(tp0)
return committed, position, position2
consumer = AIOKafkaConsumer(
loop=self.loop, group_id="test_rebalance_listener_with_coroutines",
bootstrap_servers=self.hosts, enable_auto_commit=False,
auto_offset_reset="earliest")
listener = SimpleRebalanceListener(consumer)
consumer.subscribe([self.topic], listener=listener)
await consumer.start()
self.add_cleanup(consumer.stop)
committed, position, position2 = await listener.seek_task
self.assertIsNone(committed)
self.assertIsNotNone(position)
self.assertIsNotNone(position2)
async def test_consumer_arguments(self):
with self.assertRaisesRegex(
ValueError, "`security_protocol` should be SSL or PLAINTEXT"):
AIOKafkaConsumer(
self.topic, loop=self.loop,
bootstrap_servers=self.hosts,
security_protocol="SOME")
with self.assertRaisesRegex(
ValueError, "`ssl_context` is mandatory if "
"security_protocol=='SSL'"):
AIOKafkaConsumer(
self.topic, loop=self.loop,
bootstrap_servers=self.hosts,
security_protocol="SSL", ssl_context=None)
with self.assertRaisesRegex(
ValueError, "Incorrect isolation level READ_CCC"):
consumer = AIOKafkaConsumer(
self.topic, loop=self.loop,
bootstrap_servers=self.hosts,
isolation_level="READ_CCC")
self.add_cleanup(consumer.stop)
await consumer.start()
with self.assertRaisesRegex(
ValueError,
"sasl_plain_username and sasl_plain_password required for "
"PLAIN sasl"):
consumer = AIOKafkaConsumer(
async def test_consumer_manual_assignment_no_group_after_start(self):
# Following issue #394 we seemed to mix subscription with manual
# assignment. The main test above probably missed this scenario cause
# it was initialized for subscription.
await self.send_messages(0, list(range(0, 10)))
consumer = AIOKafkaConsumer(
loop=self.loop,
enable_auto_commit=False,
auto_offset_reset="earliest",
group_id=None, bootstrap_servers=self.hosts)
tp = TopicPartition(self.topic, 0)
await consumer.start()
consumer.assign([tp])
self.add_cleanup(consumer.stop)
for i in range(10):
msg = await consumer.getone()
self.assertEqual(msg.value, str(i).encode())
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
async def test_consumer_transactions_not_supported(self):
consumer = AIOKafkaConsumer(
loop=self.loop, bootstrap_servers=self.hosts,
isolation_level="read_committed")
with self.assertRaises(UnsupportedVersionError):
await consumer.start()
await consumer.stop()