def test_existing_pid(self) -> None: config = Config(LABBY_CONFIG_YAML) with patch_file_contents(".labby/pid", "12345"): server = Server(config) server_info = server.start() self.assertTrue(server_info.existing) self.assertEqual(server_info.pid, 12345)
def test_published_messages(self) -> None: config = Config(LABBY_CONFIG_YAML) sequence = ExperimentSequence("./sequences/seq.yaml", SEQUENCE_YAML) runner = ExperimentRunner(config, sequence) with Sub0(dial=runner.subscription_address) as sub: sub.subscribe(b"") received_messages: List[ExperimentSequenceStatus] = [] with patch_time("2020-08-08"), patch_file_contents( "output/seq/000.csv"), patch("os.makedirs"): runner.start() while True: msg = sub.recv() status = ExperimentSequenceStatus.from_msgpack(msg) received_messages.append(status) if status.is_finished(): break runner.join() self.assertEqual( received_messages, [ ExperimentSequenceStatus(experiments=[ ExperimentStatus( name="000", state=ExperimentState.RUNNING, progress=0.0, ) ]), ExperimentSequenceStatus(experiments=[ ExperimentStatus( name="000", state=ExperimentState.RUNNING, progress=0.0, ) ]), ExperimentSequenceStatus(experiments=[ ExperimentStatus( name="000", state=ExperimentState.RUNNING, progress=0.5, ) ]), ExperimentSequenceStatus(experiments=[ ExperimentStatus( name="000", state=ExperimentState.RUNNING, progress=1.0, ) ]), ExperimentSequenceStatus(experiments=[ ExperimentStatus( name="000", state=ExperimentState.FINISHED, progress=1.0, ) ]), ], )
def test_parent_process_on_start(self, makedirs: MagicMock, _fork_mock: MagicMock) -> None: config = Config(LABBY_CONFIG_YAML) with patch_file_contents(".labby/pid") as pidfile: server = Server(config) server_info = server.start() self.assertFalse(server_info.existing) self.assertEqual(server_info.pid, FAKE_PID) makedirs.assert_called_with(".labby", exist_ok=True) pidfile.write.assert_called_once_with(str(FAKE_PID))
def test_basic_config(self, _serial_port_mock: Mock) -> None: config = Config(""" --- devices: - name: "zup-6-132" type: power_supply driver: labby.hw.tdklambda.power_supply.ZUP args: port: "/dev/ttyUSB0" baudrate: 9600 address: 1 """) devices = config.get_devices() self.assertEqual(len(devices), 1) device = config.devices[0] assert isinstance(device, tdklambda.power_supply.ZUP) self.assertEqual(device.port, "/dev/ttyUSB0") self.assertEqual(device.baudrate, 9600) self.assertEqual(device.address, 1)
def setUp(self) -> None: auto_discover_drivers() config: Config = Config(LABBY_CONFIG_YAML) server: Server = Server(config) def _handle(msg: EncodedData) -> None: response_bytes = ServerRequest.handle_from_msgpack(server, msg) self.req_mock.return_value.recv.return_value = response_bytes self.req_patch = patch("labby.client.Req0") self.req_mock = self.req_patch.start() self.req_mock.return_value.send.side_effect = _handle self.client = Client("foobar")
def test_child_process_on_start( self, rep0_mock: MagicMock, remove_mock: MagicMock, _makedirs: MagicMock, _fork_mock: MagicMock, ) -> None: config = Config(LABBY_CONFIG_YAML) with patch_file_contents(".labby/pid"): rep0_mock.return_value.__enter__.return_value.recv.return_value = ( b"HaltRequest:" + cast(bytes, HaltRequest().to_msgpack()) ) server = Server(config) with self.assertRaises(SystemExit): server.start() remove_mock.assert_called_once_with(".labby/pid")
def test_experiment_output(self) -> None: config = Config(LABBY_CONFIG_YAML) sequence = ExperimentSequence("./sequences/seq.yaml", SEQUENCE_YAML) runner = ExperimentRunner(config, sequence) with patch_time("2020-08-08"), patch_file_contents( "output/seq/000.csv") as output, patch( "os.makedirs") as makedirs: runner.start() runner.join() makedirs.assert_called_with(PosixPath("output/seq/"), exist_ok=True) self.assertEqual(len(output.write.call_args_list), 4) output.write.assert_has_calls([ call("seconds,voltage\n"), call("0.0,15.0\n"), call("0.5,15.0\n"), call("1.0,15.0\n"), ])
def run(cls, trigger: str, argv: Sequence[str]) -> int: try: command_klass = ALL_COMMANDS[trigger] # pyre-ignore[16]: command_klass has no __orig_bases__ attribute args_klass = get_args(command_klass.__orig_bases__[0])[0] args = args_klass(prog=f"labby {trigger}").parse_args(argv) auto_discover_drivers() with open(args.config, "r") as config_file: config = Config(config_file.read()) # pyre-ignore[45]: cannot instantiate Command with abstract method command = command_klass(config) return command.main(args) except pynng.exceptions.Timeout: # this had to be an inline import so the tests would use the # WASABI_LOG_FRIENDLY env variable correctly ¯\_(ツ)_/¯ from wasabi import msg msg.fail("Timeout") msg.text("The labby server did not respond. Are you sure it is started?") return 1