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
|
2015-08-28 00:23:26 +08:00
|
|
|
import os
|
2015-03-12 01:26:04 +08:00
|
|
|
from time import sleep
|
|
|
|
|
2016-01-26 06:49:08 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
2015-10-13 01:11:57 +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):
|
2015-06-04 19:44:07 +08:00
|
|
|
expid = {
|
2015-10-20 18:11:50 +08:00
|
|
|
"log_level": logging.WARNING,
|
2015-06-04 19:44:07 +08:00
|
|
|
"file": sys.modules[__name__].__file__,
|
2015-07-15 17:08:12 +08:00
|
|
|
"class_name": class_name,
|
2015-06-04 19:44:07 +08:00
|
|
|
"arguments": dict()
|
|
|
|
}
|
|
|
|
loop = asyncio.get_event_loop()
|
2016-01-27 03:31:42 +08:00
|
|
|
worker = Worker({})
|
2015-06-04 19:44:07 +08:00
|
|
|
loop.run_until_complete(_call_worker(worker, expid))
|
2015-03-12 01:26:04 +08:00
|
|
|
|
|
|
|
|
2015-10-13 01:11:57 +08:00
|
|
|
class WorkerCase(unittest.TestCase):
|
2015-06-04 19:40:13 +08:00
|
|
|
def setUp(self):
|
2015-08-28 00:23:26 +08:00
|
|
|
if os.name == "nt":
|
|
|
|
self.loop = asyncio.ProactorEventLoop()
|
|
|
|
else:
|
|
|
|
self.loop = asyncio.new_event_loop()
|
2015-06-04 19:40:13 +08:00
|
|
|
asyncio.set_event_loop(self.loop)
|
|
|
|
|
2015-10-13 01:11:57 +08:00
|
|
|
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-10-13 01:11:57 +08:00
|
|
|
|
2015-03-12 01:26:04 +08:00
|
|
|
def test_watchdog_no_timeout(self):
|
2015-06-04 11:22:03 +08:00
|
|
|
_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")
|
2015-06-04 19:40:13 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.loop.close()
|