|
4 | 4 | from pytestqt.qt_compat import QtCore, Signal
|
5 | 5 |
|
6 | 6 |
|
| 7 | +class Signaller(QtCore.QObject): |
| 8 | + |
| 9 | + signal = Signal() |
| 10 | + |
| 11 | + |
7 | 12 | def test_signal_blocker_exception(qtbot):
|
| 13 | + """ |
| 14 | + Make sure waitSignal without signals and timeout doesn't hang, but raises |
| 15 | + ValueError instead. |
| 16 | + """ |
8 | 17 | with pytest.raises(ValueError):
|
9 | 18 | qtbot.waitSignal(None, None).wait()
|
10 | 19 |
|
11 | 20 |
|
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 |
16 | 29 |
|
17 |
| -def test_wait_signal_context_manager(qtbot, monkeypatch): |
18 |
| - signaller = Signaller() |
19 | 30 |
|
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: |
25 | 36 | 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 |
31 | 38 |
|
32 | 39 |
|
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 | + """ |
34 | 54 | 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) |
42 | 59 |
|
43 | 60 | # 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