| 1 |
================================================= |
| 2 |
Running Tests in Parallel with Multiple Processes |
| 3 |
================================================= |
| 4 |
|
| 5 |
.. note :: |
| 6 |
|
| 7 |
New in version 0.3 |
| 8 |
|
| 9 |
Use the ``mp`` plugin to enable distribution of tests across multiple |
| 10 |
processes. Doing this may speed up your test run if your tests are |
| 11 |
heavily IO or CPU bound. But it *imposes an overhead cost* that is not |
| 12 |
trivial, and it *complicates the use of test fixtures* and may *conflict |
| 13 |
with plugins that are not designed to work with it*. |
| 14 |
|
| 15 |
Usage |
| 16 |
----- |
| 17 |
|
| 18 |
To activate the plugin, include the plugin module in the plugins list |
| 19 |
in ``[unittest]`` section in a config file:: |
| 20 |
|
| 21 |
[unittest] |
| 22 |
plugins = nose2.plugins.mp |
| 23 |
|
| 24 |
Or pass the module with the :option:`--plugin` command-line option:: |
| 25 |
|
| 26 |
nose2 --plugin=nose2.plugins.mp |
| 27 |
|
| 28 |
Then configure the number of processes to run. You can do that either |
| 29 |
with the :option:`-N` option:: |
| 30 |
|
| 31 |
nose2 -N 2 |
| 32 |
|
| 33 |
or by setting ``processes`` in the ``[multiprocess]`` section of a |
| 34 |
config file:: |
| 35 |
|
| 36 |
[multiprocess] |
| 37 |
processes = 2 |
| 38 |
|
| 39 |
.. note :: |
| 40 |
|
| 41 |
If you make the plugin always active by setting ``always-on`` in |
| 42 |
the ``[multiprocess]`` section of a config file, but do not set |
| 43 |
``processes`` or pass :option:`-N`, the number of processes |
| 44 |
defaults to the number of CPUs available. Also note that a value of 0 will |
| 45 |
set the actual number of processes to the number of CPUs on the computer. |
| 46 |
|
| 47 |
Should one wish to specify the use of internet sockets for |
| 48 |
interprocess communications, specify the ``bind_address`` |
| 49 |
setting in the ``[multiprocess]`` section of the config file, |
| 50 |
for example:: |
| 51 |
|
| 52 |
[multiprocess] |
| 53 |
bind_address = 127.0.0.1:1024 |
| 54 |
|
| 55 |
This will bind to port 1024 of ``127.0.0.1``. Also:: |
| 56 |
|
| 57 |
[multiprocess] |
| 58 |
bind_address = 127.1.2.3 |
| 59 |
|
| 60 |
will bind to any random open port on ``127.1.2.3``. Any internet |
| 61 |
address or host-name which python can recognize as such, bind, *and* |
| 62 |
connect is acceptable. While ``0.0.0.0`` can be use for listening, |
| 63 |
it is not necessarily an address to which the OS can connect. When |
| 64 |
the port address is ``0`` or omitted, a random open port is used. If |
| 65 |
the setting is omitted or blank, then sockets are not used unless |
| 66 |
nose is being executed on Windows. In which case, an address on |
| 67 |
the loop back interface and a random port are used. Whenever used, |
| 68 |
processes employ a random shared key for authentication. |
| 69 |
|
| 70 |
Guidelines for Test Authors |
| 71 |
--------------------------- |
| 72 |
|
| 73 |
Not every test suite will work well, or work at all, when run in |
| 74 |
parallel. For some test suites, parallel execution makes no sense. For |
| 75 |
others, it will expose bugs and ordering dependencies in test cases and |
| 76 |
test modules. |
| 77 |
|
| 78 |
Overhead Cost |
| 79 |
~~~~~~~~~~~~~ |
| 80 |
|
| 81 |
Starting subprocesses and dispatching tests takes time. A test run |
| 82 |
that includes a relatively small number of tests that are not I/O or |
| 83 |
CPU bound (or calling ``time.sleep()``) is likely to be *slower* when run |
| 84 |
in parallel. |
| 85 |
|
| 86 |
As of this writing, for instance, nose2's test suite |
| 87 |
takes about 10 times as long to run when using ``multiprocessing``, due to |
| 88 |
the overhead cost. |
| 89 |
|
| 90 |
Shared Fixtures |
| 91 |
~~~~~~~~~~~~~~~ |
| 92 |
|
| 93 |
The individual test processes do not share state or data after |
| 94 |
launch. This means *tests that share a fixture* -- tests that are loaded |
| 95 |
from modules where ``setUpModule`` is defined, and tests in test |
| 96 |
classes that define ``setUpClass`` -- *must all be dispatched to the |
| 97 |
same process at the same time*. So if you use these kinds of fixtures, |
| 98 |
your test runs may be less parallel than you expect. |
| 99 |
|
| 100 |
Tests Load Twice |
| 101 |
~~~~~~~~~~~~~~~~ |
| 102 |
|
| 103 |
Test cases may not be pickleable, so nose2 can't transmit them |
| 104 |
directly to its test runner processes. Tests are distributed by |
| 105 |
name. This means that *tests always load twice* -- once in the main |
| 106 |
process, during initial collection, and then again in the test runner |
| 107 |
process, where they are loaded by name. This may be problematic for |
| 108 |
some test suites. |
| 109 |
|
| 110 |
Random Execution Order |
| 111 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 112 |
|
| 113 |
Tests do not execute in the same order when run in parallel. Results |
| 114 |
will be returned in effectively random order, and tests in the same |
| 115 |
module (*as long as they do not share fixtures*) may execute in any |
| 116 |
order and in different processes. Some test suites have ordering |
| 117 |
dependencies, intentional or not, and those that do will fail randomly |
| 118 |
when run with this plugin. |
| 119 |
|
| 120 |
Guidelines for Plugin Authors |
| 121 |
----------------------------- |
| 122 |
|
| 123 |
The MultiProcess plugin is designed to work with other plugins, but |
| 124 |
other plugins may have to return the favor, especially if they load |
| 125 |
tests or care about something that happens *during* test execution. |
| 126 |
|
| 127 |
|
| 128 |
New Methods |
| 129 |
~~~~~~~~~~~ |
| 130 |
|
| 131 |
The ``MultiProcess`` plugin adds a few plugin hooks that other plugins can |
| 132 |
use to set themselves up for multiprocess test runs. Plugins don't |
| 133 |
have to do anything special to register for these hooks; just |
| 134 |
implement the methods as normal. |
| 135 |
|
| 136 |
.. function :: registerInSubprocess(self, event) |
| 137 |
|
| 138 |
:param event: :class:`nose2.plugins.mp.RegisterInSubprocessEvent` |
| 139 |
|
| 140 |
The ``registerInSubprocess`` hook is called after plugin |
| 141 |
registration to enable plugins that need to run in subprocesses to |
| 142 |
register that fact. The most common thing to do, for plugins that |
| 143 |
need to run in subprocesses, is:: |
| 144 |
|
| 145 |
def registerInSubprocess(self, event): |
| 146 |
event.pluginClasses.append(self.__class__) |
| 147 |
|
| 148 |
It is not required that plugins append their own class. If for some |
| 149 |
reason there is a different plugin class, or set of classes, that |
| 150 |
should run in the test-running subprocesses, add that class or |
| 151 |
those classes instead. |
| 152 |
|
| 153 |
.. function :: startSubprocess(self, event) |
| 154 |
|
| 155 |
:param event: :class:`nose2.plugins.mp.SubprocessEvent` |
| 156 |
|
| 157 |
The ``startSubprocess`` hook fires in each test-running subprocess |
| 158 |
after it has loaded its plugins but before any tests are executed. |
| 159 |
|
| 160 |
Plugins can customize test execution here in the same way as in |
| 161 |
:func:`startTestRun`, by setting ``event.executeTests``, and |
| 162 |
prevent test execution by setting ``event.handled`` to True and |
| 163 |
returning False. |
| 164 |
|
| 165 |
.. function :: stopSubprocess(self, event) |
| 166 |
|
| 167 |
:param event: :class:`nose2.plugins.mp.SubprocessEvent` |
| 168 |
|
| 169 |
The ``stopSubprocess`` event fires just before each test running |
| 170 |
subprocess shuts down. Plugins can use this hook for any |
| 171 |
per-process finalization that they may need to do. |
| 172 |
|
| 173 |
The same event instance is passed to ``startSubprocess`` and |
| 174 |
``stopSubprocess``, which enables plugins to use that event's |
| 175 |
metadata to communicate state or other information from the |
| 176 |
start to the stop hooks, if needed. |
| 177 |
|
| 178 |
New Events |
| 179 |
~~~~~~~~~~ |
| 180 |
|
| 181 |
The ``MultiProcess`` plugin's new hooks come with custom event classes. |
| 182 |
|
| 183 |
.. autoclass :: nose2.plugins.mp.RegisterInSubprocessEvent |
| 184 |
:members: |
| 185 |
|
| 186 |
.. autoclass :: nose2.plugins.mp.SubprocessEvent |
| 187 |
:members: |
| 188 |
|
| 189 |
Stern Warning |
| 190 |
~~~~~~~~~~~~~ |
| 191 |
|
| 192 |
All event attributes, *including ``event.metadata``, must be |
| 193 |
pickleable*. If your plugin sets any event attributes or puts anything |
| 194 |
into ``event.metadata``, it is your responsibility to ensure that |
| 195 |
anything you can possibly put in is pickleable. |
| 196 |
|
| 197 |
Do I Really Care? |
| 198 |
~~~~~~~~~~~~~~~~~ |
| 199 |
|
| 200 |
If you answer *yes* to any of the following questions, then your |
| 201 |
plugin will not work with multiprocess testing without modification: |
| 202 |
|
| 203 |
* Does your plugin load tests? |
| 204 |
* Does your plugin capture something that happens during test execution? |
| 205 |
* Does your plugin require user interaction during test execution? |
| 206 |
* Does your plugin set executeTests in startTestRun? |
| 207 |
|
| 208 |
Here's how to handle each of those cases. |
| 209 |
|
| 210 |
Loading Tests |
| 211 |
^^^^^^^^^^^^^ |
| 212 |
|
| 213 |
* Implement :func:`registerInSubprocess` as suggested to enable your |
| 214 |
plugin in the test runner processes. |
| 215 |
|
| 216 |
Capturing Test Execution State |
| 217 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 218 |
|
| 219 |
* Implement :func:`registerInSubprocess` as suggested to enable your |
| 220 |
plugin in the test runner processes. |
| 221 |
|
| 222 |
* Be wary of setting ``event.metadata`` unconditionally. Your plugin |
| 223 |
will execute in the main process and in the test runner processes, |
| 224 |
and will see :func:`setTestOutcome` and :func:`testOutcome` events |
| 225 |
*in both processes*. If you unconditionally set a key in |
| 226 |
``event.metadata``, the plugin instance in the main process will |
| 227 |
overwrite anything set in that key by the instance in the |
| 228 |
subprocess. |
| 229 |
|
| 230 |
* If you need to write something to a file, implement |
| 231 |
:func:`stopSubprocess` to write a file in each test runner process. |
| 232 |
|
| 233 |
Overriding Test Execution |
| 234 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 235 |
|
| 236 |
* Implement :func:`registerInSubprocess` as suggested to enable your |
| 237 |
plugin in the test runner processes and make a note that your plugin |
| 238 |
is running under a multiprocess session. |
| 239 |
|
| 240 |
* When running multiprocess, *do not* set ``event.executeTests`` in |
| 241 |
:func:`startTestRun` -- instead, set it in :func:`startSubprocess` |
| 242 |
instead. This will allow the multiprocess plugin to install its test |
| 243 |
executor in the main process, while your plugin takes over test |
| 244 |
execution in the test runner subprocesses. |
| 245 |
|
| 246 |
Interacting with Users |
| 247 |
^^^^^^^^^^^^^^^^^^^^^^ |
| 248 |
|
| 249 |
* You are probably safe because as a responsible plugin author you are |
| 250 |
already firing the interaction hooks (:func:`beforeInteraction`, |
| 251 |
:func:`afterInteraction`) around your interactive bits, and skipping |
| 252 |
them when the :func:`beforeInteraction` hook returns ``False`` and sets |
| 253 |
``event.handled``. |
| 254 |
|
| 255 |
If you're not doing that, start! |
| 256 |
|
| 257 |
Possible Issues On Windows |
| 258 |
-------------------------- |
| 259 |
|
| 260 |
On windows, there are a few known bugs with respect to multiprocessing. |
| 261 |
|
| 262 |
Python on windows does not use fork(). It bootstraps from a |
| 263 |
separate interpreter invocation. In certain contexts, the "value" for |
| 264 |
a parameter will be taken as a "count" and subprocess use this to build |
| 265 |
the flag for the command-line. E.g., If this value is 2 billion |
| 266 |
(like a hash seed), subprocess.py may attempt to built a 2gig string, |
| 267 |
and possibly throw a MemoryError exception. The related bug is |
| 268 |
http://bugs.python.org/issue20954. |
| 269 |
|
| 270 |
Reference |
| 271 |
--------- |
| 272 |
|
| 273 |
.. autoplugin :: nose2.plugins.mp.MultiProcess |