diff --git a/pylabrobot/liquid_handling/backends/simulation/simulation_tests.py b/pylabrobot/liquid_handling/backends/simulation/simulation_tests.py index b584246a2c..7f3aec68e0 100644 --- a/pylabrobot/liquid_handling/backends/simulation/simulation_tests.py +++ b/pylabrobot/liquid_handling/backends/simulation/simulation_tests.py @@ -64,7 +64,7 @@ def test_get_index_html(self): """ Test that the index.html file is returned. """ r = requests.get("http://localhost:1337/", timeout=10) self.assertEqual(r.status_code, 200) - self.assertEqual(r.headers["Content-Type"], "text/html") + self.assertIn(r.headers["Content-Type"], ["text/html", "text/html; charset=utf-8"]) async def test_connect(self): await self.client.send('{"event": "ready"}') diff --git a/pylabrobot/liquid_handling/backends/simulation/simulator_backend.py b/pylabrobot/liquid_handling/backends/simulation/simulator_backend.py index af450cb5ab..a364e96e14 100644 --- a/pylabrobot/liquid_handling/backends/simulation/simulator_backend.py +++ b/pylabrobot/liquid_handling/backends/simulation/simulator_backend.py @@ -42,7 +42,7 @@ class SimulatorBackend(WebSocketBackend): >>> from pylabrobot.liquid_handling.liquid_handler import LiquidHandler >>> sb = simulation.SimulatorBackend() >>> lh = LiquidHandler(backend=sb) - >>> lh.setup() + >>> await lh.setup() INFO:websockets.server:server listening on 127.0.0.1:2121 INFO:pyhamilton.liquid_handling.backends.simulation.simulation:Simulation server started at http://127.0.0.1:2121 @@ -127,11 +127,11 @@ def _run_file_server(self): def start_server(): # try to start the server. If the port is in use, try with another port until it succeeds. - og_path = os.getcwd() - os.chdir(path) # only within thread. - class QuietSimpleHTTPRequestHandler(http.server.SimpleHTTPRequestHandler): """ A simple HTTP request handler that does not log requests. """ + def __init__(self, *args, **kwargs): + super().__init__(*args, directory=path, **kwargs) + def log_message(self, format, *args): # pylint: disable=redefined-builtin pass @@ -148,8 +148,6 @@ def log_message(self, format, *args): self.httpd.serve_forever() - os.chdir(og_path) - self._fst = threading.Thread(name="simulation_fs", target=start_server, daemon=True) self.fst.start() diff --git a/pylabrobot/liquid_handling/backends/websocket.py b/pylabrobot/liquid_handling/backends/websocket.py index e55c9043a5..9035ce1a0f 100644 --- a/pylabrobot/liquid_handling/backends/websocket.py +++ b/pylabrobot/liquid_handling/backends/websocket.py @@ -266,7 +266,7 @@ def start_loop(): lock = threading.Lock() lock.acquire() # pylint: disable=consider-using-with self._loop = asyncio.new_event_loop() - self._t = threading.Thread(target=start_loop) + self._t = threading.Thread(target=start_loop, daemon=True) self.t.start() while lock.locked(): diff --git a/pylabrobot/liquid_handling/liquid_handler.py b/pylabrobot/liquid_handling/liquid_handler.py index 3db24fb28f..bfe50211ea 100644 --- a/pylabrobot/liquid_handling/liquid_handler.py +++ b/pylabrobot/liquid_handling/liquid_handler.py @@ -95,7 +95,7 @@ async def setup(self): for resource in self.deck.children: self.resource_assigned_callback(resource) - super().setup() + await super().setup() def update_head_state(self, state: Dict[int, Optional[Tip]]): """ Update the state of the liquid handler head. diff --git a/pylabrobot/machine.py b/pylabrobot/machine.py index bd354392b6..8001215a0c 100644 --- a/pylabrobot/machine.py +++ b/pylabrobot/machine.py @@ -46,8 +46,10 @@ def setup_finished(self) -> bool: return self._setup_finished async def setup(self): - self.backend.setup() + await self.backend.setup() + self._setup_finished = True @need_setup_finished async def stop(self): - self.backend.stop() + await self.backend.stop() + self._setup_finished = False diff --git a/pylabrobot/server/liquid_handling_api_tests.py b/pylabrobot/server/liquid_handling_api_tests.py index 180c00313b..7ce64155af 100644 --- a/pylabrobot/server/liquid_handling_api_tests.py +++ b/pylabrobot/server/liquid_handling_api_tests.py @@ -1,3 +1,4 @@ +import time from typing import cast import unittest @@ -53,6 +54,7 @@ def test_setup(self): # TODO: Figure out how we can configure LH self.assertIn(response.json.get("status"), {"running", "succeeded"}) + time.sleep(0.1) assert self.lh.setup_finished def test_stop(self): @@ -111,6 +113,7 @@ def setUp(self) -> None: assert self.lh.deck.resources == deck.resources client.post(self.base_url + "/setup") + time.sleep(0.5) def test_tip_pickup(self): with self.app.test_client() as client: @@ -125,7 +128,6 @@ def test_tip_pickup(self): "tip": serialize(tip), "offset": None, }], "use_channels": [0]}) - print(response) self.assertIn(response.json.get("status"), {"running", "succeeded"}) self.assertEqual(response.status_code, 200)