Skip to content

Commit 85c9125

Browse files
committed
fix threading bugs
1 parent 8298dcb commit 85c9125

File tree

6 files changed

+14
-12
lines changed

6 files changed

+14
-12
lines changed

pylabrobot/liquid_handling/backends/simulation/simulation_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_get_index_html(self):
6464
""" Test that the index.html file is returned. """
6565
r = requests.get("http://localhost:1337/", timeout=10)
6666
self.assertEqual(r.status_code, 200)
67-
self.assertEqual(r.headers["Content-Type"], "text/html")
67+
self.assertIn(r.headers["Content-Type"], ["text/html", "text/html; charset=utf-8"])
6868

6969
async def test_connect(self):
7070
await self.client.send('{"event": "ready"}')

pylabrobot/liquid_handling/backends/simulation/simulator_backend.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class SimulatorBackend(WebSocketBackend):
4242
>>> from pylabrobot.liquid_handling.liquid_handler import LiquidHandler
4343
>>> sb = simulation.SimulatorBackend()
4444
>>> lh = LiquidHandler(backend=sb)
45-
>>> lh.setup()
45+
>>> await lh.setup()
4646
INFO:websockets.server:server listening on 127.0.0.1:2121
4747
INFO:pyhamilton.liquid_handling.backends.simulation.simulation:Simulation server started at
4848
http://127.0.0.1:2121
@@ -127,11 +127,11 @@ def _run_file_server(self):
127127

128128
def start_server():
129129
# try to start the server. If the port is in use, try with another port until it succeeds.
130-
og_path = os.getcwd()
131-
os.chdir(path) # only within thread.
132-
133130
class QuietSimpleHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
134131
""" A simple HTTP request handler that does not log requests. """
132+
def __init__(self, *args, **kwargs):
133+
super().__init__(*args, directory=path, **kwargs)
134+
135135
def log_message(self, format, *args):
136136
# pylint: disable=redefined-builtin
137137
pass
@@ -148,8 +148,6 @@ def log_message(self, format, *args):
148148

149149
self.httpd.serve_forever()
150150

151-
os.chdir(og_path)
152-
153151
self._fst = threading.Thread(name="simulation_fs", target=start_server, daemon=True)
154152
self.fst.start()
155153

pylabrobot/liquid_handling/backends/websocket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def start_loop():
266266
lock = threading.Lock()
267267
lock.acquire() # pylint: disable=consider-using-with
268268
self._loop = asyncio.new_event_loop()
269-
self._t = threading.Thread(target=start_loop)
269+
self._t = threading.Thread(target=start_loop, daemon=True)
270270
self.t.start()
271271

272272
while lock.locked():

pylabrobot/liquid_handling/liquid_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ async def setup(self):
9595
for resource in self.deck.children:
9696
self.resource_assigned_callback(resource)
9797

98-
super().setup()
98+
await super().setup()
9999

100100
def update_head_state(self, state: Dict[int, Optional[Tip]]):
101101
""" Update the state of the liquid handler head.

pylabrobot/machine.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ def setup_finished(self) -> bool:
4646
return self._setup_finished
4747

4848
async def setup(self):
49-
self.backend.setup()
49+
await self.backend.setup()
50+
self._setup_finished = True
5051

5152
@need_setup_finished
5253
async def stop(self):
53-
self.backend.stop()
54+
await self.backend.stop()
55+
self._setup_finished = False

pylabrobot/server/liquid_handling_api_tests.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
from typing import cast
23
import unittest
34

@@ -53,6 +54,7 @@ def test_setup(self): # TODO: Figure out how we can configure LH
5354

5455
self.assertIn(response.json.get("status"), {"running", "succeeded"})
5556

57+
time.sleep(0.1)
5658
assert self.lh.setup_finished
5759

5860
def test_stop(self):
@@ -111,6 +113,7 @@ def setUp(self) -> None:
111113
assert self.lh.deck.resources == deck.resources
112114

113115
client.post(self.base_url + "/setup")
116+
time.sleep(0.5)
114117

115118
def test_tip_pickup(self):
116119
with self.app.test_client() as client:
@@ -125,7 +128,6 @@ def test_tip_pickup(self):
125128
"tip": serialize(tip),
126129
"offset": None,
127130
}], "use_channels": [0]})
128-
print(response)
129131
self.assertIn(response.json.get("status"), {"running", "succeeded"})
130132
self.assertEqual(response.status_code, 200)
131133

0 commit comments

Comments
 (0)