From e037b930d8e160e55f5b4e7230b34748e5002cfe Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Wed, 11 Mar 2015 18:26:04 +0100 Subject: [PATCH] test: add worker unittest --- artiq/test/worker.py | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 artiq/test/worker.py diff --git a/artiq/test/worker.py b/artiq/test/worker.py new file mode 100644 index 000000000..51bd35597 --- /dev/null +++ b/artiq/test/worker.py @@ -0,0 +1,58 @@ +import unittest +import asyncio +import sys +from time import sleep + +from artiq import * +from artiq.master.worker import * + + +class WatchdogNoTimeout(Experiment, AutoDB): + def run(self): + for i in range(10): + with self.scheduler.watchdog(0.5*s): + sleep(0.1) + + +class WatchdogTimeout(Experiment, AutoDB): + def run(self): + with self.scheduler.watchdog(0.1*s): + sleep(100.0) + + +@asyncio.coroutine +def _call_worker(worker, run_params): + yield from worker.prepare(0, run_params) + try: + yield from worker.run() + yield from worker.analyze() + finally: + yield from worker.close() + + +def _run_experiment(experiment): + run_params = { + "file": sys.modules[__name__].__file__, + "experiment": experiment, + "arguments": dict() + } + handlers = { + "init_rt_results": lambda description: None + } + + worker = Worker(handlers) + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + try: + loop.run_until_complete(_call_worker(worker, run_params)) + finally: + loop.close() + + +class WatchdogCase(unittest.TestCase): + def test_watchdog_no_timeout(self): + _run_experiment("WatchdogNoTimeout") + + def test_watchdog_timeout(self): + with self.assertRaises(WorkerWatchdogTimeout): + _run_experiment("WatchdogTimeout")