|
| 1 | +import asyncio |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from tests.smoke_tests.run_smoke_test import ( |
| 6 | + run_smoke_test, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +# Marks all test coroutines in this module |
| 11 | +pytestmark = pytest.mark.asyncio(loop_scope="module") |
| 12 | + |
| 13 | + |
| 14 | +async def try_running_test_task(task: asyncio.Task) -> None: |
| 15 | + """ |
| 16 | + Helper for running task. |
| 17 | +
|
| 18 | + If an exception is reached, then cancel the task and wait for it to be cleared. |
| 19 | + """ |
| 20 | + try: |
| 21 | + await task |
| 22 | + except Exception as e: |
| 23 | + task.cancel() |
| 24 | + await asyncio.gather(task, return_exceptions=True) # allow time to clean up cancelled task |
| 25 | + pytest.fail(f"Smoke test failed due to error. {e}") |
| 26 | + |
| 27 | + |
| 28 | +def assert_on_done_task(task: asyncio.Task) -> None: |
| 29 | + """ |
| 30 | + This function takes a done task and makes assert if a result was returned. |
| 31 | +
|
| 32 | + If an exception was returned, then it fails the pytest for proper shutdown. |
| 33 | + Also, if the task was cancelled, then it cleans up the cancelled tasks so the |
| 34 | + next test doesn't get this hangover and fails as a result. |
| 35 | + """ |
| 36 | + e = task.exception() # handle TimeoutError / CancelledError above this func |
| 37 | + # at this point there is either an Exception or a Result and the task wasn't cancelled |
| 38 | + if e: |
| 39 | + pytest.fail(f"Smoke test execution failed: {e}") |
| 40 | + else: |
| 41 | + server_errors, client_errors = task.result() |
| 42 | + assert len(server_errors) == 0, f"Server metrics check failed. Errors: {server_errors}" |
| 43 | + assert len(client_errors) == 0, f"Client metrics check failed. Errors: {client_errors}" |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.smoketest |
| 47 | +async def test_nnunet_config_2d(tolerance: float) -> None: |
| 48 | + coroutine = run_smoke_test( # By default will use Task04_Hippocampus Dataset |
| 49 | + server_python_path="examples.nnunet_example.server", |
| 50 | + client_python_path="examples.nnunet_example.client", |
| 51 | + config_path="tests/smoke_tests/nnunet_config_2d.yaml", |
| 52 | + dataset_path="examples/datasets/nnunet", |
| 53 | + tolerance=tolerance, |
| 54 | + read_logs_timeout=450, |
| 55 | + ) |
| 56 | + task = asyncio.create_task(coroutine) |
| 57 | + await try_running_test_task(task) |
| 58 | + assert_on_done_task(task) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.smoketest |
| 62 | +async def test_nnunet_config_3d(tolerance: float) -> None: |
| 63 | + coroutine = run_smoke_test( # By default will use Task04_Hippocampus Dataset |
| 64 | + server_python_path="examples.nnunet_example.server", |
| 65 | + client_python_path="examples.nnunet_example.client", |
| 66 | + config_path="tests/smoke_tests/nnunet_config_3d.yaml", |
| 67 | + dataset_path="examples/datasets/nnunet", |
| 68 | + tolerance=tolerance, |
| 69 | + read_logs_timeout=450, |
| 70 | + ) |
| 71 | + task = asyncio.create_task(coroutine) |
| 72 | + await try_running_test_task(task) |
| 73 | + assert_on_done_task(task) |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.smoketest |
| 77 | +async def test_flexible_nnunet_config_2d(tolerance: float) -> None: |
| 78 | + coroutine = run_smoke_test( # By default will use Task04_Hippocampus Dataset |
| 79 | + server_python_path="examples.nnunet_example.server", |
| 80 | + client_python_path="examples.nnunet_example.client_flexible", |
| 81 | + config_path="tests/smoke_tests/nnunet_config_2d.yaml", |
| 82 | + dataset_path="examples/datasets/nnunet", |
| 83 | + tolerance=tolerance, |
| 84 | + read_logs_timeout=450, |
| 85 | + ) |
| 86 | + task = asyncio.create_task(coroutine) |
| 87 | + await try_running_test_task(task) |
| 88 | + assert_on_done_task(task) |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.smoketest |
| 92 | +async def test_flexible_nnunet_config_3d(tolerance: float) -> None: |
| 93 | + coroutine = run_smoke_test( # By default will use Task04_Hippocampus Dataset |
| 94 | + server_python_path="examples.nnunet_example.server", |
| 95 | + client_python_path="examples.nnunet_example.client_flexible", |
| 96 | + config_path="tests/smoke_tests/nnunet_config_3d.yaml", |
| 97 | + dataset_path="examples/datasets/nnunet", |
| 98 | + tolerance=tolerance, |
| 99 | + read_logs_timeout=450, |
| 100 | + ) |
| 101 | + task = asyncio.create_task(coroutine) |
| 102 | + await try_running_test_task(task) |
| 103 | + assert_on_done_task(task) |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.smoketest |
| 107 | +async def test_ditto_flexible_nnunet_config_2d(tolerance: float) -> None: |
| 108 | + coroutine = run_smoke_test( # By default will use Task04_Hippocampus Dataset |
| 109 | + server_python_path="examples.nnunet_pfl_example.server", |
| 110 | + client_python_path="examples.nnunet_pfl_example.client", |
| 111 | + config_path="tests/smoke_tests/nnunet_config_2d.yaml", |
| 112 | + dataset_path="examples/datasets/nnunet", |
| 113 | + tolerance=tolerance, |
| 114 | + read_logs_timeout=450, |
| 115 | + ) |
| 116 | + task = asyncio.create_task(coroutine) |
| 117 | + await try_running_test_task(task) |
| 118 | + assert_on_done_task(task) |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.smoketest |
| 122 | +async def test_nnunet_pfl_mr_mtl_config_3d(tolerance: float) -> None: |
| 123 | + coroutine = run_smoke_test( # By default will use Task04_Hippocampus Dataset |
| 124 | + server_python_path="examples.nnunet_pfl_example.server", |
| 125 | + client_python_path="examples.nnunet_pfl_example.client", |
| 126 | + config_path="tests/smoke_tests/nnunet_config_3d.yaml", |
| 127 | + dataset_path="examples/datasets/nnunet", |
| 128 | + additional_client_args={"--personalized_strategy": "mr_mtl"}, |
| 129 | + tolerance=tolerance, |
| 130 | + read_logs_timeout=450, |
| 131 | + ) |
| 132 | + task = asyncio.create_task(coroutine) |
| 133 | + await try_running_test_task(task) |
| 134 | + assert_on_done_task(task) |
| 135 | + |
| 136 | + |
| 137 | +@pytest.mark.smoketest |
| 138 | +async def test_nnunet_pfl_mr_mtl_config_2d(tolerance: float) -> None: |
| 139 | + coroutine = run_smoke_test( # By default will use Task04_Hippocampus Dataset |
| 140 | + server_python_path="examples.nnunet_pfl_example.server", |
| 141 | + client_python_path="examples.nnunet_pfl_example.client", |
| 142 | + config_path="tests/smoke_tests/nnunet_config_2d.yaml", |
| 143 | + dataset_path="examples/datasets/nnunet", |
| 144 | + additional_client_args={"--personalized_strategy": "mr_mtl"}, |
| 145 | + tolerance=tolerance, |
| 146 | + read_logs_timeout=450, |
| 147 | + ) |
| 148 | + task = asyncio.create_task(coroutine) |
| 149 | + await try_running_test_task(task) |
| 150 | + assert_on_done_task(task) |
0 commit comments