@@ -12,16 +12,19 @@ def test_single_bus(self):
12
12
with can .Bus ("test" , interface = "virtual" , receive_own_messages = True ) as bus :
13
13
reader = can .BufferedReader ()
14
14
notifier = can .Notifier (bus , [reader ], 0.1 )
15
+ self .assertFalse (notifier .stopped )
15
16
msg = can .Message ()
16
17
bus .send (msg )
17
18
self .assertIsNotNone (reader .get_message (1 ))
18
19
notifier .stop ()
20
+ self .assertTrue (notifier .stopped )
19
21
20
22
def test_multiple_bus (self ):
21
23
with can .Bus (0 , interface = "virtual" , receive_own_messages = True ) as bus1 :
22
24
with can .Bus (1 , interface = "virtual" , receive_own_messages = True ) as bus2 :
23
25
reader = can .BufferedReader ()
24
26
notifier = can .Notifier ([bus1 , bus2 ], [reader ], 0.1 )
27
+ self .assertFalse (notifier .stopped )
25
28
msg = can .Message ()
26
29
bus1 .send (msg )
27
30
time .sleep (0.1 )
@@ -33,6 +36,30 @@ def test_multiple_bus(self):
33
36
self .assertIsNotNone (recv_msg )
34
37
self .assertEqual (recv_msg .channel , 1 )
35
38
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 )
36
63
37
64
38
65
class AsyncNotifierTest (unittest .TestCase ):
0 commit comments