Skip to content

Commit af6852a

Browse files
committed
Added docs for waitSignal and example
1 parent e9c00f3 commit af6852a

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

docs/index.rst

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,32 @@ created earlier::
116116
assert window.filesTable.item(0, 0).text() == 'video1.avi'
117117
assert window.filesTable.item(1, 0).text() == 'video2.avi'
118118
119-
120-
And that's it for this quick tutorial!
119+
120+
121+
Waiting for threads, processes, etc.
122+
====================================
123+
124+
If your program has long running cumputations running in other threads or
125+
processes, you can use :meth:`qtbot.waitSignal <pytestqt.plugin.QtBot.waitSignal>`
126+
to block a test until a signal is emitted (such as ``QThread.finished``) or a
127+
timeout is reached. This makes it easy to write tests that wait until a
128+
computation running in another thread or process is completed before
129+
ensuring the results are correct::
130+
131+
def test_long_computation(qtbot):
132+
app = Application()
133+
app.worker.start()
134+
135+
with qtbot.waitSignal(app.worker.finished, timeout=10000):
136+
assert_application_results(app)
137+
121138

122139
QtBot
123140
=====
124141

125142
.. module:: pytestqt.plugin
126143
.. autoclass:: QtBot
144+
.. autoclass:: SignalBlocker
127145

128146
Versioning
129147
==========

pytestqt/_tests/test_wait_signal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_signal_triggered(qtbot, wait_function, emit_delay, timeout):
5353
"""
5454
signaller = Signaller()
5555
QtCore.QTimer.singleShot(emit_delay, signaller.signal.emit)
56-
56+
5757
# block signal until either signal is emitted or timeout is reached
5858
loop, start_time = wait_function(qtbot, signaller.signal, timeout)
5959

pytestqt/plugin.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ def waitSignal(self, signal=None, timeout=1000):
224224
with qtbot.waitSignal(signal, timeout=1000):
225225
long_function_that_calls_signal()
226226
227-
Can also be used to return blocker object::
227+
Also, you can use the :class:`SignalBlocker` directly if the context
228+
manager form is not convenient::
228229
229230
blocker = qtbot.waitSignal(signal, timeout=1000)
230231
blocker.connect(other_signal)
@@ -249,21 +250,43 @@ def waitSignal(self, signal=None, timeout=1000):
249250
return blocker
250251

251252

252-
class SignalBlocker:
253+
class SignalBlocker(object):
254+
"""
255+
Returned by :meth:`QtBot.waitSignal` method.
256+
257+
.. automethod:: wait
258+
.. automethod:: connect
259+
260+
:ivar int timeout: maximum time to wait for a signal to be triggered. Can
261+
be changed before :meth:`wait` is called.
262+
"""
253263

254264
def __init__(self, timeout=1000):
255265
self._loop = QtCore.QEventLoop()
256266
self._signals = []
257267
self.timeout = timeout
258268

259269
def wait(self):
270+
"""
271+
Waits until either condition signal is triggered or
272+
timeout is reached.
273+
274+
:raise ValueError: if no signals are connected and timeout is None; in
275+
this case it would wait forever.
276+
"""
260277
if self.timeout is None and len(self._signals) == 0:
261278
raise ValueError("No signals or timeout specified.")
262279
if self.timeout is not None:
263280
QtCore.QTimer.singleShot(self.timeout, self._loop.quit)
264281
self._loop.exec_()
265282

266283
def connect(self, signal):
284+
"""
285+
Connects to the given signal, making :meth:`wait()` return once this signal
286+
is emitted.
287+
288+
:param signal: QtCore.Signal
289+
"""
267290
signal.connect(self._loop.quit)
268291
self._signals.append(signal)
269292

0 commit comments

Comments
 (0)