7
7
class Signaller (QtCore .QObject ):
8
8
9
9
signal = Signal ()
10
+ signal_2 = Signal ()
10
11
11
12
12
13
def test_signal_blocker_exception (qtbot ):
@@ -18,21 +19,26 @@ def test_signal_blocker_exception(qtbot):
18
19
qtbot .waitSignal (None , None ).wait ()
19
20
20
21
21
- def explicit_wait (qtbot , signal , timeout ):
22
+ def explicit_wait (qtbot , signal , timeout , multiple ):
22
23
"""
23
24
Explicit wait for the signal using blocker API.
24
25
"""
25
- blocker = qtbot .waitSignal (signal , timeout )
26
- assert not blocker .signal_triggered
26
+ func = qtbot .waitSignals if multiple else qtbot .waitSignal
27
+ blocker = func (signal , timeout )
28
+ if multiple :
29
+ assert not blocker .signals_triggered
30
+ else :
31
+ assert not blocker .signal_triggered
27
32
blocker .wait ()
28
33
return blocker
29
34
30
35
31
- def context_manager_wait (qtbot , signal , timeout ):
36
+ def context_manager_wait (qtbot , signal , timeout , multiple ):
32
37
"""
33
38
Waiting for signal using context manager API.
34
39
"""
35
- with qtbot .waitSignal (signal , timeout ) as blocker :
40
+ func = qtbot .waitSignals if multiple else qtbot .waitSignal
41
+ with func (signal , timeout ) as blocker :
36
42
pass
37
43
return blocker
38
44
@@ -55,14 +61,15 @@ def test_signal_triggered(qtbot, wait_function, emit_delay, timeout,
55
61
the expected results.
56
62
"""
57
63
signaller = Signaller ()
64
+
58
65
timer = QtCore .QTimer ()
59
66
timer .setSingleShot (True )
60
67
timer .timeout .connect (signaller .signal .emit )
61
68
timer .start (emit_delay )
62
69
63
70
# block signal until either signal is emitted or timeout is reached
64
71
start_time = time .time ()
65
- blocker = wait_function (qtbot , signaller .signal , timeout )
72
+ blocker = wait_function (qtbot , signaller .signal , timeout , multiple = False )
66
73
67
74
# Check that event loop exited.
68
75
assert not blocker ._loop .isRunning ()
@@ -78,6 +85,60 @@ def test_signal_triggered(qtbot, wait_function, emit_delay, timeout,
78
85
assert time .time () - start_time < (max_wait_ms / 1000.0 )
79
86
80
87
88
+ @pytest .mark .parametrize (
89
+ ('wait_function' , 'emit_delay_1' , 'emit_delay_2' , 'timeout' ,
90
+ 'expected_signals_triggered' ),
91
+ [
92
+ (explicit_wait , 500 , 600 , 2000 , True ),
93
+ (explicit_wait , 500 , 600 , None , True ),
94
+ (context_manager_wait , 500 , 600 , 2000 , True ),
95
+ (context_manager_wait , 500 , 600 , None , True ),
96
+ (explicit_wait , 2000 , 2000 , 500 , False ),
97
+ (explicit_wait , 500 , 2000 , 1000 , False ),
98
+ (explicit_wait , 2000 , 500 , 1000 , False ),
99
+ (context_manager_wait , 2000 , 2000 , 500 , False ),
100
+ (context_manager_wait , 500 , 2000 , 1000 , False ),
101
+ (context_manager_wait , 2000 , 500 , 1000 , False ),
102
+ ]
103
+ )
104
+ def test_signal_triggered_multiple (qtbot , wait_function , emit_delay_1 ,
105
+ emit_delay_2 , timeout ,
106
+ expected_signals_triggered ):
107
+ """
108
+ Testing for a signal in different conditions, ensuring we are obtaining
109
+ the expected results.
110
+ """
111
+ signaller = Signaller ()
112
+
113
+ timer = QtCore .QTimer ()
114
+ timer .setSingleShot (True )
115
+ timer .timeout .connect (signaller .signal .emit )
116
+ timer .start (emit_delay_1 )
117
+
118
+ timer2 = QtCore .QTimer ()
119
+ timer2 .setSingleShot (True )
120
+ timer2 .timeout .connect (signaller .signal_2 .emit )
121
+ timer2 .start (emit_delay_2 )
122
+
123
+ # block signal until either signal is emitted or timeout is reached
124
+ start_time = time .time ()
125
+ blocker = wait_function (qtbot , [signaller .signal , signaller .signal_2 ],
126
+ timeout , multiple = True )
127
+
128
+ # Check that event loop exited.
129
+ assert not blocker ._loop .isRunning ()
130
+
131
+ # ensure that either signal was triggered or timeout occurred
132
+ assert blocker .signals_triggered == expected_signals_triggered
133
+
134
+ # Check that we exited by the earliest parameter; timeout = None means
135
+ # wait forever, so ensure we waited at most 4 times emit-delay
136
+ if timeout is None :
137
+ timeout = max (emit_delay_1 , emit_delay_2 ) * 4
138
+ max_wait_ms = max (emit_delay_1 , emit_delay_2 , timeout )
139
+ assert time .time () - start_time < (max_wait_ms / 1000.0 )
140
+
141
+
81
142
def test_explicit_emit (qtbot ):
82
143
"""
83
144
Make sure an explicit emit() inside a waitSignal block works.
@@ -87,3 +148,16 @@ def test_explicit_emit(qtbot):
87
148
signaller .signal .emit ()
88
149
89
150
assert waiting .signal_triggered
151
+
152
+
153
+ def test_explicit_emit_multiple (qtbot ):
154
+ """
155
+ Make sure an explicit emit() inside a waitSignal block works.
156
+ """
157
+ signaller = Signaller ()
158
+ with qtbot .waitSignals ([signaller .signal , signaller .signal_2 ],
159
+ timeout = 5000 ) as waiting :
160
+ signaller .signal .emit ()
161
+ signaller .signal_2 .emit ()
162
+
163
+ assert waiting .signals_triggered
0 commit comments