Skip to content

Commit 5fff910

Browse files
committed
test: refactor test_webserver_graceful_shutdown to use pytest.mark.parametrize
1 parent 94143d4 commit 5fff910

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

tests/questionpy_sdk/webserver/test_webserver.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -189,37 +189,37 @@ class PackageQuestion(Question):
189189
return QuestionTypeWrapper(PackageQuestion, package)
190190

191191

192-
async def _assert_graceful_shutdown(pkg_location: PackageLocation, storage_path: Path, port: int) -> None:
193-
async with WebServer(pkg_location, state_storage_path=storage_path, port=port):
194-
await asyncio.sleep(0)
195-
196-
pending = [t for t in asyncio.all_tasks() if t is not asyncio.current_task() and not t.done()]
197-
if pending:
198-
pending_tasks_display = "\n".join(f" - {t}" for t in pending)
199-
for task in pending:
200-
task.cancel()
201-
await asyncio.gather(*pending, return_exceptions=True)
202-
pytest.fail(f"Pending tasks after shutdown:\n{pending_tasks_display}")
192+
@pytest.fixture
193+
def function_pkg_location() -> FunctionPackageLocation:
194+
return FunctionPackageLocation.from_function(_pkg_init)
203195

204196

205-
@pytest.mark.asyncio(loop_scope="function")
206-
async def test_webserver_shutdown_function(tmp_path: Path, port: int) -> None:
207-
pkg_location = FunctionPackageLocation.from_function(_pkg_init)
197+
@pytest.fixture
198+
def dir_pkg_location(source_path: Path) -> DirPackageLocation:
199+
with DirPackageBuilder(PackageSource(source_path)) as builder:
200+
builder.write_package()
201+
return DirPackageLocation(source_path / DIST_DIR)
208202

209-
await _assert_graceful_shutdown(pkg_location, tmp_path, port)
210203

204+
@pytest.fixture
205+
def zip_pkg_location(qpy_pkg_path: Path) -> ZipPackageLocation:
206+
return ZipPackageLocation(qpy_pkg_path, calculate_hash(qpy_pkg_path))
211207

212-
@pytest.mark.asyncio(loop_scope="function")
213-
async def test_webserver_shutdown_dir(tmp_path: Path, port: int, source_path: Path) -> None:
214-
with DirPackageBuilder(PackageSource(source_path)) as builder:
215-
builder.write_package()
216-
pkg_location = DirPackageLocation(source_path / DIST_DIR)
217208

218-
await _assert_graceful_shutdown(pkg_location, tmp_path, port)
209+
@pytest.fixture(params=["function_pkg_location", "dir_pkg_location", "zip_pkg_location"])
210+
def pkg_location(request: pytest.FixtureRequest) -> PackageLocation:
211+
return request.getfixturevalue(request.param)
219212

220213

221214
@pytest.mark.asyncio(loop_scope="function")
222-
async def test_webserver_shutdown_zip(tmp_path: Path, port: int, qpy_pkg_path: Path) -> None:
223-
pkg_location = ZipPackageLocation(qpy_pkg_path, calculate_hash(qpy_pkg_path))
215+
async def test_webserver_graceful_shutdown(pkg_location: PackageLocation, tmp_path: Path, port: int) -> None:
216+
async with WebServer(pkg_location, state_storage_path=tmp_path, port=port):
217+
await asyncio.sleep(0)
224218

225-
await _assert_graceful_shutdown(pkg_location, tmp_path, port)
219+
pending = [t for t in asyncio.all_tasks() if t is not asyncio.current_task() and not t.done()]
220+
if pending:
221+
pending_tasks_display = "\n".join(f" - {t}" for t in pending)
222+
for task in pending:
223+
task.cancel()
224+
await asyncio.gather(*pending, return_exceptions=True)
225+
pytest.fail(f"Pending tasks after shutdown:\n{pending_tasks_display}")

0 commit comments

Comments
 (0)