From 6b6ce1d43f837eb9e4d9faea30b70ac21019127a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Mon, 23 Sep 2024 09:44:43 +0200 Subject: [PATCH] adds test checking for multiple instances of a class containing a task --- tests/task/test_task.py | 151 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/tests/task/test_task.py b/tests/task/test_task.py index bd53c3d..9c83d42 100644 --- a/tests/task/test_task.py +++ b/tests/task/test_task.py @@ -138,3 +138,154 @@ async def test_nested_dict_autostart_task( "'sub_services_dict[\"second\"].my_task.status' changed to 'TaskStatus.RUNNING'" in caplog.text ) + + +@pytest.mark.asyncio(scope="function") +async def test_manual_start_with_multiple_service_instances( + caplog: LogCaptureFixture, +) -> None: + class MySubService(pydase.DataService): + @task() + async def my_task(self) -> None: + logger.info("Triggered task.") + while True: + await asyncio.sleep(1) + + class MyService(pydase.DataService): + sub_services_list = [MySubService() for i in range(2)] + sub_services_dict = {"first": MySubService(), "second": MySubService()} + + service_instance = MyService() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) + + autostart_service_tasks(service_instance) + + await asyncio.sleep(0.1) + + assert ( + service_instance.sub_services_list[0].my_task.status == TaskStatus.NOT_RUNNING + ) + assert ( + service_instance.sub_services_list[1].my_task.status == TaskStatus.NOT_RUNNING + ) + assert ( + service_instance.sub_services_dict["first"].my_task.status + == TaskStatus.NOT_RUNNING + ) + assert ( + service_instance.sub_services_dict["second"].my_task.status + == TaskStatus.NOT_RUNNING + ) + + service_instance.sub_services_list[0].my_task.start() + await asyncio.sleep(0.01) + + assert service_instance.sub_services_list[0].my_task.status == TaskStatus.RUNNING + assert ( + "'sub_services_list[0].my_task.status' changed to 'TaskStatus.RUNNING'" + in caplog.text + ) + assert ( + "'sub_services_list[1].my_task.status' changed to 'TaskStatus.RUNNING'" + not in caplog.text + ) + assert ( + "'sub_services_dict[\"first\"].my_task.status' changed to 'TaskStatus.RUNNING'" + not in caplog.text + ) + assert ( + "'sub_services_dict[\"second\"].my_task.status' changed to 'TaskStatus.RUNNING'" + not in caplog.text + ) + + service_instance.sub_services_list[0].my_task.stop() + await asyncio.sleep(0.01) + + assert "Task 'my_task' was cancelled" in caplog.text + caplog.clear() + + service_instance.sub_services_list[1].my_task.start() + await asyncio.sleep(0.01) + + assert service_instance.sub_services_list[1].my_task.status == TaskStatus.RUNNING + assert ( + "'sub_services_list[0].my_task.status' changed to 'TaskStatus.RUNNING'" + not in caplog.text + ) + assert ( + "'sub_services_list[1].my_task.status' changed to 'TaskStatus.RUNNING'" + in caplog.text + ) + assert ( + "'sub_services_dict[\"first\"].my_task.status' changed to 'TaskStatus.RUNNING'" + not in caplog.text + ) + assert ( + "'sub_services_dict[\"second\"].my_task.status' changed to 'TaskStatus.RUNNING'" + not in caplog.text + ) + + service_instance.sub_services_list[1].my_task.stop() + await asyncio.sleep(0.01) + + assert "Task 'my_task' was cancelled" in caplog.text + caplog.clear() + + service_instance.sub_services_dict["first"].my_task.start() + await asyncio.sleep(0.01) + + assert ( + service_instance.sub_services_dict["first"].my_task.status == TaskStatus.RUNNING + ) + assert ( + "'sub_services_list[0].my_task.status' changed to 'TaskStatus.RUNNING'" + not in caplog.text + ) + assert ( + "'sub_services_list[1].my_task.status' changed to 'TaskStatus.RUNNING'" + not in caplog.text + ) + assert ( + "'sub_services_dict[\"first\"].my_task.status' changed to 'TaskStatus.RUNNING'" + in caplog.text + ) + assert ( + "'sub_services_dict[\"second\"].my_task.status' changed to 'TaskStatus.RUNNING'" + not in caplog.text + ) + + service_instance.sub_services_dict["first"].my_task.stop() + await asyncio.sleep(0.01) + + assert "Task 'my_task' was cancelled" in caplog.text + caplog.clear() + + service_instance.sub_services_dict["second"].my_task.start() + await asyncio.sleep(0.01) + + assert ( + service_instance.sub_services_dict["second"].my_task.status + == TaskStatus.RUNNING + ) + assert ( + "'sub_services_list[0].my_task.status' changed to 'TaskStatus.RUNNING'" + not in caplog.text + ) + assert ( + "'sub_services_list[1].my_task.status' changed to 'TaskStatus.RUNNING'" + not in caplog.text + ) + assert ( + "'sub_services_dict[\"first\"].my_task.status' changed to 'TaskStatus.RUNNING'" + not in caplog.text + ) + assert ( + "'sub_services_dict[\"second\"].my_task.status' changed to 'TaskStatus.RUNNING'" + in caplog.text + ) + + service_instance.sub_services_dict["second"].my_task.stop() + await asyncio.sleep(0.01) + + assert "Task 'my_task' was cancelled" in caplog.text