Skip to content

Commit c656d5b

Browse files
committed
Using parametrize to test different timeouts
1 parent b63f7f0 commit c656d5b

File tree

1 file changed

+49
-27
lines changed

1 file changed

+49
-27
lines changed

pytestqt/_tests/test_wait_signal.py

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,65 @@
44
from pytestqt.qt_compat import QtCore, Signal
55

66

7+
class Signaller(QtCore.QObject):
8+
9+
signal = Signal()
10+
11+
712
def test_signal_blocker_exception(qtbot):
13+
"""
14+
Make sure waitSignal without signals and timeout doesn't hang, but raises
15+
ValueError instead.
16+
"""
817
with pytest.raises(ValueError):
918
qtbot.waitSignal(None, None).wait()
1019

1120

12-
class Signaller(QtCore.QObject):
13-
14-
signal = Signal()
15-
21+
def explicit_wait(qtbot, signal, timeout):
22+
"""
23+
Explicit wait for the signal using blocker API.
24+
"""
25+
blocker = qtbot.waitSignal(signal, timeout)
26+
start_time = time.time()
27+
blocker.wait()
28+
return blocker.loop, start_time
1629

17-
def test_wait_signal_context_manager(qtbot, monkeypatch):
18-
signaller = Signaller()
1930

20-
# Emit a signal after half a second, and block the signal with a timeout
21-
# of 2 seconds.
22-
QtCore.QTimer.singleShot(500, signaller.signal.emit)
23-
with qtbot.waitSignal(signaller.signal, 2000) as blocker:
24-
saved_loop = blocker.loop
31+
def context_manager_wait(qtbot, signal, timeout):
32+
"""
33+
Waiting for signal using context manager API.
34+
"""
35+
with qtbot.waitSignal(signal, timeout) as blocker:
2536
start_time = time.time()
26-
27-
# Check that event loop exited.
28-
assert not saved_loop.isRunning()
29-
# Check that it didn't exit by a timeout.
30-
assert time.time() - start_time < 2 # Less than 2 seconds elapsed
37+
return blocker.loop, start_time
3138

3239

33-
def test_wait_signal_function(qtbot, monkeypatch):
40+
@pytest.mark.parametrize(
41+
('wait_function', 'emit_delay', 'timeout'),
42+
[
43+
(explicit_wait, 500, 2000),
44+
(explicit_wait, 500, None),
45+
(context_manager_wait, 500, 2000),
46+
(context_manager_wait, 500, None),
47+
]
48+
)
49+
def test_signal_triggered(qtbot, wait_function, emit_delay, timeout):
50+
"""
51+
Ensure that a signal being triggered before timeout expires makes the
52+
loop quitting early.
53+
"""
3454
signaller = Signaller()
35-
36-
# Emit a signal after half a second, and block the signal with a timeout
37-
# of 2 seconds.
38-
QtCore.QTimer.singleShot(500, signaller.signal.emit)
39-
blocker = qtbot.waitSignal(signaller.signal, 2000)
40-
start_time = time.time()
41-
blocker.wait()
55+
QtCore.QTimer.singleShot(emit_delay, signaller.signal.emit)
56+
57+
# block signal until either signal is emitted or timeout is reached
58+
loop, start_time = wait_function(qtbot, signaller.signal, timeout)
4259

4360
# Check that event loop exited.
44-
assert not blocker.loop.isRunning()
45-
# Check that it didn't exit by a timeout.
46-
assert time.time() - start_time < 2 # Less than 2 seconds elapsed
61+
assert not loop.isRunning()
62+
63+
# Check that we exited by the earliest parameter; timeout = None means
64+
# wait forever, so ensure we waited at most 4 times emit-delay
65+
if timeout is None:
66+
timeout = emit_delay * 4
67+
max_wait_ms = max(emit_delay, timeout)
68+
assert time.time() - start_time < (max_wait_ms / 1000.0)

0 commit comments

Comments
 (0)