Skip to content

Commit 64c8601

Browse files
committed
FIX: Mock live server test fixture
1 parent 1afe5f8 commit 64c8601

2 files changed

Lines changed: 27 additions & 26 deletions

File tree

tests/conftest.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
import random
99
import string
1010
import threading
11-
from collections.abc import AsyncGenerator
1211
from collections.abc import Generator
1312
from collections.abc import Iterable
1413
from typing import Callable
1514

1615
import pytest
17-
import pytest_asyncio
1816
from databento import historical
1917
from databento import live
2018
from databento.common.publishers import Dataset
@@ -200,13 +198,35 @@ def fixture_test_api_key() -> str:
200198
return f"db-{random_str}"
201199

202200

203-
@pytest_asyncio.fixture(name="mock_live_server")
204-
async def fixture_mock_live_server(
201+
@pytest.fixture(name="thread_loop", scope="session")
202+
def fixture_thread_loop() -> Generator[asyncio.AbstractEventLoop, None, None]:
203+
"""
204+
Fixture for a threaded event loop.
205+
206+
Yields
207+
------
208+
asyncio.AbstractEventLoop
209+
210+
"""
211+
loop = asyncio.new_event_loop()
212+
thread = threading.Thread(
213+
name="MockLiveServer",
214+
target=loop.run_forever,
215+
args=(),
216+
daemon=True,
217+
)
218+
thread.start()
219+
yield loop
220+
loop.stop()
221+
222+
@pytest.fixture(name="mock_live_server")
223+
def fixture_mock_live_server(
224+
thread_loop: asyncio.AbstractEventLoop,
205225
test_api_key: str,
206226
caplog: pytest.LogCaptureFixture,
207227
unused_tcp_port: int,
208228
monkeypatch: pytest.MonkeyPatch,
209-
) -> AsyncGenerator[MockLiveServer, None]:
229+
) -> Generator[MockLiveServer, None, None]:
210230
"""
211231
Fixture for a MockLiveServer instance.
212232
@@ -229,41 +249,23 @@ async def fixture_mock_live_server(
229249
"CONNECT_TIMEOUT_SECONDS",
230250
1,
231251
)
232-
233-
loop = asyncio.new_event_loop()
234-
thread = threading.Thread(
235-
name="MockLiveServer",
236-
target=loop.run_forever,
237-
args=(),
238-
daemon=True,
239-
)
240-
thread.start()
241-
242252
with caplog.at_level("DEBUG"):
243253
mock_live_server = asyncio.run_coroutine_threadsafe(
244254
coro=MockLiveServer.create(
245255
host="127.0.0.1",
246256
port=unused_tcp_port,
247257
dbn_path=TESTS_ROOT / "data",
248258
),
249-
loop=loop,
259+
loop=thread_loop,
250260
).result()
251261

252262
yield mock_live_server
253263

254264
asyncio.run_coroutine_threadsafe(
255265
coro=mock_live_server.stop(),
256-
loop=loop,
266+
loop=thread_loop,
257267
).result()
258268

259-
loop.run_in_executor(
260-
None,
261-
loop.stop,
262-
)
263-
264-
thread.join()
265-
266-
267269
@pytest.fixture(name="historical_client")
268270
def fixture_historical_client(
269271
test_api_key: str,

tests/mock_live_server.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,6 @@ async def stop(self) -> None:
711711
)
712712

713713
self.server.close()
714-
await self.server.wait_closed()
715714

716715

717716
if __name__ == "__main__":

0 commit comments

Comments
 (0)