88import random
99import string
1010import threading
11- from collections .abc import AsyncGenerator
1211from collections .abc import Generator
1312from collections .abc import Iterable
1413from typing import Callable
1514
1615import pytest
17- import pytest_asyncio
1816from databento import historical
1917from databento import live
2018from 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" )
268270def fixture_historical_client (
269271 test_api_key : str ,
0 commit comments