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,7 +75,9 @@ 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)
try:
loop.run_until_complete(self._asyncio_echo()) loop.run_until_complete(self._asyncio_echo())
finally:
loop.close() loop.close()
def test_asyncio_echo(self): def test_asyncio_echo(self):

View File

@ -40,8 +40,7 @@ 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,
@ -51,12 +50,19 @@ def _run_experiment(experiment):
"init_rt_results": lambda description: None "init_rt_results": lambda description: None
} }
loop = asyncio.get_event_loop()
worker = Worker(handlers) worker = Worker(handlers)
loop.run_until_complete(_call_worker(worker, expid)) loop.run_until_complete(_call_worker(worker, expid))
finally:
loop.close() 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()