From 21d88d834535cbbd5d74b81d4d5e94ad019a6361 Mon Sep 17 00:00:00 2001 From: Yann Sionneau Date: Wed, 3 Jun 2015 15:23:34 +0200 Subject: [PATCH 1/4] tests: use a different event loop for each test --- artiq/test/pc_rpc.py | 7 +++++-- artiq/test/scheduler.py | 13 ++++++++++--- artiq/test/sync_struct.py | 11 +++++++---- artiq/test/worker.py | 4 +++- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/artiq/test/pc_rpc.py b/artiq/test/pc_rpc.py index bbd4dde80..c0afcf6f9 100644 --- a/artiq/test/pc_rpc.py +++ b/artiq/test/pc_rpc.py @@ -73,8 +73,10 @@ class RPCCase(unittest.TestCase): remote.close_rpc() 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.close() def test_asyncio_echo(self): self._run_server_and_test(self._loop_asyncio_echo) @@ -96,7 +98,8 @@ class Echo: def run_server(): - loop = asyncio.get_event_loop() + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) try: echo = Echo() server = pc_rpc.Server({"test": echo}) diff --git a/artiq/test/scheduler.py b/artiq/test/scheduler.py index 436f9316a..a70342486 100644 --- a/artiq/test/scheduler.py +++ b/artiq/test/scheduler.py @@ -55,7 +55,12 @@ _handlers = { class SchedulerCase(unittest.TestCase): + def setUp(self): + self.loop = asyncio.new_event_loop() + asyncio.set_event_loop(self.loop) + def test_steps(self): + loop = self.loop scheduler = Scheduler(0, _handlers) expid = _get_expid("EmptyExperiment") @@ -70,7 +75,6 @@ class SchedulerCase(unittest.TestCase): done.set() scheduler.notifier.publish = notify - loop = asyncio.get_event_loop() scheduler.start() # 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()) def test_pause(self): + loop = self.loop scheduler = Scheduler(0, _handlers) expid_bg = _get_expid("BackgroundExperiment") expid = _get_expid("EmptyExperiment") @@ -113,7 +118,6 @@ class SchedulerCase(unittest.TestCase): done.set() scheduler.notifier.publish = notify - loop = asyncio.get_event_loop() scheduler.start() scheduler.submit("main", expid_bg, -99, None, False) loop.run_until_complete(background_running.wait()) @@ -122,6 +126,7 @@ class SchedulerCase(unittest.TestCase): loop.run_until_complete(scheduler.stop()) def test_flush(self): + loop = self.loop scheduler = Scheduler(0, _handlers) expid = _get_expid("EmptyExperiment") @@ -147,10 +152,12 @@ class SchedulerCase(unittest.TestCase): done.set() scheduler.notifier.publish = notify - loop = asyncio.get_event_loop() scheduler.start() scheduler.submit("main", expid, 0, None, False) loop.run_until_complete(first_preparing.wait()) scheduler.submit("main", expid, 1, None, True) loop.run_until_complete(done.wait()) loop.run_until_complete(scheduler.stop()) + + def tearDown(self): + self.loop.close() diff --git a/artiq/test/sync_struct.py b/artiq/test/sync_struct.py index e4e6003a1..d04b5d127 100644 --- a/artiq/test/sync_struct.py +++ b/artiq/test/sync_struct.py @@ -35,9 +35,12 @@ class SyncStructCase(unittest.TestCase): or "Finished" not in self.test_dict.keys(): 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): - self.loop = loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) + loop = self.loop publisher = asyncio.Future() test_dict = asyncio.Future() asyncio.async(start_server(publisher, test_dict)) @@ -55,8 +58,8 @@ class SyncStructCase(unittest.TestCase): test_port)) loop.run_until_complete(self.do_recv()) self.assertEqual(self.test_dict, test_vector) - - def tearDown(self): self.loop.run_until_complete(self.subscriber.close()) self.loop.run_until_complete(self.publisher.stop()) + + def tearDown(self): self.loop.close() diff --git a/artiq/test/worker.py b/artiq/test/worker.py index ecde74104..7f5c7ba0c 100644 --- a/artiq/test/worker.py +++ b/artiq/test/worker.py @@ -40,6 +40,8 @@ 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, @@ -50,8 +52,8 @@ def _run_experiment(experiment): } worker = Worker(handlers) - loop = asyncio.get_event_loop() loop.run_until_complete(_call_worker(worker, expid)) + loop.close() class WatchdogCase(unittest.TestCase): From b8bdce5bd1ff06f95d4d159866defc5c195d0e93 Mon Sep 17 00:00:00 2001 From: Yann Sionneau Date: Wed, 3 Jun 2015 15:40:58 +0200 Subject: [PATCH 2/4] sync_struct test: don't poll, use Event instead --- artiq/test/sync_struct.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/artiq/test/sync_struct.py b/artiq/test/sync_struct.py index d04b5d127..d059727f4 100644 --- a/artiq/test/sync_struct.py +++ b/artiq/test/sync_struct.py @@ -29,11 +29,11 @@ class SyncStructCase(unittest.TestCase): self.test_dict = init return init - @asyncio.coroutine - def do_recv(self): - while not hasattr(self, "test_dict")\ - or "Finished" not in self.test_dict.keys(): - yield from asyncio.sleep(0.5) + def notify(self, mod): + print("mod: {}".format(mod)) + if (mod["action"] == "init" and "Finished" in mod["struct"])\ + or (mod["action"] == "setitem" and mod["key"] == "Finished"): + self.receiving_done.set() def setUp(self): self.loop = asyncio.new_event_loop() @@ -41,6 +41,7 @@ class SyncStructCase(unittest.TestCase): def test_recv(self): loop = self.loop + self.receiving_done = asyncio.Event() publisher = asyncio.Future() test_dict = asyncio.Future() asyncio.async(start_server(publisher, test_dict)) @@ -53,10 +54,11 @@ class SyncStructCase(unittest.TestCase): loop.run_until_complete(write_test_data(test_vector)) asyncio.async(write_test_data(test_dict)) - self.subscriber = sync_struct.Subscriber("test", self.init_test_dict) + self.subscriber = sync_struct.Subscriber("test", self.init_test_dict, + self.notify) loop.run_until_complete(self.subscriber.connect(test_address, test_port)) - loop.run_until_complete(self.do_recv()) + loop.run_until_complete(self.receiving_done.wait()) self.assertEqual(self.test_dict, test_vector) self.loop.run_until_complete(self.subscriber.close()) self.loop.run_until_complete(self.publisher.stop()) From b27254ba8039f7469ed5ac5be3292ca3a8a541cf Mon Sep 17 00:00:00 2001 From: Yann Sionneau Date: Wed, 3 Jun 2015 15:54:50 +0200 Subject: [PATCH 3/4] sync_struct test: test more cases, pep8 fix, remove print --- artiq/test/sync_struct.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/artiq/test/sync_struct.py b/artiq/test/sync_struct.py index d059727f4..a66185ce3 100644 --- a/artiq/test/sync_struct.py +++ b/artiq/test/sync_struct.py @@ -1,5 +1,6 @@ import unittest import asyncio +import numpy as np from artiq.protocols import sync_struct @@ -9,8 +10,14 @@ test_port = 7777 @asyncio.coroutine def write_test_data(test_dict): + test_values = [5, 2.1, None, True, False, + {"a": 5, 2: np.linspace(0, 10, 1)}, + (4, 5), (10,), "ab\nx\"'"] for i in range(10): test_dict[str(i)] = i + for key, value in enumerate(test_values): + test_dict[key] = value + test_dict[1.5] = 1.5 test_dict["Finished"] = True @@ -30,9 +37,8 @@ class SyncStructCase(unittest.TestCase): return init def notify(self, mod): - print("mod: {}".format(mod)) if (mod["action"] == "init" and "Finished" in mod["struct"])\ - or (mod["action"] == "setitem" and mod["key"] == "Finished"): + or (mod["action"] == "setitem" and mod["key"] == "Finished"): self.receiving_done.set() def setUp(self): From 0bf3b7a32beb2df766c6d7cbd91f70ae988f615c Mon Sep 17 00:00:00 2001 From: Yann Sionneau Date: Wed, 3 Jun 2015 15:56:54 +0200 Subject: [PATCH 4/4] conda/setuptools: artiq needs python >= 3.4.3 --- conda/artiq/meta.yaml | 4 ++-- setup.py | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/conda/artiq/meta.yaml b/conda/artiq/meta.yaml index dec979308..9a2a3daec 100644 --- a/conda/artiq/meta.yaml +++ b/conda/artiq/meta.yaml @@ -27,13 +27,13 @@ build: requirements: build: - - python + - python >=3.4.3 - setuptools - numpy - migen - pyelftools run: - - python + - python >=3.4.3 - llvmlite-or1k - scipy - numpy diff --git a/setup.py b/setup.py index ff98ab0ba..b783092fb 100755 --- a/setup.py +++ b/setup.py @@ -1,7 +1,10 @@ #!/usr/bin/env python3 from setuptools import setup, find_packages +import sys +if sys.version_info[:3] < (3, 4, 3): + raise Exception("You need at least Python 3.4.3 to run ARTIQ") requirements = [ "sphinx", "sphinx-argparse", "pyserial", "numpy", "scipy",