From c8ab6c1b2b364123019f271ad5002c612d01b718 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Tue, 10 Jan 2023 12:36:33 +0800 Subject: [PATCH] test_worker: fix asyncio event loop management --- artiq/test/test_worker.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/artiq/test/test_worker.py b/artiq/test/test_worker.py index c33daf430..9667e69ea 100644 --- a/artiq/test/test_worker.py +++ b/artiq/test/test_worker.py @@ -62,44 +62,42 @@ async def _call_worker(worker, expid): await worker.close() -def _run_experiment(class_name): - expid = { - "log_level": logging.WARNING, - "file": sys.modules[__name__].__file__, - "class_name": class_name, - "arguments": dict() - } - loop = asyncio.get_event_loop() - worker = Worker({}) - loop.run_until_complete(_call_worker(worker, expid)) - - class WorkerCase(unittest.TestCase): def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) + def _run_experiment(self, class_name): + expid = { + "log_level": logging.WARNING, + "file": sys.modules[__name__].__file__, + "class_name": class_name, + "arguments": dict() + } + worker = Worker({}) + self.loop.run_until_complete(_call_worker(worker, expid)) + def test_simple_run(self): - _run_experiment("SimpleExperiment") + self._run_experiment("SimpleExperiment") def test_exception(self): with self.assertLogs() as logs: with self.assertRaises(WorkerInternalException): - _run_experiment("ExceptionTermination") + self._run_experiment("ExceptionTermination") self.assertGreater(len(logs.records), 0) self.assertIn("Terminating with exception (TypeError)", logs.output[-1]) def test_watchdog_no_timeout(self): - _run_experiment("WatchdogNoTimeout") + self._run_experiment("WatchdogNoTimeout") def test_watchdog_timeout(self): with self.assertRaises(WorkerWatchdogTimeout): - _run_experiment("WatchdogTimeout") + self._run_experiment("WatchdogTimeout") def test_watchdog_timeout_in_build(self): with self.assertRaises(WorkerWatchdogTimeout): - _run_experiment("WatchdogTimeoutInBuild") + self._run_experiment("WatchdogTimeoutInBuild") def tearDown(self): self.loop.close()