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_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_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_run_sequence(self) -> None: SEQUENCE_CONTENTS = """ --- sequence: - experiment_type: labby.tests.test_server.TestExperiment - experiment_type: labby.tests.test_server.TestExperiment """ with patch_file_contents( "sequence/test.yml", SEQUENCE_CONTENTS ), patch_file_contents("output/test/000.csv") as output_0, patch_file_contents( "output/test/001.csv" ) as output_1, patch( "os.makedirs" ) as makedirs, patch_time( "2020-08-08" ): self.client.run_sequence("sequence/test.yml") while True: sequence_status = self.client.experiment_status().sequence_status if sequence_status and sequence_status.is_finished(): break time.sleep(0) makedirs.assert_called_with(PosixPath("output/test/"), exist_ok=True) self.assertEqual(len(output_0.write.call_args_list), 4) output_0.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"), ] ) output_1.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 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 test_server_start(self, _makedirs: MagicMock, _fork_mock: MagicMock) -> None: with labby_config(LABBY_CONFIG), patch_file_contents(".labby/pid"): (rc, stdout, _stderr) = self.main(["server", "start"]) self.assertEqual(rc, 0)