@@ -37,28 +37,71 @@ def test_cycle_time(self):
3737 bus2 .shutdown ()
3838
3939
40- @unittest .skipIf (IS_CI , "the timing sensitive behaviour cannot be reproduced reliably on a CI server" )
4140 def test_removing_bus_tasks (self ):
42-
43- bus1 = can .interface .Bus (bustype = 'virtual' )
44- bus2 = can .interface .Bus (bustype = 'virtual' )
41+ bus = can .interface .Bus (bustype = 'virtual' )
4542 tasks = []
4643 for task_i in range (10 ):
4744 msg = can .Message (extended_id = False , arbitration_id = 0x123 , data = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ])
4845 msg .arbitration_id = task_i
49- task = bus1 .send_periodic (msg , 0.1 , 1 )
46+ task = bus .send_periodic (msg , 0.1 , 1 )
5047 tasks .append (task )
5148 self .assertIsInstance (task , can .broadcastmanager .CyclicSendTaskABC )
5249
53- assert len (bus1 ._periodic_tasks ) == 10
50+ assert len (bus ._periodic_tasks ) == 10
5451
5552 for task in tasks :
53+ # Note calling task.stop will remove the task from the Bus's internal task management list
5654 task .stop ()
5755
58- assert len (bus1 ._periodic_tasks ) == 0
56+ assert len (bus ._periodic_tasks ) == 0
57+ bus .shutdown ()
5958
60- bus1 .shutdown ()
61- bus2 .shutdown ()
59+ def test_managed_tasks (self ):
60+ bus = can .interface .Bus (bustype = 'virtual' , receive_own_messages = True )
61+ tasks = []
62+ for task_i in range (3 ):
63+ msg = can .Message (extended_id = False , arbitration_id = 0x123 , data = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ])
64+ msg .arbitration_id = task_i
65+ task = bus .send_periodic (msg , 0.1 , 10 , store_task = False )
66+ tasks .append (task )
67+ self .assertIsInstance (task , can .broadcastmanager .CyclicSendTaskABC )
68+
69+ assert len (bus ._periodic_tasks ) == 0
70+
71+ # Self managed tasks should still be sending messages
72+ for _ in range (50 ):
73+ received_msg = bus .recv (timeout = 5.0 )
74+ assert received_msg is not None
75+ assert received_msg .arbitration_id in {0 , 1 , 2 }
76+
77+ for task in tasks :
78+ task .stop ()
79+
80+ for task in tasks :
81+ assert task .thread .join (5.0 ) is None , "Task didn't stop before timeout"
82+
83+ bus .shutdown ()
84+
85+ def test_stopping_perodic_tasks (self ):
86+ bus = can .interface .Bus (bustype = 'virtual' )
87+ tasks = []
88+ for task_i in range (10 ):
89+ msg = can .Message (extended_id = False , arbitration_id = 0x123 , data = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ])
90+ msg .arbitration_id = task_i
91+ task = bus .send_periodic (msg , 0.1 , 1 )
92+ tasks .append (task )
93+
94+ # stop half the tasks using the task object
95+ for task in tasks [::2 ]:
96+ task .stop ()
97+
98+ # stop the other half using the bus api
99+ bus .stop_all_periodic_tasks ()
100+
101+ for task in tasks :
102+ assert task .thread .join (5.0 ) is None , "Task didn't stop before timeout"
103+
104+ bus .shutdown ()
62105
63106
64107if __name__ == '__main__' :
0 commit comments