Skip to content

Commit 5fd0632

Browse files
committed
Expose activate() in remote server itself.
Eases getting information about the port that is used
1 parent c6f718f commit 5fd0632

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/robotremoteserver.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,22 @@ def server_port(self):
101101
"""
102102
return self._server.server_address[1]
103103

104+
def activate(self):
105+
"""Bind port and activate the server but do not yet start serving.
106+
107+
:return Port number that the server is going to use. This is the
108+
actual port to use, even if the initially given port is 0.
109+
"""
110+
return self._server.activate()
111+
104112
def serve(self, log=True):
105113
"""Start the server and wait for it to be stopped.
106114
107115
:param log: When ``True``, print messages about start and stop to
108116
the console.
109117
118+
Automatically activates the server if it is not activated already.
119+
110120
If this method is executed in the main thread, automatically registers
111121
signals SIGINT, SIGTERM and SIGHUP to stop the server.
112122
@@ -186,23 +196,14 @@ def __init__(self, host, port):
186196
self._activated = False
187197
self._stopper_thread = None
188198

189-
# TODO: Expose in Remote server itself
190199
def activate(self):
191-
"""Activate server but do not yet start serving.
192-
193-
Most importantly, port is bind and :attr:`server_address` contains
194-
the actual port number to use also if the initially given port is 0.
195-
"""
196200
if not self._activated:
197201
self.server_bind()
198202
self.server_activate()
199203
self._activated = True
204+
return self.server_address[1]
200205

201206
def serve(self):
202-
"""Start serving until the server is stopped.
203-
204-
Activates the server automatically if it is not done before.
205-
"""
206207
self.activate()
207208
try:
208209
self.serve_forever()
@@ -213,6 +214,7 @@ def serve(self):
213214
self.server_close()
214215
if self._stopper_thread:
215216
self._stopper_thread.join()
217+
self._stopper_thread = None
216218

217219
def stop(self):
218220
self._stopper_thread = threading.Thread(target=self.shutdown)

test/utest/test_serve.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ def test_serve(self):
3333
self._wait_until_stopped(uri)
3434
self.assertEqual(test_remote_server(uri, log=False), False)
3535

36+
def test_activate(self):
37+
port = self.server.activate()
38+
self.assertNotEqual(port, 0)
39+
self.assertEqual(port, self.server.server_port)
40+
with self._server_thread():
41+
self.assertEqual(port, self.server.server_port)
42+
self.server.stop()
43+
3644
@contextmanager
3745
def _server_thread(self):
3846
thread = threading.Thread(target=self.server.serve,

0 commit comments

Comments
 (0)