tests: use a different event loop for each test

This commit is contained in:
Yann Sionneau 2015-06-03 15:23:34 +02:00
parent e5f16b29fd
commit 21d88d8345
4 changed files with 25 additions and 10 deletions

View File

@ -73,8 +73,10 @@ class RPCCase(unittest.TestCase):
remote.close_rpc() remote.close_rpc()
def _loop_asyncio_echo(self): def _loop_asyncio_echo(self):
loop = asyncio.get_event_loop() loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self._asyncio_echo()) loop.run_until_complete(self._asyncio_echo())
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)
@ -96,7 +98,8 @@ class Echo:
def run_server(): def run_server():
loop = asyncio.get_event_loop() loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try: try:
echo = Echo() echo = Echo()
server = pc_rpc.Server({"test": echo}) server = pc_rpc.Server({"test": echo})

View File

@ -55,7 +55,12 @@ _handlers = {
class SchedulerCase(unittest.TestCase): class SchedulerCase(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
def test_steps(self): def test_steps(self):
loop = self.loop
scheduler = Scheduler(0, _handlers) scheduler = Scheduler(0, _handlers)
expid = _get_expid("EmptyExperiment") expid = _get_expid("EmptyExperiment")
@ -70,7 +75,6 @@ class SchedulerCase(unittest.TestCase):
done.set() done.set()
scheduler.notifier.publish = notify scheduler.notifier.publish = notify
loop = asyncio.get_event_loop()
scheduler.start() scheduler.start()
# Verify that a timed experiment far in the future does not # Verify that a timed experiment far in the future does not
@ -91,6 +95,7 @@ class SchedulerCase(unittest.TestCase):
loop.run_until_complete(scheduler.stop()) loop.run_until_complete(scheduler.stop())
def test_pause(self): def test_pause(self):
loop = self.loop
scheduler = Scheduler(0, _handlers) scheduler = Scheduler(0, _handlers)
expid_bg = _get_expid("BackgroundExperiment") expid_bg = _get_expid("BackgroundExperiment")
expid = _get_expid("EmptyExperiment") expid = _get_expid("EmptyExperiment")
@ -113,7 +118,6 @@ class SchedulerCase(unittest.TestCase):
done.set() done.set()
scheduler.notifier.publish = notify scheduler.notifier.publish = notify
loop = asyncio.get_event_loop()
scheduler.start() scheduler.start()
scheduler.submit("main", expid_bg, -99, None, False) scheduler.submit("main", expid_bg, -99, None, False)
loop.run_until_complete(background_running.wait()) loop.run_until_complete(background_running.wait())
@ -122,6 +126,7 @@ class SchedulerCase(unittest.TestCase):
loop.run_until_complete(scheduler.stop()) loop.run_until_complete(scheduler.stop())
def test_flush(self): def test_flush(self):
loop = self.loop
scheduler = Scheduler(0, _handlers) scheduler = Scheduler(0, _handlers)
expid = _get_expid("EmptyExperiment") expid = _get_expid("EmptyExperiment")
@ -147,10 +152,12 @@ class SchedulerCase(unittest.TestCase):
done.set() done.set()
scheduler.notifier.publish = notify scheduler.notifier.publish = notify
loop = asyncio.get_event_loop()
scheduler.start() scheduler.start()
scheduler.submit("main", expid, 0, None, False) scheduler.submit("main", expid, 0, None, False)
loop.run_until_complete(first_preparing.wait()) loop.run_until_complete(first_preparing.wait())
scheduler.submit("main", expid, 1, None, True) scheduler.submit("main", expid, 1, None, True)
loop.run_until_complete(done.wait()) loop.run_until_complete(done.wait())
loop.run_until_complete(scheduler.stop()) loop.run_until_complete(scheduler.stop())
def tearDown(self):
self.loop.close()

View File

@ -35,9 +35,12 @@ class SyncStructCase(unittest.TestCase):
or "Finished" not in self.test_dict.keys(): or "Finished" not in self.test_dict.keys():
yield from asyncio.sleep(0.5) yield from asyncio.sleep(0.5)
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
def test_recv(self): def test_recv(self):
self.loop = loop = asyncio.new_event_loop() loop = self.loop
asyncio.set_event_loop(loop)
publisher = asyncio.Future() publisher = asyncio.Future()
test_dict = asyncio.Future() test_dict = asyncio.Future()
asyncio.async(start_server(publisher, test_dict)) asyncio.async(start_server(publisher, test_dict))
@ -55,8 +58,8 @@ class SyncStructCase(unittest.TestCase):
test_port)) test_port))
loop.run_until_complete(self.do_recv()) loop.run_until_complete(self.do_recv())
self.assertEqual(self.test_dict, test_vector) self.assertEqual(self.test_dict, test_vector)
def tearDown(self):
self.loop.run_until_complete(self.subscriber.close()) self.loop.run_until_complete(self.subscriber.close())
self.loop.run_until_complete(self.publisher.stop()) self.loop.run_until_complete(self.publisher.stop())
def tearDown(self):
self.loop.close() self.loop.close()

View File

@ -40,6 +40,8 @@ def _call_worker(worker, expid):
def _run_experiment(experiment): def _run_experiment(experiment):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
expid = { expid = {
"file": sys.modules[__name__].__file__, "file": sys.modules[__name__].__file__,
"experiment": experiment, "experiment": experiment,
@ -50,8 +52,8 @@ def _run_experiment(experiment):
} }
worker = Worker(handlers) worker = Worker(handlers)
loop = asyncio.get_event_loop()
loop.run_until_complete(_call_worker(worker, expid)) loop.run_until_complete(_call_worker(worker, expid))
loop.close()
class WatchdogCase(unittest.TestCase): class WatchdogCase(unittest.TestCase):