bpd tests: use mp.Queue to communicate assigned port
Use a `multiprocessing.Queue` instead of a `multiprocessing.Value` to avoid the
manual polling/timeout handling.
TODO: Strangely Listener seems to be constructed twice. Only the second one is
used. Fix that and then remove the code working around it.
@@ -247,13 +247,14 @@ def start_server(args, assigned_port, listener_patch):
"""
def listener_wrap(host, port):
"""Wrap `bluelet.Listener`, writing the port to `assigend_port`.
-
- `bluelet.Listener` has previously been saved to
- `bluelet_listener` as this function will replace it at its
- original location.
"""
+ # `bluelet.Listener` has previously been saved to
+ # `bluelet_listener` as this function will replace it at its
+ # original location.
listener = bluelet_listener(host, port)
- assigned_port.value = listener.sock.getsockname()[1]
+ # read which port has been assigned by the OS
+ # TODO: change to put_nowait. There should always be a free slot.
+ assigned_port.put(listener.sock.getsockname()[1])
return listener
listener_patch.side_effect = listener_wrap
@@ -301,7 +302,7 @@ class BPDTestHelper(unittest.TestCase, TestHelper):
config_file.close()
# Fork and launch BPD in the new process:
- assigned_port = mp.Value("I", 0)
+ assigned_port = mp.Queue(1)
server = mp.Process(target=start_server, args=([
'--library', self.config['library'].as_filename(),
'--directory', py3_path(self.libdir),
@@ -311,17 +312,12 @@ class BPDTestHelper(unittest.TestCase, TestHelper):
server.start()
try:
- # Wait until the socket is connected:
- for _ in range(20):
- if assigned_port.value != 0:
- # read which port has been assigned by the OS
- port = assigned_port.value
- break
- time.sleep(0.01)
- else:
- raise RuntimeError(
- 'Timed out waiting for the BPD server to start'
- )
+ # TODO: ugly hack. remove
+ print("ignoring port in queue:", assigned_port.get(timeout=2))
+ # Wait until the socket is connected
+ port = assigned_port.get(timeout=2)
+ print("test bpd server on port", port)
+ time.sleep(0.1)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
