Skip to content

Commit

Permalink
fix(stream): recreate consumer when a stream instance is restarted (#77)
Browse files Browse the repository at this point in the history
This reverts commit f0dca3a.

Co-authored-by: Marcos Schroh <[email protected]>
  • Loading branch information
marcosschroh and marcosschroh authored Nov 11, 2022
1 parent e592312 commit 672eeb5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs-preview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ jobs:
poetry run mkdocs build
- name: Deploy preview
uses: rossjrw/pr-preview-action@v1
uses: rossjrw/pr-preview-action@v1.2.0
with:
source-dir: ./site/
5 changes: 2 additions & 3 deletions kstreams/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ async def func_wrapper(func):
f"CRASHED Stream!!! Task {self._consumer_task} \n\n {e}"
)

if self.consumer is None:
self.consumer = self._create_consumer()

# Always create a consumer on stream.start
self.consumer = self._create_consumer()
func = self.func(self)
await self.consumer.start()
self.running = True
Expand Down
19 changes: 0 additions & 19 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from unittest import mock
from unittest.mock import Mock

import pytest
Expand Down Expand Up @@ -267,21 +266,3 @@ async def test_e2e_consume_multiple_topics():
assert topic_2.total_events == total_events

assert TopicManager.all_messages_consumed()


@pytest.mark.asyncio
async def test_create_consumer_if_consumer_is_none(stream_engine: StreamEngine):
with mock.patch("kstreams.clients.aiokafka.AIOKafkaConsumer.start"):
topic_name = "local--kstreams"

@stream_engine.stream(topic_name, name="my-stream")
async def consume(stream):
async for cr in stream:
...

stream = stream_engine.get_stream("my-stream")
await stream.start()
temp = stream.consumer
await stream.stop()
await stream.start()
assert temp == stream.consumer
19 changes: 19 additions & 0 deletions tests/test_stream_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ async def stream(_):
Consumer.stop.assert_awaited()


@pytest.mark.asyncio
async def test_recreate_consumer_on_re_tart_stream(stream_engine: StreamEngine):
with mock.patch("kstreams.clients.aiokafka.AIOKafkaConsumer.start"):
topic_name = "local--kstreams"
stream_name = "my-stream"

@stream_engine.stream(topic_name, name=stream_name)
async def consume(stream):
async for _ in stream:
...

stream = stream_engine.get_stream(stream_name)
await stream.start()
consumer = stream.consumer
await stream.stop()
await stream.start()
assert consumer is not stream.consumer


@pytest.mark.asyncio
async def test_engine_not_started(stream_engine: StreamEngine):
topic = "local--hello-kpn"
Expand Down

0 comments on commit 672eeb5

Please sign in to comment.