@@ -12,16 +12,19 @@ def test_single_bus(self):
1212 with can .Bus ("test" , interface = "virtual" , receive_own_messages = True ) as bus :
1313 reader = can .BufferedReader ()
1414 notifier = can .Notifier (bus , [reader ], 0.1 )
15+ self .assertFalse (notifier .stopped )
1516 msg = can .Message ()
1617 bus .send (msg )
1718 self .assertIsNotNone (reader .get_message (1 ))
1819 notifier .stop ()
20+ self .assertTrue (notifier .stopped )
1921
2022 def test_multiple_bus (self ):
2123 with can .Bus (0 , interface = "virtual" , receive_own_messages = True ) as bus1 :
2224 with can .Bus (1 , interface = "virtual" , receive_own_messages = True ) as bus2 :
2325 reader = can .BufferedReader ()
2426 notifier = can .Notifier ([bus1 , bus2 ], [reader ], 0.1 )
27+ self .assertFalse (notifier .stopped )
2528 msg = can .Message ()
2629 bus1 .send (msg )
2730 time .sleep (0.1 )
@@ -33,6 +36,30 @@ def test_multiple_bus(self):
3336 self .assertIsNotNone (recv_msg )
3437 self .assertEqual (recv_msg .channel , 1 )
3538 notifier .stop ()
39+ self .assertTrue (notifier .stopped )
40+
41+ def test_context_manager (self ):
42+ with can .Bus ("test" , interface = "virtual" , receive_own_messages = True ) as bus :
43+ reader = can .BufferedReader ()
44+ with can .Notifier (bus , [reader ], 0.1 ) as notifier :
45+ self .assertFalse (notifier .stopped )
46+ msg = can .Message ()
47+ bus .send (msg )
48+ self .assertIsNotNone (reader .get_message (1 ))
49+ notifier .stop ()
50+ self .assertTrue (notifier .stopped )
51+
52+ def test_registry (self ):
53+ with can .Bus ("test" , interface = "virtual" , receive_own_messages = True ) as bus :
54+ reader = can .BufferedReader ()
55+ with can .Notifier (bus , [reader ], 0.1 ):
56+ # creating a second notifier for the same bus must fail
57+ self .assertRaises (ValueError , can .Notifier , bus , [reader ], 0.1 )
58+
59+ # now the first notifier is stopped, a new notifier can be created without error:
60+ with can .Notifier (bus , [reader ], 0.1 ):
61+ # the next notifier call should fail again since there is an active notifier already
62+ self .assertRaises (ValueError , can .Notifier , bus , [reader ], 0.1 )
3663
3764
3865class AsyncNotifierTest (unittest .TestCase ):
0 commit comments