Skip to content

Commit bd9580b

Browse files
committed
Add documentation about waitSignals
- Also add docs about the SignalBlocker and MultiSignalBlocker instance variables
1 parent d54cd87 commit bd9580b

File tree

2 files changed

+70
-29
lines changed

2 files changed

+70
-29
lines changed

docs/index.rst

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,44 @@ ensuring the results are correct::
163163
assert_application_results(app)
164164

165165

166+
167+
raising
168+
-------
169+
166170
.. versionadded:: 1.4
167171

168172
Additionally, you can pass ``raising=True`` to raise a
169-
:class:`SignalTimeoutError` if the timeout is reached.
173+
:class:`SignalTimeoutError` if the timeout is reached before the signal
174+
is triggered::
175+
176+
def test_long_computation(qtbot):
177+
...
178+
with qtbot.waitSignal(app.worker.finished, raising=True) as blocker:
179+
app.worker.start()
180+
# if timeout is reached, SignalTimeoutError will be raised at this point
181+
assert_application_results(app)
182+
183+
184+
waitSignals
185+
-----------
186+
187+
.. versionadded:: 1.4
188+
189+
If you have to wait until **all** signals in a list are triggered, use
190+
:meth:`qtbot.waitSignals <pytestqt.plugin.QtBot.waitSignals>`, which receives
191+
a list of signals instead of a single signal. As with
192+
:meth:`qtbot.waitSignal <pytestqt.plugin.QtBot.waitSignal>`, it also supports
193+
the new ``raising`` parameter::
194+
195+
def test_workers(qtbot):
196+
workers = spawn_workers()
197+
with qtbot.waitSignal([w.finished for w in workers], raising=True):
198+
for w in workers:
199+
w.start()
200+
201+
# this will be reached after all workers emit their "finished"
202+
# signal or a SignalTimeoutError will be raised
203+
assert_application_results(app)
170204

171205
Exceptions in virtual methods
172206
=============================
@@ -238,10 +272,11 @@ QtBot
238272
.. module:: pytestqt.plugin
239273
.. autoclass:: QtBot
240274

241-
SignalBlocker
242-
-------------
275+
Signals
276+
-------
243277

244278
.. autoclass:: SignalBlocker
279+
.. autoclass:: MultiSignalBlocker
245280

246281
.. autoclass:: SignalTimeoutError
247282

pytestqt/plugin.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,12 @@ def waitSignal(self, signal=None, timeout=1000, raising=False):
252252
manager form is not convenient::
253253
254254
blocker = qtbot.waitSignal(signal, timeout=1000)
255-
blocker.connect(other_signal)
255+
blocker.connect(another_signal)
256256
long_function_that_calls_signal()
257257
blocker.wait()
258258
259+
Any additional signal, when triggered, will make :meth:`wait` return.
260+
259261
.. versionadded:: 1.4
260262
The *raising* parameter.
261263
@@ -286,20 +288,19 @@ def waitSignals(self, signals=None, timeout=1000, raising=False):
286288
287289
Stops current test until all given signals are triggered.
288290
289-
Used to stop the control flow of a test until all given signals are
290-
emitted, or a number of milliseconds, specified by ``timeout``, has
291-
elapsed.
291+
Used to stop the control flow of a test until all (and only
292+
all) signals are emitted, or a number of milliseconds, specified by
293+
``timeout``, has elapsed.
292294
293295
Best used as a context manager::
294296
295-
with qtbot.waitSignals(signal, timeout=1000):
296-
long_function_that_calls_signal()
297+
with qtbot.waitSignals([signal1, signal2], timeout=1000):
298+
long_function_that_calls_signals()
297299
298300
Also, you can use the :class:`MultiSignalBlocker` directly if the
299301
context manager form is not convenient::
300302
301-
blocker = qtbot.waitSignals(signal, timeout=1000)
302-
blocker.connect(other_signal)
303+
blocker = qtbot.waitSignals(signals, timeout=1000)
303304
long_function_that_calls_signal()
304305
blocker.wait()
305306
@@ -322,13 +323,13 @@ def waitSignals(self, signals=None, timeout=1000, raising=False):
322323
blocker = MultiSignalBlocker(timeout=timeout, raising=raising)
323324
if signals is not None:
324325
for signal in signals:
325-
blocker.add_signal(signal)
326+
blocker._add_signal(signal)
326327
return blocker
327328

328329
wait_signals = waitSignals # pep-8 alias
329330

330331

331-
class AbstractSignalBlocker(object):
332+
class _AbstractSignalBlocker(object):
332333

333334
"""
334335
Base class for :class:`SignalBlocker` and :class:`MultiSignalBlocker`.
@@ -340,16 +341,6 @@ class AbstractSignalBlocker(object):
340341
Subclasses also need to provide ``self._signals`` which should evaluate to
341342
``False`` if no signals were configured.
342343
343-
:ivar int timeout: maximum time to wait for a signal to be triggered. Can
344-
be changed before :meth:`wait` is called.
345-
346-
:ivar bool signal_triggered: set to ``True`` if a signal (or all signals in
347-
case of :class:MultipleSignalBlocker:) was triggered, or
348-
``False`` if timeout was reached instead. Until :meth:`wait` is called,
349-
this is set to ``None``.
350-
351-
:ivar bool raising:
352-
If :class:`SignalTimeoutError` should be raised if a timeout occurred.
353344
"""
354345

355346
def __init__(self, timeout=1000, raising=False):
@@ -383,14 +374,24 @@ def __exit__(self, type, value, traceback):
383374
self.wait()
384375

385376

386-
class SignalBlocker(AbstractSignalBlocker):
377+
class SignalBlocker(_AbstractSignalBlocker):
387378

388379
"""
389380
Returned by :meth:`QtBot.waitSignal` method.
390381
382+
:ivar int timeout: maximum time to wait for a signal to be triggered. Can
383+
be changed before :meth:`wait` is called.
384+
385+
:ivar bool signal_triggered: set to ``True`` if a signal (or all signals in
386+
case of :class:`MultipleSignalBlocker`) was triggered, or
387+
``False`` if timeout was reached instead. Until :meth:`wait` is called,
388+
this is set to ``None``.
389+
390+
:ivar bool raising:
391+
If :class:`SignalTimeoutError` should be raised if a timeout occurred.
392+
391393
.. automethod:: wait
392394
.. automethod:: connect
393-
394395
"""
395396

396397
def __init__(self, timeout=1000, raising=False):
@@ -418,20 +419,25 @@ def _quit_loop_by_signal(self):
418419
self._loop.quit()
419420

420421

421-
class MultiSignalBlocker(AbstractSignalBlocker):
422+
class MultiSignalBlocker(_AbstractSignalBlocker):
422423

423424
"""
424-
Returned by :meth:`QtBot.waitSignals` method.
425+
Returned by :meth:`QtBot.waitSignals` method, blocks until all signals
426+
connected to it are triggered or the timeout is reached.
427+
428+
Variables identical to :class:`SignalBlocker`:
429+
- ``timeout``
430+
- ``signal_triggered``
431+
- ``raising``
425432
426433
.. automethod:: wait
427-
.. automethod:: add_signal
428434
"""
429435

430436
def __init__(self, timeout=1000, raising=False):
431437
super(MultiSignalBlocker, self).__init__(timeout, raising=raising)
432438
self._signals = {}
433439

434-
def add_signal(self, signal):
440+
def _add_signal(self, signal):
435441
"""
436442
Adds the given signal to the list of signals which :meth:`wait()` waits
437443
for.

0 commit comments

Comments
 (0)