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_subscribe_manual(self):
msgs1 = await self.send_messages(0, range(0, 10))
msgs2 = await self.send_messages(1, range(10, 20))
available_msgs = msgs1 + msgs2
consumer = await self.consumer_factory()
pos = await consumer.position(TopicPartition(self.topic, 0))
with self.assertRaises(IllegalStateError):
consumer.assign([TopicPartition(self.topic, 0)])
consumer.unsubscribe()
consumer.assign([TopicPartition(self.topic, 0)])
result = []
for i in range(10):
msg = await consumer.getone()
result.append(msg.value)
self.assertEqual(set(result), set(msgs1))
await consumer.commit()
pos = await consumer.position(TopicPartition(self.topic, 0))
self.assertTrue(pos > 0)
consumer.unsubscribe()
consumer.assign([TopicPartition(self.topic, 1)])
for i in range(10):
msg = await consumer.getone()
with self.assertRaises(ValueError):
await consumer.commit("something")
with self.assertRaises(ValueError):
await consumer.commit({tp: (offset, "metadata", 100)})
with self.assertRaisesRegex(
ValueError, "Key should be TopicPartition instance"):
await consumer.commit({"my_topic": offset_and_metadata})
with self.assertRaisesRegex(
ValueError, "Metadata should be a string"):
await consumer.commit({tp: (offset, 1000)})
with self.assertRaisesRegex(
ValueError, "Metadata should be a string"):
await consumer.commit({tp: (offset, b"\x00\x02")})
with self.assertRaisesRegex(
IllegalStateError, "Partition .* is not assigned"):
await consumer.commit({TopicPartition(self.topic, 10): 1000})
consumer.unsubscribe()
with self.assertRaisesRegex(
IllegalStateError, "Not subscribed to any topics"):
await consumer.commit({tp: 1000})
consumer = AIOKafkaConsumer(
loop=self.loop,
group_id='group-{}'.format(self.id()),
bootstrap_servers=self.hosts)
await consumer.start()
self.add_cleanup(consumer.stop)
consumer.subscribe(topics=set([self.topic]))
with self.assertRaisesRegex(
IllegalStateError, "No partitions assigned"):
def test_subscribe_topic(subscription_state):
mock_listener = MockListener()
subscription_state.subscribe({"tp1", "tp2"}, listener=mock_listener)
assert subscription_state.subscription is not None
assert subscription_state.subscription.topics == {"tp1", "tp2"}
assert subscription_state.subscription.assignment is None
assert subscription_state.subscription.active is True
assert subscription_state.subscription.unsubscribe_future.done() is False
# After subscription to topic we can't change the subscription to pattern
# or user assignment
with pytest.raises(IllegalStateError):
subscription_state.subscribe_pattern(
pattern=re.compile("^tests-.*$"), listener=mock_listener)
with pytest.raises(IllegalStateError):
subscription_state.assign_from_user([TopicPartition("topic", 0)])
# Subsciption of the same type can be applied
old_subsciption = subscription_state.subscription
subscription_state.subscribe(
{"tp1", "tp2", "tp3"}, listener=mock_listener)
assert subscription_state.subscription is not None
assert subscription_state.subscription.topics == {"tp1", "tp2", "tp3"}
assert old_subsciption is not subscription_state.subscription
assert old_subsciption.active is False
assert old_subsciption.unsubscribe_future.done() is True
def _set_subscription_type(self, subscription_type: SubscriptionType):
if (self._subscription_type == SubscriptionType.NONE or
self._subscription_type == subscription_type):
self._subscription_type = subscription_type
else:
raise IllegalStateError(
"Subscription to topics, partitions and pattern are mutually "
"exclusive")
.. versionchanged:: 0.4.0
Changed ``AssertionError`` to ``IllegalStateError`` in case of
unassigned partition.
.. versionchanged:: 0.4.0
Will now raise ``CommitFailedError`` in case membership changed,
as (posibly) this partition is handled by another consumer.
"""
if self._group_id is None:
raise IllegalOperation("Requires group_id")
subscription = self._subscription.subscription
if subscription is None:
raise IllegalStateError("Not subscribed to any topics")
assignment = subscription.assignment
if assignment is None:
raise IllegalStateError("No partitions assigned")
if offsets is None:
offsets = assignment.all_consumed_offsets()
else:
offsets = commit_structure_validate(offsets)
for tp in offsets:
if tp not in assignment.tps:
raise IllegalStateError(
"Partition {} is not assigned".format(tp))
await self._coordinator.commit_offsets(assignment, offsets)
def _assigned_state(self, tp: TopicPartition) -> "TopicPartitionState":
assert self._subscription is not None
assert self._subscription.assignment is not None
tp_state = self._subscription.assignment.state_value(tp)
if tp_state is None:
raise IllegalStateError(
"No current assignment for partition {}".format(tp))
return tp_state
unassigned partition.
.. versionchanged:: 0.4.0
Will now raise ``CommitFailedError`` in case membership changed,
as (posibly) this partition is handled by another consumer.
"""
if self._group_id is None:
raise IllegalOperation("Requires group_id")
subscription = self._subscription.subscription
if subscription is None:
raise IllegalStateError("Not subscribed to any topics")
assignment = subscription.assignment
if assignment is None:
raise IllegalStateError("No partitions assigned")
if offsets is None:
offsets = assignment.all_consumed_offsets()
else:
offsets = commit_structure_validate(offsets)
for tp in offsets:
if tp not in assignment.tps:
raise IllegalStateError(
"Partition {} is not assigned".format(tp))
await self._coordinator.commit_offsets(assignment, offsets)