Skip to content

Commit 68228c3

Browse files
committed
added SignalBlocker.signal_triggered attribute and docs
1 parent af6852a commit 68228c3

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

docs/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ ensuring the results are correct::
132132
app = Application()
133133
app.worker.start()
134134

135-
with qtbot.waitSignal(app.worker.finished, timeout=10000):
135+
with qtbot.waitSignal(app.worker.finished, timeout=10000) as blocker:
136+
assert blocker.signal_triggered
136137
assert_application_results(app)
137138

138139

pytestqt/_tests/test_wait_signal.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,49 @@ def explicit_wait(qtbot, signal, timeout):
2323
Explicit wait for the signal using blocker API.
2424
"""
2525
blocker = qtbot.waitSignal(signal, timeout)
26-
start_time = time.time()
26+
assert blocker.signal_triggered is None
2727
blocker.wait()
28-
return blocker._loop, start_time
28+
return blocker
2929

3030

3131
def context_manager_wait(qtbot, signal, timeout):
3232
"""
3333
Waiting for signal using context manager API.
3434
"""
3535
with qtbot.waitSignal(signal, timeout) as blocker:
36-
start_time = time.time()
37-
return blocker._loop, start_time
36+
pass
37+
return blocker
3838

3939

4040
@pytest.mark.parametrize(
41-
('wait_function', 'emit_delay', 'timeout'),
41+
('wait_function', 'emit_delay', 'timeout', 'expected_signal_triggered'),
4242
[
43-
(explicit_wait, 500, 2000),
44-
(explicit_wait, 500, None),
45-
(context_manager_wait, 500, 2000),
46-
(context_manager_wait, 500, None),
43+
(explicit_wait, 500, 2000, True),
44+
(explicit_wait, 500, None, True),
45+
(context_manager_wait, 500, 2000, True),
46+
(context_manager_wait, 500, None, True),
47+
(explicit_wait, 2000, 500, False),
48+
(context_manager_wait, 2000, 500, False),
4749
]
4850
)
49-
def test_signal_triggered(qtbot, wait_function, emit_delay, timeout):
51+
def test_signal_triggered(qtbot, wait_function, emit_delay, timeout,
52+
expected_signal_triggered):
5053
"""
51-
Ensure that a signal being triggered before timeout expires makes the
52-
loop quitting early.
54+
Testing for a signal in different conditions, ensuring we are obtaining
55+
the expected results.
5356
"""
5457
signaller = Signaller()
5558
QtCore.QTimer.singleShot(emit_delay, signaller.signal.emit)
5659

5760
# block signal until either signal is emitted or timeout is reached
58-
loop, start_time = wait_function(qtbot, signaller.signal, timeout)
61+
start_time = time.time()
62+
blocker = wait_function(qtbot, signaller.signal, timeout)
5963

6064
# Check that event loop exited.
61-
assert not loop.isRunning()
65+
assert not blocker._loop.isRunning()
66+
67+
# ensure that either signal was triggered or timeout occurred
68+
assert blocker.signal_triggered == expected_signal_triggered
6269

6370
# Check that we exited by the earliest parameter; timeout = None means
6471
# wait forever, so ensure we waited at most 4 times emit-delay

pytestqt/plugin.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,17 @@ class SignalBlocker(object):
259259
260260
:ivar int timeout: maximum time to wait for a signal to be triggered. Can
261261
be changed before :meth:`wait` is called.
262+
263+
:ivar bool signal_triggered: set to ``True`` if a signal was triggered, or
264+
``False`` if timeout was reached instead. Until :meth:`wait` is called,
265+
this is set to ``None``.
262266
"""
263267

264268
def __init__(self, timeout=1000):
265269
self._loop = QtCore.QEventLoop()
266270
self._signals = []
267271
self.timeout = timeout
272+
self.signal_triggered = None
268273

269274
def wait(self):
270275
"""
@@ -278,6 +283,7 @@ def wait(self):
278283
raise ValueError("No signals or timeout specified.")
279284
if self.timeout is not None:
280285
QtCore.QTimer.singleShot(self.timeout, self._loop.quit)
286+
self.signal_triggered = False
281287
self._loop.exec_()
282288

283289
def connect(self, signal):
@@ -287,9 +293,17 @@ def connect(self, signal):
287293
288294
:param signal: QtCore.Signal
289295
"""
290-
signal.connect(self._loop.quit)
296+
signal.connect(self._quit_loop_by_signal)
291297
self._signals.append(signal)
292298

299+
300+
def _quit_loop_by_signal(self):
301+
"""
302+
quits the event loop and marks that we finished because of a signal.
303+
"""
304+
self.signal_triggered = True
305+
self._loop.quit()
306+
293307
def __enter__(self):
294308
# Return self for testing purposes. Generally not needed.
295309
return self

0 commit comments

Comments
 (0)