Skip to content

Commit f69cf34

Browse files
committed
Docmenting starting server on background #22
1 parent 8d5152e commit f69cf34

File tree

2 files changed

+108
-41
lines changed

2 files changed

+108
-41
lines changed

README.rst

Lines changed: 91 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,25 @@ Remote server configuration
8181

8282
The remote server accepts following configuration parameters:
8383

84-
============== ================ ========================================
85-
Argument Default Explanation
86-
============== ================ ========================================
87-
``library`` Test library instance or module to host. Mandatory argument.
88-
``host`` ``'127.0.0.1'`` Address to listen. Use ``'0.0.0.0'`` to listen to all available interfaces.
89-
``port`` ``8270`` Port to listen. Use ``0`` to select a free port automatically. Can be given as an integer or as a string.
90-
``port_file`` ``None`` File to write port that is used. ``None`` means no such file is written.
91-
``allow_stop`` ``True`` Allow/disallow stopping the server using ``Stop Remote Server`` keyword.
92-
============== ================ ========================================
84+
===================== ================= ========================================
85+
Argument Default Explanation
86+
===================== ================= ========================================
87+
``library`` Test library instance or module to host. Mandatory argument.
88+
``host`` ``'127.0.0.1'`` Address to listen. Use ``'0.0.0.0'`` to listen to all available interfaces.
89+
``port`` ``8270`` Port to listen. Use ``0`` to select a free port automatically. Can be given as an integer or as a string.
90+
``port_file`` ``None`` File to write port that is used. ``None`` means no such file is written.
91+
``allow_stop`` ``'DEPRECATED'`` DEPRECATED. User ``allow_remote_stop`` instead.
92+
``serve`` ``True`` If ``True``, start the server automatically and wait for it to be stopped. If ``False``, server can be started using ``serve`` or ``start`` methods. New in version 1.1.
93+
``allow_remote_stop`` ``True`` Allow/disallow stopping the server using ``Stop Remote Server`` keyword and ``stop_remote_server`` XML-RPC method. New in version 1.1.
94+
===================== ================= ========================================
9395

9496
Address and port that are used are printed to the console where the server is
9597
started. Writing port to a file by using ``port_file`` argument is especially
9698
useful when the server selects a free port automatically. Other tools can then
9799
easily read the active port from the file. If the file is removed prior to
98100
starting the server, tools can also wait until the file exists to know that
99-
the server is up and running.
101+
the server is up and running. Starting from version 1.1, the server removes
102+
the port file automatically.
100103

101104
Example:
102105

@@ -106,23 +109,23 @@ Example:
106109
from mylibrary import MyLibrary
107110

108111
RobotRemoteServer(MyLibrary(), host='10.0.0.42', port=0,
109-
port_file='/tmp/remote-port.txt', allow_stop=False)
112+
port_file='/tmp/remote-port.txt')
110113

111-
Testing is server running
112-
-------------------------
114+
Starting from versoin 1.1, the server can also first be initialized and started
115+
afterwards:
113116

114-
Starting from version 1.0.1 , ``robotremoteserver`` module supports testing is
115-
a remote server running. This can be accomplished by running the module as
116-
a script with ``test`` argument and an optional URI::
117+
.. sourcecode:: python
117118

118-
$ python -m robotremoteserver test
119-
Remote server running at http://127.0.0.1:8270.
120-
$ python -m robotremoteserver test http://10.0.0.42:57347
121-
No remote server running at http://10.0.0.42:57347.
119+
server = RobotRemoteServer(MyLibrary(), host='10.0.0.42', port=0,
120+
port_file='/tmp/remote-port.txt', serve=False)
121+
server.serve()
122+
123+
The above is totally equivalent to the earlier example and both of them result
124+
with the server starting and running until it is `explicitly stopped`__.
125+
Alternatively it is possible to `start the server on background`__.
122126

123-
.. tip:: As discussed below, using ``stop`` instead of ``test`` allows stopping
124-
the server. Both testing and stopping should work also against other
125-
Robot Framework remote server implementations.
127+
__ `Stopping remote server`_
128+
__ `Starting server on background`_
126129

127130
Stopping remote server
128131
----------------------
@@ -136,19 +139,77 @@ The remote server can be gracefully stopped using several different methods:
136139
work on Windows. Notice that with Jython you need to send the signal to the
137140
started Java process, not to the shell typically started by ``jython`` command.
138141

139-
- Using ``Stop Remote Server`` keyword. This can be disabled by using
140-
``allow_stop=False`` when starting the server.
142+
- Using ``Stop Remote Server`` keyword.
143+
144+
- Using ``stop_remote_server`` function in the XML-RPC interface.
145+
146+
- Running ``python -m robotremoteserver stop [uri]`` or using
147+
``stop_remote_server`` function similarly as when `testing is server running`_.
148+
149+
- Calling ``stop`` method of the running server object.
150+
151+
Using ``Stop Remote Server`` keyword, ``stop_remote_server`` XML-RPC function
152+
or stopping functionality provided by ``robotremoteserver`` itself can all be
153+
disabled by using ``allow_remote_stop=False`` when initializing the server.
154+
155+
Starting server on background
156+
-----------------------------
157+
158+
Sometimes it is useful to start the server on background and keep doing
159+
something else, like starting more servers, on the main thread. Starting
160+
from RobotRemoteServer 1.1 this can be accomplished easily:
161+
162+
.. sourcecode:: python
163+
164+
from robotremoteserver import RobotRemoteServer
165+
from mylibrary import MyLibrary
166+
167+
server = RobotRemoteServer(MyLibrary(), port=0, serve=False)
168+
server.start()
169+
print('Remote server started on port %d.' % server.server_port)
170+
# Do something ...
171+
server.stop()
172+
173+
As the above example illustrates, the ``start`` method starts the server on
174+
background. When the server is started on background, none of the earlier
175+
methods to `stop the server`__ work. Instead the server can be stopped, as
176+
shown in the example, by using the ``stop`` method.
177+
178+
__ `Stopping remote server`_
179+
180+
Testing is server running
181+
-------------------------
182+
183+
Starting from version 1.0.1 , ``robotremoteserver`` module supports testing is
184+
a remote server running. This can be accomplished by running the module as
185+
a script with ``test`` argument and an optional URI::
186+
187+
$ python -m robotremoteserver test
188+
Remote server running at http://127.0.0.1:8270.
189+
$ python -m robotremoteserver test http://10.0.0.42:57347
190+
No remote server running at http://10.0.0.42:57347.
191+
192+
Starting from version 1.1, ``robotremoteserver`` module contains function
193+
``stop_remote_server`` that can be used programmatically:
194+
195+
.. sourcecode:: python
196+
197+
from robotremoteserver import test_remote_server
198+
199+
if test_remote_server('http://localhost:8270'):
200+
print('Remote server running!')
141201

142-
- Running ``python -m robotremoteserver stop [uri]`` similarly as when `testing
143-
is server running`_. Also this can be disabled using ``allow_stop=False``.
144-
New in version 1.0.1.
202+
``robotremoteserver`` can be also used to stop a remote server by using
203+
``stop`` argument on the command line or by using ``stop_remote_server``
204+
function programmatically. Testing and stopping should work also with
205+
other Robot Framework remote server implementations.
145206

146207
Example
147208
-------
148209

149-
The remote server project contains an example_ that can be studied and also
210+
The remote server project contains an example__ that can be studied and also
150211
executed once the library is installed. You can get the example by cloning
151212
the project on GitHub_, and it is also included in the source distribution
152213
available on PyPI_.
153214

154-
.. _example: https://github.com/robotframework/PythonRemoteServer/tree/master/example
215+
__ https://github.com/robotframework/PythonRemoteServer/tree/master/example

src/robotremoteserver.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
class RobotRemoteServer(object):
4949

5050
def __init__(self, library, host='127.0.0.1', port=8270, port_file=None,
51-
allow_stop=True, serve=True):
51+
allow_stop='DEPRECATED', serve=True, allow_remote_stop=True):
5252
"""Configure and start-up remote server.
5353
5454
:param library: Test library instance or module to host.
@@ -59,14 +59,19 @@ def __init__(self, library, host='127.0.0.1', port=8270, port_file=None,
5959
a string.
6060
:param port_file: File to write port that is used. ``None`` means
6161
no such file is written.
62-
:param allow_stop: Allow/disallow stopping the server using ``Stop
63-
Remote Server`` keyword.
64-
:param serve: When ``True`` starts the server automatically.
65-
When ``False``, server can be started with
66-
:meth:`serve` or :meth:`start` methods.
62+
:param allow_stop: DEPRECATED. User ``allow_remote_stop`` instead.
63+
:param serve: If ``True``, start the server automatically and
64+
wait for it to be stopped. If ``False``, server can
65+
be started using :meth:`serve` or :meth:`start`
66+
methods.
67+
:param allow_remote_stop: Allow/disallow stopping the server using
68+
``Stop Remote Server`` keyword and
69+
``stop_remote_server`` XML-RPC method.
6770
"""
71+
if allow_stop != 'DEPRECATED':
72+
allow_remote_stop = allow_stop
6873
self._server = StoppableXMLRPCServer(host, int(port), port_file,
69-
allow_stop)
74+
allow_remote_stop)
7075
self._library = RemoteLibraryFactory(library)
7176
self._register_functions(self._server)
7277
if serve:
@@ -90,7 +95,7 @@ def _register_functions(self, server):
9095
server.register_function(self._stop_serve, 'stop_remote_server')
9196

9297
def serve(self, log=True):
93-
"""Start the server and wait for it to finish.
98+
"""Start the server and wait for it to be stopped.
9499
95100
:param log: Log message about startup or not.
96101
@@ -103,8 +108,9 @@ def serve(self, log=True):
103108
to start the server on background.
104109
105110
In addition to signals, the server can be stopped with ``Stop Remote
106-
Server`` keyword. Using :meth:`stop` method is possible too, but
107-
requires running this method in a thread.
111+
Server`` keyword, unless disabled when the server is initialized.
112+
Using :meth:`stop` method is possible too, but requires running this
113+
method in a thread.
108114
"""
109115
self._server.serve(log=log)
110116

@@ -566,7 +572,7 @@ def test_remote_server(uri, log=True):
566572

567573

568574
def stop_remote_server(uri, log=True):
569-
"""Stop remote server.
575+
"""Stop remote server unless server has disabled stopping.
570576
571577
:param uri: Server address.
572578
:param log: Log status message or not.

0 commit comments

Comments
 (0)