forked from M-Labs/artiq
1
0
Fork 0

tests: use try/finally to close event loop + wait for process to die after killing it

This commit is contained in:
Yann Sionneau 2015-06-04 13:40:13 +02:00
parent 78f9268277
commit 60bdf74137
3 changed files with 30 additions and 15 deletions

View File

@ -101,6 +101,8 @@ class Worker:
logger.warning("failed to send terminate command to worker" logger.warning("failed to send terminate command to worker"
" (RID %d), killing", self.rid, exc_info=True) " (RID %d), killing", self.rid, exc_info=True)
self.process.kill() self.process.kill()
# Wait for process to terminate
yield from self.process.communicate()
return return
try: try:
yield from asyncio_process_wait_timeout(self.process, yield from asyncio_process_wait_timeout(self.process,
@ -108,6 +110,8 @@ class Worker:
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning("worker did not exit (RID %d), killing", self.rid) logger.warning("worker did not exit (RID %d), killing", self.rid)
self.process.kill() self.process.kill()
# Wait for process to terminate
yield from self.process.communicate()
else: else:
logger.debug("worker exited gracefully (RID %d)", self.rid) logger.debug("worker exited gracefully (RID %d)", self.rid)
finally: finally:

View File

@ -75,8 +75,10 @@ class RPCCase(unittest.TestCase):
def _loop_asyncio_echo(self): def _loop_asyncio_echo(self):
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
loop.run_until_complete(self._asyncio_echo()) try:
loop.close() loop.run_until_complete(self._asyncio_echo())
finally:
loop.close()
def test_asyncio_echo(self): def test_asyncio_echo(self):
self._run_server_and_test(self._loop_asyncio_echo) self._run_server_and_test(self._loop_asyncio_echo)

View File

@ -40,23 +40,29 @@ def _call_worker(worker, expid):
def _run_experiment(experiment): def _run_experiment(experiment):
loop = asyncio.new_event_loop() try:
asyncio.set_event_loop(loop) expid = {
expid = { "file": sys.modules[__name__].__file__,
"file": sys.modules[__name__].__file__, "experiment": experiment,
"experiment": experiment, "arguments": dict()
"arguments": dict() }
} handlers = {
handlers = { "init_rt_results": lambda description: None
"init_rt_results": lambda description: None }
}
worker = Worker(handlers) loop = asyncio.get_event_loop()
loop.run_until_complete(_call_worker(worker, expid)) worker = Worker(handlers)
loop.close() loop.run_until_complete(_call_worker(worker, expid))
finally:
loop.close()
class WatchdogCase(unittest.TestCase): class WatchdogCase(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
def test_watchdog_no_timeout(self): def test_watchdog_no_timeout(self):
_run_experiment("WatchdogNoTimeout") _run_experiment("WatchdogNoTimeout")
@ -67,3 +73,6 @@ class WatchdogCase(unittest.TestCase):
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") _run_experiment("WatchdogTimeoutInBuild")
def tearDown(self):
self.loop.close()