0

I am trying to forward messages to internal topics in faust. As suggested by faust in this example:

https://faust.readthedocs.io/en/latest/playbooks/vskafka.html

I have the following code

in_topic = app.topic("in_topic", internal=False, partitions=1)
batching = app.topic("batching", internal=True, partitions=1)

....

@app.agent(in_topic)
async def process(stream):
    async for event in stream:
        event.forward(batching)
        yield

But i always get the following error when runnning my pytest:

 AttributeError: 'str' object has no attribute 'forward'

Was this feature removed or do i need to specify the topic differently to get an event, or is this even an issue with pytest ?

Chris
  • 476
  • 4
  • 10

3 Answers3

1

You are using the syntax backwards, thats why!

in_topic = app.topic("in_topic", internal=False, partitions=1)
batching = app.topic("batching", internal=True, partitions=1)

....

@app.agent(in_topic)
async def process(stream):
    async for event in stream:
        batching.send(value=event)
        yield

Should actually work.

Edit:

This is also the only they that I could make correct use of pytest with faust.
The sink method is only usable if you delete all but the last sink of the mocked agent, which does not seem intuitive.

If you first import your sink and then add this decorator to your test, everything should work fine:

from my_path import my_sink, my_agent
from unittest.mock import patch, AsyncMock

@patch(__name__ + '.my_sink.send', new_callable=AsyncMock)
def test_my_agent(test_app)
    async with my_agent.test_context() as agent:
        payload = 'This_works_fine'
        agent.put(payload)
        my_sink.send.assert_called_with(value=payload)
0

try the next one:

@app.agent(in_topic, sink=[batching])
async def process(stream):
    async for event in stream:
        yield event
0

The answer from @Florian Hall works fine. I also found out that the forward method is only implemented for Events, in my case i received a str. The reason could be the difference between Faust Topics and Channels. Another thing is that using pytest and the Faust test_context() behaves weird, for example it forces you to yield even if you don't have a sink defined.

Chris
  • 476
  • 4
  • 10