-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE(AsyncGenerator): Stream as async generators (yield fr…
…om stream) are only available with the new typing approach (#157)
- Loading branch information
1 parent
dcf20f5
commit 110215c
Showing
12 changed files
with
390 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,14 @@ authors = ["Marcos Schroh <[email protected]>"] | |
|
||
[tool.poetry.dependencies] | ||
python = "^3.8" | ||
sse-starlette = "^0.10.3" | ||
sse-starlette = "^1.8.2" | ||
kstreams = { path = "../../.", develop = true } | ||
uvicorn = "^0.18.2" | ||
fastapi = "^0.78.0" | ||
fastapi = "^0.108.0" | ||
|
||
[build-system] | ||
requires = ["poetry-core>=1.0.0"] | ||
build-backend = "poetry.core.masonry.api" | ||
|
||
[tool.poetry.scripts] | ||
app = "fastapi_sse.__main__:main" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
from typing import Dict, Sequence, Tuple | ||
from typing import ( | ||
Dict, | ||
Sequence, | ||
Tuple, | ||
) | ||
|
||
Headers = Dict[str, str] | ||
EncodedHeaders = Sequence[Tuple[str, bytes]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from typing import Callable | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from kstreams import ConsumerRecord, Stream, StreamEngine, TestStreamClient | ||
from kstreams.clients import Consumer | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_add_stream_as_generator( | ||
stream_engine: StreamEngine, consumer_record_factory: Callable[..., ConsumerRecord] | ||
): | ||
@stream_engine.stream("local--hello-kpn") | ||
async def stream(cr: ConsumerRecord): | ||
yield cr | ||
|
||
assert stream == stream_engine._streams[0] | ||
assert not stream.running | ||
|
||
cr = consumer_record_factory() | ||
|
||
async def getone(_): | ||
return cr | ||
|
||
with mock.patch.multiple(Consumer, start=mock.DEFAULT, getone=getone): | ||
# simulate an engine start | ||
await stream.start() | ||
|
||
# Now the stream should be running as we are in the context | ||
assert stream.running | ||
async for value in stream: | ||
assert value == cr | ||
break | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_stream_consume_events_as_generator_cr_typing( | ||
stream_engine: StreamEngine, | ||
): | ||
topic = "local--hello-kpn" | ||
event = b'{"message": "Hello world!"}' | ||
client = TestStreamClient(stream_engine) | ||
save_to_db = mock.Mock() | ||
|
||
@stream_engine.stream(topic) | ||
async def stream(cr: ConsumerRecord): | ||
save_to_db(cr.value) | ||
yield cr | ||
|
||
async with client: | ||
await client.send(topic, value=event, key="1") | ||
|
||
async with stream as stream_flow: | ||
async for cr in stream_flow: | ||
assert cr.value == event | ||
break | ||
|
||
# we left the stream context, so it has stopped | ||
assert not stream.running | ||
|
||
# check that the event was consumed | ||
save_to_db.assert_called_once_with(event) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_stream_consume_events_as_generator_all_typing( | ||
stream_engine: StreamEngine, | ||
): | ||
topic = "local--hello-kpn" | ||
event = b'{"message": "Hello world!"}' | ||
client = TestStreamClient(stream_engine) | ||
save_to_db = mock.Mock() | ||
|
||
@stream_engine.stream(topic) | ||
async def my_stream(cr: ConsumerRecord, stream: Stream): | ||
save_to_db(cr.value) | ||
# commit the event. Not ideal but we want to prove that works | ||
await stream.commit() | ||
yield cr | ||
|
||
async with client: | ||
await client.send(topic, value=event, key="1") | ||
|
||
async with my_stream as stream_flow: | ||
async for cr in stream_flow: | ||
assert cr.value == event | ||
break | ||
|
||
# we left the stream context, so it has stopped | ||
assert not my_stream.running | ||
|
||
# check that the event was consumed | ||
save_to_db.assert_called_once_with(event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.