Skip to content

Commit b445420

Browse files
authored
Fixing non-deterministic consumer test (#195)
## Fixed Issue with Non-Deterministic Consumer Test Resolved an issue that was causing non-deterministic behavior in WebSocket consumer tests. ### Cause The problem was due to using a shared `channel_layer` for message passing between application instances over WebSocket. During test execution, messages could leak between the test environment and the running application, resulting in inconsistent test outcomes. This wasn’t an issue when the application was not running, as only the tests were using the `channel_layer`. ### Solution The consumer tests were updated to use a separate `channel_layer`, isolating the test environment from the running application. This ensures the tests now behave deterministically and are not affected by external interference.
1 parent b4d67cc commit b445420

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

backend/frege/repositories/tests/test_consumers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ def api_key():
2727
_, key = APIKey.objects.create_key(name="test-key")
2828
return key
2929

30+
@pytest.fixture(autouse=True)
31+
def use_test_channel_layer(settings):
32+
settings.CHANNEL_LAYERS = {
33+
"default": {"BACKEND": "channels.layers.InMemoryChannelLayer"}
34+
}
35+
3036
@pytest.mark.django_db(transaction=True)
3137
@pytest.mark.asyncio
3238
class TestLiveStatusConsumer:
@@ -45,7 +51,7 @@ async def _test_event_api(
4551
api_key, request_action, create_fn, response_action, serializer
4652
):
4753
communicator = WebsocketCommunicator(
48-
LiveStatusConsumer.as_asgi(), "/ws/"
54+
LiveStatusConsumer.as_asgi(), "/ws/test-live-status/"
4955
)
5056
try:
5157
connected, subprotocol = await communicator.connect()

0 commit comments

Comments
 (0)