Skip to content

Commit 60bdf74

Browse files
committedJun 4, 2015
tests: use try/finally to close event loop + wait for process to die after killing it
1 parent 78f9268 commit 60bdf74

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed
 

‎artiq/master/worker.py

+4
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,17 @@ def close(self):
101101
logger.warning("failed to send terminate command to worker"
102102
" (RID %d), killing", self.rid, exc_info=True)
103103
self.process.kill()
104+
# Wait for process to terminate
105+
yield from self.process.communicate()
104106
return
105107
try:
106108
yield from asyncio_process_wait_timeout(self.process,
107109
self.term_timeout)
108110
except asyncio.TimeoutError:
109111
logger.warning("worker did not exit (RID %d), killing", self.rid)
110112
self.process.kill()
113+
# Wait for process to terminate
114+
yield from self.process.communicate()
111115
else:
112116
logger.debug("worker exited gracefully (RID %d)", self.rid)
113117
finally:

‎artiq/test/pc_rpc.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ def _asyncio_echo(self):
7575
def _loop_asyncio_echo(self):
7676
loop = asyncio.new_event_loop()
7777
asyncio.set_event_loop(loop)
78-
loop.run_until_complete(self._asyncio_echo())
79-
loop.close()
78+
try:
79+
loop.run_until_complete(self._asyncio_echo())
80+
finally:
81+
loop.close()
8082

8183
def test_asyncio_echo(self):
8284
self._run_server_and_test(self._loop_asyncio_echo)

‎artiq/test/worker.py

+23-14
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,29 @@ def _call_worker(worker, expid):
4040

4141

4242
def _run_experiment(experiment):
43-
loop = asyncio.new_event_loop()
44-
asyncio.set_event_loop(loop)
45-
expid = {
46-
"file": sys.modules[__name__].__file__,
47-
"experiment": experiment,
48-
"arguments": dict()
49-
}
50-
handlers = {
51-
"init_rt_results": lambda description: None
52-
}
53-
54-
worker = Worker(handlers)
55-
loop.run_until_complete(_call_worker(worker, expid))
56-
loop.close()
43+
try:
44+
expid = {
45+
"file": sys.modules[__name__].__file__,
46+
"experiment": experiment,
47+
"arguments": dict()
48+
}
49+
handlers = {
50+
"init_rt_results": lambda description: None
51+
}
52+
53+
loop = asyncio.get_event_loop()
54+
worker = Worker(handlers)
55+
loop.run_until_complete(_call_worker(worker, expid))
56+
finally:
57+
loop.close()
5758

5859

5960
class WatchdogCase(unittest.TestCase):
61+
62+
def setUp(self):
63+
self.loop = asyncio.new_event_loop()
64+
asyncio.set_event_loop(self.loop)
65+
6066
def test_watchdog_no_timeout(self):
6167
_run_experiment("WatchdogNoTimeout")
6268

@@ -67,3 +73,6 @@ def test_watchdog_timeout(self):
6773
def test_watchdog_timeout_in_build(self):
6874
with self.assertRaises(WorkerWatchdogTimeout):
6975
_run_experiment("WatchdogTimeoutInBuild")
76+
77+
def tearDown(self):
78+
self.loop.close()

0 commit comments

Comments
 (0)
Please sign in to comment.