|
| 1 | +import unittest |
| 2 | +import asyncio |
| 3 | +import sys |
| 4 | +from time import sleep |
| 5 | + |
| 6 | +from artiq import * |
| 7 | +from artiq.master.scheduler import Scheduler |
| 8 | + |
| 9 | + |
| 10 | +class EmptyExperiment(Experiment, AutoDB): |
| 11 | + def run(self): |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +class BackgroundExperiment(Experiment, AutoDB): |
| 16 | + def run(self): |
| 17 | + while True: |
| 18 | + self.scheduler.pause() |
| 19 | + sleep(0.2) |
| 20 | + |
| 21 | + |
| 22 | +def _get_expid(name): |
| 23 | + return { |
| 24 | + "file": sys.modules[__name__].__file__, |
| 25 | + "experiment": name, |
| 26 | + "arguments": dict() |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | +def _get_basic_steps(rid, expid): |
| 31 | + return [ |
| 32 | + {"action": "setitem", "key": rid, "value": |
| 33 | + {"pipeline": "main", "status": "pending", "priority": 0, |
| 34 | + "expid": expid, "due_date": None}, "path": []}, |
| 35 | + {"action": "setitem", "key": "status", "value": "preparing", |
| 36 | + "path": [rid]}, |
| 37 | + {"action": "setitem", "key": "status", "value": "prepare_done", |
| 38 | + "path": [rid]}, |
| 39 | + {"action": "setitem", "key": "status", "value": "running", |
| 40 | + "path": [rid]}, |
| 41 | + {"action": "setitem", "key": "status", "value": "run_done", |
| 42 | + "path": [rid]}, |
| 43 | + {"action": "setitem", "key": "status", "value": "analyzing", |
| 44 | + "path": [rid]}, |
| 45 | + {"action": "setitem", "key": "status", "value": "analyze_done", |
| 46 | + "path": [rid]}, |
| 47 | + {"action": "delitem", "key": rid, "path": []} |
| 48 | + ] |
| 49 | + |
| 50 | + |
| 51 | +_handlers = { |
| 52 | + "init_rt_results": lambda description: None |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +class SchedulerCase(unittest.TestCase): |
| 57 | + def test_steps(self): |
| 58 | + scheduler = Scheduler(0, _handlers) |
| 59 | + expid = _get_expid("EmptyExperiment") |
| 60 | + |
| 61 | + expect = _get_basic_steps(0, expid) |
| 62 | + done = asyncio.Event() |
| 63 | + expect_idx = 0 |
| 64 | + def notify(notifier, mod): |
| 65 | + nonlocal expect_idx |
| 66 | + self.assertEqual(mod, expect[expect_idx]) |
| 67 | + expect_idx += 1 |
| 68 | + if expect_idx >= len(expect): |
| 69 | + done.set() |
| 70 | + scheduler.notifier.publish = notify |
| 71 | + |
| 72 | + loop = asyncio.get_event_loop() |
| 73 | + scheduler.start() |
| 74 | + scheduler.submit("main", expid, 0, None) |
| 75 | + loop.run_until_complete(done.wait()) |
| 76 | + loop.run_until_complete(scheduler.stop()) |
| 77 | + |
| 78 | + def test_pause(self): |
| 79 | + scheduler = Scheduler(0, _handlers) |
| 80 | + expid_bg = _get_expid("BackgroundExperiment") |
| 81 | + expid = _get_expid("EmptyExperiment") |
| 82 | + |
| 83 | + expect = _get_basic_steps(1, expid) |
| 84 | + background_running = asyncio.Event() |
| 85 | + done = asyncio.Event() |
| 86 | + expect_idx = 0 |
| 87 | + def notify(notifier, mod): |
| 88 | + nonlocal expect_idx |
| 89 | + if mod == {"path": [0], |
| 90 | + "value": "running", |
| 91 | + "key": "status", |
| 92 | + "action": "setitem"}: |
| 93 | + background_running.set() |
| 94 | + if mod["path"] == [1] or (mod["path"] == [] and mod["key"] == 1): |
| 95 | + self.assertEqual(mod, expect[expect_idx]) |
| 96 | + expect_idx += 1 |
| 97 | + if expect_idx >= len(expect): |
| 98 | + done.set() |
| 99 | + scheduler.notifier.publish = notify |
| 100 | + |
| 101 | + loop = asyncio.get_event_loop() |
| 102 | + scheduler.start() |
| 103 | + scheduler.submit("main", expid_bg, -99, None) |
| 104 | + loop.run_until_complete(background_running.wait()) |
| 105 | + scheduler.submit("main", expid, 0, None) |
| 106 | + loop.run_until_complete(done.wait()) |
| 107 | + loop.run_until_complete(scheduler.stop()) |
0 commit comments