faststream icon indicating copy to clipboard operation
faststream copied to clipboard

Bug: Adding a subscriber to a TestRabbitBroker persists between test runs

Open HelgeKrueger opened this issue 2 years ago • 3 comments

Describe the bug

If I create a TestRabbitBroker in a fixture, and then add a subscriber. This subscriber is still available in the next test. As the fixture is function scoped, so on every call this means that the subscriber is added to the real broker.

One can work around this by creating the broker in a factory method.

How to reproduce Install pytest + faststream[rabbit] save source as 'test_bug.pythen runpytest`

import pytest
import pytest_asyncio

from faststream.rabbit import RabbitBroker, TestRabbitBroker


broker = RabbitBroker("bug")


@pytest_asyncio.fixture
async def test_broker():
    async with TestRabbitBroker(broker) as br:
        yield br


@pytest.mark.asyncio
async def test_one(test_broker):
    @test_broker.subscriber("queue")
    async def sub():
        return "foo"

    result = await broker.publish("", queue="queue", rpc=True)

    assert result == "foo"


@pytest.mark.asyncio
async def test_two(test_broker):
    result = await broker.publish("", queue="queue", rpc=True)

    assert result == "foo"...

Environment Include the output of the faststream -v command to display your current project and system environment.

Running FastStream 0.3.3 with CPython 3.11.6 on Linux

HelgeKrueger avatar Dec 11 '23 19:12 HelgeKrueger

Thank you for the Issue, I'll take a look tomorrow

Lancetnik avatar Dec 11 '23 20:12 Lancetnik

Well, TestClient patches the original broker indeed. And it doesn't make it in a clearest way. Seems like I have to refactor all test class logic and implementation and it can't be a fast fix, sorry.

Lancetnik avatar Dec 12 '23 19:12 Lancetnik

The behavior of this test case has changed. I can no longer add new subscribers after TestRabbitBroker has been called, instead I need to add them before creating the TestRabbitBroker.

However: This is an annoying issue, not actually a critical one.

HelgeKrueger avatar Jan 01 '24 16:01 HelgeKrueger