test_worker: fix asyncio event loop management

This commit is contained in:
Sebastien Bourdeauducq 2023-01-10 12:36:33 +08:00
parent a96bbd8508
commit c8ab6c1b2b
1 changed files with 15 additions and 17 deletions

View File

@ -62,44 +62,42 @@ async def _call_worker(worker, expid):
await worker.close() 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): class WorkerCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.loop = asyncio.new_event_loop() self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.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): def test_simple_run(self):
_run_experiment("SimpleExperiment") self._run_experiment("SimpleExperiment")
def test_exception(self): def test_exception(self):
with self.assertLogs() as logs: with self.assertLogs() as logs:
with self.assertRaises(WorkerInternalException): with self.assertRaises(WorkerInternalException):
_run_experiment("ExceptionTermination") self._run_experiment("ExceptionTermination")
self.assertGreater(len(logs.records), 0) self.assertGreater(len(logs.records), 0)
self.assertIn("Terminating with exception (TypeError)", self.assertIn("Terminating with exception (TypeError)",
logs.output[-1]) logs.output[-1])
def test_watchdog_no_timeout(self): def test_watchdog_no_timeout(self):
_run_experiment("WatchdogNoTimeout") self._run_experiment("WatchdogNoTimeout")
def test_watchdog_timeout(self): def test_watchdog_timeout(self):
with self.assertRaises(WorkerWatchdogTimeout): with self.assertRaises(WorkerWatchdogTimeout):
_run_experiment("WatchdogTimeout") self._run_experiment("WatchdogTimeout")
def test_watchdog_timeout_in_build(self): def test_watchdog_timeout_in_build(self):
with self.assertRaises(WorkerWatchdogTimeout): with self.assertRaises(WorkerWatchdogTimeout):
_run_experiment("WatchdogTimeoutInBuild") self._run_experiment("WatchdogTimeoutInBuild")
def tearDown(self): def tearDown(self):
self.loop.close() self.loop.close()