artiq/artiq/test/worker.py

79 lines
1.8 KiB
Python
Raw Normal View History

2015-03-12 01:26:04 +08:00
import unittest
import asyncio
import sys
from time import sleep
from artiq import *
2015-03-12 02:07:04 +08:00
from artiq.master.worker import *
2015-03-12 01:26:04 +08:00
class WatchdogNoTimeout(Experiment, AutoDB):
def run(self):
for i in range(10):
2015-04-28 23:23:59 +08:00
with watchdog(0.5*s):
2015-03-12 01:26:04 +08:00
sleep(0.1)
class WatchdogTimeout(Experiment, AutoDB):
def run(self):
2015-04-28 23:23:59 +08:00
with watchdog(0.1*s):
2015-03-12 01:26:04 +08:00
sleep(100.0)
2015-03-12 02:07:04 +08:00
class WatchdogTimeoutInBuild(Experiment, AutoDB):
def build(self):
2015-04-28 23:23:59 +08:00
with watchdog(0.1*s):
2015-03-12 02:07:04 +08:00
sleep(100.0)
def run(self):
pass
2015-03-12 01:26:04 +08:00
@asyncio.coroutine
def _call_worker(worker, expid):
2015-03-12 01:26:04 +08:00
try:
yield from worker.prepare(0, "main", expid, 0)
2015-03-12 01:26:04 +08:00
yield from worker.run()
yield from worker.analyze()
finally:
yield from worker.close()
def _run_experiment(experiment):
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()
2015-03-12 01:26:04 +08:00
class WatchdogCase(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
2015-03-12 01:26:04 +08:00
def test_watchdog_no_timeout(self):
_run_experiment("WatchdogNoTimeout")
2015-03-12 01:26:04 +08:00
def test_watchdog_timeout(self):
with self.assertRaises(WorkerWatchdogTimeout):
_run_experiment("WatchdogTimeout")
2015-03-12 02:07:04 +08:00
def test_watchdog_timeout_in_build(self):
with self.assertRaises(WorkerWatchdogTimeout):
_run_experiment("WatchdogTimeoutInBuild")
def tearDown(self):
self.loop.close()