artiq/artiq/test/test_worker.py

110 lines
2.5 KiB
Python
Raw Normal View History

2015-03-12 01:26:04 +08:00
import unittest
2015-10-20 18:11:50 +08:00
import logging
2015-03-12 01:26:04 +08:00
import asyncio
import sys
import os
2015-03-12 01:26:04 +08:00
from time import sleep
from artiq.experiment import *
2015-03-12 02:07:04 +08:00
from artiq.master.worker import *
2015-03-12 01:26:04 +08:00
class SimpleExperiment(EnvExperiment):
def build(self):
pass
def run(self):
pass
class ExceptionTermination(EnvExperiment):
def build(self):
pass
def run(self):
raise TypeError
2015-07-14 04:08:20 +08:00
class WatchdogNoTimeout(EnvExperiment):
def build(self):
pass
2015-03-12 01:26:04 +08:00
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)
2015-07-14 04:08:20 +08:00
class WatchdogTimeout(EnvExperiment):
def build(self):
pass
2015-03-12 01:26:04 +08:00
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-07-14 04:08:20 +08:00
class WatchdogTimeoutInBuild(EnvExperiment):
2015-03-12 02:07:04 +08:00
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-10-03 19:28:57 +08:00
async def _call_worker(worker, expid):
2015-03-12 01:26:04 +08:00
try:
2015-10-03 19:28:57 +08:00
await worker.build(0, "main", None, expid, 0)
await worker.prepare()
await worker.run()
await worker.analyze()
2015-03-12 01:26:04 +08:00
finally:
2015-10-03 19:28:57 +08:00
await worker.close()
2015-03-12 01:26:04 +08:00
2015-07-15 17:08:12 +08:00
def _run_experiment(class_name):
expid = {
2015-10-20 18:11:50 +08:00
"log_level": logging.WARNING,
"file": sys.modules[__name__].__file__,
2015-07-15 17:08:12 +08:00
"class_name": class_name,
"arguments": dict()
}
loop = asyncio.get_event_loop()
worker = Worker({})
loop.run_until_complete(_call_worker(worker, expid))
2015-03-12 01:26:04 +08:00
class WorkerCase(unittest.TestCase):
def setUp(self):
if os.name == "nt":
self.loop = asyncio.ProactorEventLoop()
else:
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
def test_simple_run(self):
_run_experiment("SimpleExperiment")
def test_exception(self):
2016-03-02 17:12:22 +08:00
with self.assertLogs() as logs:
with self.assertRaises(WorkerInternalException):
_run_experiment("ExceptionTermination")
self.assertEqual(len(logs.records), 1)
self.assertIn("Terminating with exception (TypeError)",
logs.output[0])
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()