Skip to content

Commit 482ef5e

Browse files
author
Jeny Sadadia
committed
test: add 'event_loop' fixture
Default event loop is being closed after running `async` tests from test_pubsub.py. This fails subscribe and unsubscribe handler tests. Need to create an instance for default event loop for all the async tests. Signed-off-by: Jeny Sadadia <[email protected]>
1 parent 1d23e72 commit 482ef5e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

test/conftest.py

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""pytest fixtures for KernelCI API"""
1212

1313
from unittest.mock import AsyncMock
14+
import asyncio
1415
import fakeredis.aioredis
1516
from fastapi.testclient import TestClient
1617
import pytest
@@ -35,6 +36,20 @@ def client():
3536
return TestClient(app)
3637

3738

39+
@pytest.fixture
40+
def event_loop():
41+
"""Create an instance of the default event loop for each test case.
42+
This is a workaround to prevent the default event loop to be closed by
43+
async pubsub tests. It was causing other tests unable to run.
44+
The issue has already been reported here:
45+
https://github.com/pytest-dev/pytest-asyncio/issues/371
46+
"""
47+
loop = asyncio.new_event_loop()
48+
asyncio.set_event_loop(loop)
49+
yield loop
50+
loop.close()
51+
52+
3853
@pytest.fixture
3954
def mock_db_create(mocker):
4055
"""Mocks async call to Database class method used to create object"""

0 commit comments

Comments
 (0)