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

View File

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

View File

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