Skip to content

Commit 3fe766f

Browse files
committed
Consolidate fixtures
1 parent fedd6a1 commit 3fe766f

File tree

2 files changed

+42
-50
lines changed

2 files changed

+42
-50
lines changed

tests/conftest.py

+34-24
Original file line numberDiff line numberDiff line change
@@ -95,37 +95,47 @@ def pg_dispatcher() -> DispatcherMain:
9595
def test_settings():
9696
return DispatcherSettings(BASIC_CONFIG)
9797

98+
99+
@pytest.fixture
100+
def adispatcher_factory():
101+
@contextlib.asynccontextmanager
102+
async def _rf(config):
103+
dispatcher = None
104+
try:
105+
settings = DispatcherSettings(config)
106+
dispatcher = from_settings(settings=settings)
107+
108+
await dispatcher.connect_signals()
109+
await dispatcher.start_working()
110+
await dispatcher.wait_for_producers_ready()
111+
await dispatcher.pool.events.workers_ready.wait()
112+
113+
assert dispatcher.pool.finished_count == 0 # sanity
114+
assert dispatcher.control_count == 0
115+
116+
yield dispatcher
117+
finally:
118+
if dispatcher:
119+
try:
120+
await dispatcher.shutdown()
121+
await dispatcher.cancel_tasks()
122+
except Exception:
123+
logger.exception('shutdown had error')
124+
return _rf
125+
126+
98127
@pytest_asyncio.fixture(
99128
loop_scope="function",
100129
scope="function",
101130
params=['ProcessManager', 'ForkServerManager'],
102131
ids=["fork", "forkserver"],
103132
)
104-
async def apg_dispatcher(request) -> AsyncIterator[DispatcherMain]:
105-
dispatcher = None
106-
try:
107-
this_test_config = BASIC_CONFIG.copy()
108-
this_test_config.setdefault('service', {})
109-
this_test_config['service']['process_manager_cls'] = request.param
110-
this_settings = DispatcherSettings(this_test_config)
111-
dispatcher = from_settings(settings=this_settings)
112-
113-
await dispatcher.connect_signals()
114-
await dispatcher.start_working()
115-
await dispatcher.wait_for_producers_ready()
116-
await dispatcher.pool.events.workers_ready.wait()
117-
118-
assert dispatcher.pool.finished_count == 0 # sanity
119-
assert dispatcher.control_count == 0
120-
133+
async def apg_dispatcher(request, adispatcher_factory) -> AsyncIterator[DispatcherMain]:
134+
this_test_config = BASIC_CONFIG.copy()
135+
this_test_config.setdefault('service', {})
136+
this_test_config['service']['process_manager_cls'] = request.param
137+
async with adispatcher_factory(this_test_config) as dispatcher:
121138
yield dispatcher
122-
finally:
123-
if dispatcher:
124-
try:
125-
await dispatcher.shutdown()
126-
await dispatcher.cancel_tasks()
127-
except Exception:
128-
logger.exception('shutdown had error')
129139

130140

131141
@pytest_asyncio.fixture(loop_scope="function", scope="function")

tests/integration/test_socket_use.py

+8-26
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,19 @@ def sock_path(tmp_path_factory):
2020

2121

2222
@pytest.fixture(scope='session')
23-
def socket_settings(sock_path):
24-
config = {"version": 2, "brokers": {"socket": {"socket_path": sock_path}}, "service": {"main_kwargs": {"node_id": "socket-test-server"}}}
25-
return DispatcherSettings(config)
23+
def socket_config(sock_path):
24+
return {"version": 2, "brokers": {"socket": {"socket_path": sock_path}}, "service": {"main_kwargs": {"node_id": "socket-test-server"}}}
2625

2726

28-
@pytest_asyncio.fixture
29-
async def asock_dispatcher(socket_settings) -> AsyncIterator[DispatcherMain]:
30-
dispatcher = None
31-
try:
32-
dispatcher = from_settings(settings=socket_settings)
33-
sock_pth = dispatcher.producers[0].broker.socket_path
34-
assert not os.path.exists(sock_pth)
35-
36-
await dispatcher.connect_signals()
37-
await dispatcher.start_working()
38-
await dispatcher.wait_for_producers_ready()
39-
await dispatcher.pool.events.workers_ready.wait()
40-
41-
assert os.path.exists(sock_pth)
27+
@pytest.fixture(scope='session')
28+
def socket_settings(socket_config):
29+
return DispatcherSettings(socket_config)
4230

43-
assert dispatcher.pool.finished_count == 0 # sanity
44-
assert dispatcher.control_count == 0
4531

32+
@pytest_asyncio.fixture
33+
async def asock_dispatcher(socket_config, adispatcher_factory) -> AsyncIterator[DispatcherMain]:
34+
async with adispatcher_factory(socket_config) as dispatcher:
4635
yield dispatcher
47-
finally:
48-
if dispatcher:
49-
try:
50-
await dispatcher.shutdown()
51-
await dispatcher.cancel_tasks()
52-
except Exception:
53-
logger.exception('shutdown had error')
5436

5537

5638
@pytest_asyncio.fixture

0 commit comments

Comments
 (0)