@@ -155,25 +155,49 @@ def test_stopping_perodic_tasks(self):
155
155
def test_restart_perodic_tasks (self ):
156
156
period = 0.01
157
157
safe_timeout = period * 5 if not IS_PYPY else 1.0
158
+ duration = 0.3
158
159
159
160
msg = can .Message (
160
161
is_extended_id = False , arbitration_id = 0x123 , data = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
161
162
)
162
163
164
+ def _read_all_messages (_bus : can .interfaces .virtual .VirtualBus ) -> None :
165
+ sleep (safe_timeout )
166
+ while not _bus .queue .empty ():
167
+ _bus .recv (timeout = period )
168
+ sleep (safe_timeout )
169
+
163
170
with can .ThreadSafeBus (interface = "virtual" , receive_own_messages = True ) as bus :
164
171
task = bus .send_periodic (msg , period )
165
- self .assertIsInstance (task , can .broadcastmanager .RestartableCyclicTaskABC )
172
+ self .assertIsInstance (task , can .broadcastmanager .ThreadBasedCyclicSendTask )
166
173
167
174
# Test that the task is sending messages
168
175
sleep (safe_timeout )
169
176
assert not bus .queue .empty (), "messages should have been transmitted"
170
177
171
178
# Stop the task and check that messages are no longer being sent
172
179
bus .stop_all_periodic_tasks (remove_tasks = False )
180
+ _read_all_messages (bus )
181
+ assert bus .queue .empty (), "messages should not have been transmitted"
182
+
183
+ # Restart the task and check that messages are being sent again
184
+ task .start ()
173
185
sleep (safe_timeout )
174
- while not bus .queue .empty ():
175
- bus .recv (timeout = period )
176
- sleep (safe_timeout )
186
+ assert not bus .queue .empty (), "messages should have been transmitted"
187
+
188
+ # Stop the task and check that messages are no longer being sent
189
+ bus .stop_all_periodic_tasks (remove_tasks = False )
190
+ _read_all_messages (bus )
191
+ assert bus .queue .empty (), "messages should not have been transmitted"
192
+
193
+ # Restart the task with limited duration and wait until it stops
194
+ task .duration = duration
195
+ task .start ()
196
+ sleep (duration + safe_timeout )
197
+ assert task .stopped
198
+ assert time .time () > task .end_time
199
+ assert not bus .queue .empty (), "messages should have been transmitted"
200
+ _read_all_messages (bus )
177
201
assert bus .queue .empty (), "messages should not have been transmitted"
178
202
179
203
# Restart the task and check that messages are being sent again
0 commit comments