Skip to content

Fix implementation of repeated single-message fetching #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions mockafka/aiokafka/aiokafka_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,23 @@ async def getone(self):
next_offset = self.kafka.get_partition_next_offset(
topic=topic, partition=partition
)
consumer_amount = self.consumer_store.get(
self._get_key(topic, partition)
)
if first_offset == next_offset:
# Topic partition is empty
continue

topic_key = self._get_key(topic, partition)

consumer_amount = self.consumer_store.setdefault(
topic_key, first_offset
)
if consumer_amount == next_offset:
# Topic partition is exhausted
continue

if consumer_amount is not None:
self.consumer_store[self._get_key(topic, partition)] += 1
else:
self.consumer_store[self._get_key(topic, partition)] = (
first_offset + 1
)
self.consumer_store[topic_key] += 1

return self.kafka.get_message(
topic=topic, partition=partition, offset=first_offset
topic=topic, partition=partition, offset=consumer_amount
)

return None
Expand Down
19 changes: 9 additions & 10 deletions mockafka/conumser.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,23 @@ def poll(self, timeout=None):
next_offset = self.kafka.get_partition_next_offset(
topic=topic, partition=partition
)
consumer_amount = self.consumer_store.get(
self._get_key(topic, partition)
)
if first_offset == next_offset:
# Topic is empty
continue

topic_key = self._get_key(topic, partition)

consumer_amount = self.consumer_store.setdefault(
topic_key, first_offset
)
if consumer_amount == next_offset:
# Topic is exhausted
continue

if consumer_amount is not None:
self.consumer_store[self._get_key(topic, partition)] += 1
else:
self.consumer_store[self._get_key(topic, partition)] = (
first_offset + 1
)
self.consumer_store[topic_key] += 1

return self.kafka.get_message(
topic=topic, partition=partition, offset=first_offset
topic=topic, partition=partition, offset=consumer_amount
)

return None
Expand Down
2 changes: 1 addition & 1 deletion tests/test_aiokafka/test_aiokafka_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def test_poll_without_commit(self):
message = await self.consumer.getone()
self.assertEqual(message.value(payload=None), "test")
message = await self.consumer.getone()
self.assertEqual(message.value(payload=None), "test")
self.assertEqual(message.value(payload=None), "test1")

self.assertIsNone(await self.consumer.getone())
self.assertIsNone(await self.consumer.getone())
Expand Down
2 changes: 1 addition & 1 deletion tests/test_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_poll_without_commit(self):
message = self.consumer.poll()
self.assertEqual(message.value(payload=None), "test")
message = self.consumer.poll()
self.assertEqual(message.value(payload=None), "test")
self.assertEqual(message.value(payload=None), "test1")

self.assertIsNone(self.consumer.poll())
self.assertIsNone(self.consumer.poll())
Expand Down
Loading