Skip to content

Commit

Permalink
tests: use try/finally to close event loop + wait for process to die …
Browse files Browse the repository at this point in the history
…after killing it
fallen committed Jun 4, 2015
1 parent 78f9268 commit 60bdf74
Showing 3 changed files with 31 additions and 16 deletions.
4 changes: 4 additions & 0 deletions artiq/master/worker.py
Original file line number Diff line number Diff line change
@@ -101,13 +101,17 @@ def close(self):
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,
self.term_timeout)
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:
6 changes: 4 additions & 2 deletions artiq/test/pc_rpc.py
Original file line number Diff line number Diff line change
@@ -75,8 +75,10 @@ def _asyncio_echo(self):
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)
37 changes: 23 additions & 14 deletions artiq/test/worker.py
Original file line number Diff line number Diff line change
@@ -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
}

worker = Worker(handlers)
loop.run_until_complete(_call_worker(worker, expid))
loop.close()
try:
expid = {
"file": sys.modules[__name__].__file__,
"experiment": experiment,
"arguments": dict()
}
handlers = {
"init_rt_results": lambda description: None
}

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 @@ def test_watchdog_timeout(self):
def test_watchdog_timeout_in_build(self):
with self.assertRaises(WorkerWatchdogTimeout):
_run_experiment("WatchdogTimeoutInBuild")

def tearDown(self):
self.loop.close()

0 comments on commit 60bdf74

Please sign in to comment.