1212 import isotp
1313 import can
1414 s = isotp .socket ()
15- s .bind (_interface_name ,rxid = 1 ,txid = 2 )
15+ s .bind (_interface_name , isotp . Address ( rxid = 1 , txid = 2 ) )
1616 s .close ()
1717 _STACK_POSSIBLE = True
1818except Exception as e :
2525except Exception as e :
2626 _AISOTP_POSSIBLE = False
2727
28+
2829class TestIsoTPSocketConnection (UdsTest ):
2930
3031 def setUp (self ):
3132 self .tpsock1 = StubbedIsoTPSocket (timeout = 0.1 )
3233 self .tpsock2 = StubbedIsoTPSocket (timeout = 0.1 )
3334
3435 def test_open (self ):
35- conn = IsoTPSocketConnection (interface = 'vcan0' , rxid = 0x001 , txid = 0x002 , tpsock = self .tpsock1 , name = 'unittest' )
36+ addr = isotp .Address (isotp .AddressingMode .Normal_11bits , rxid = 0x001 , txid = 0x002 )
37+ conn = IsoTPSocketConnection (interface = 'vcan0' , address = addr , tpsock = self .tpsock1 , name = 'unittest' )
3638 self .assertFalse (conn .is_open ())
3739 conn .open ()
3840 self .assertTrue (conn .is_open ())
3941 conn .close ()
4042 self .assertFalse (conn .is_open ())
4143
4244 def test_transmit (self ):
43- conn1 = IsoTPSocketConnection (interface = 'vcan0' , rxid = 0x100 , txid = 0x101 , tpsock = self .tpsock1 , name = 'unittest' )
44- conn2 = IsoTPSocketConnection (interface = 'vcan0' , rxid = 0x101 , txid = 0x100 , tpsock = self .tpsock2 , name = 'unittest' )
45+ addr1 = isotp .Address (isotp .AddressingMode .Normal_11bits , rxid = 0x100 , txid = 0x101 )
46+ addr2 = isotp .Address (isotp .AddressingMode .Normal_11bits , rxid = 0x101 , txid = 0x100 )
47+ conn1 = IsoTPSocketConnection (interface = 'vcan0' , address = addr1 , tpsock = self .tpsock1 , name = 'unittest' )
48+ conn2 = IsoTPSocketConnection (interface = 'vcan0' , address = addr2 , tpsock = self .tpsock2 , name = 'unittest' )
4549
4650 with conn1 .open ():
4751 with conn2 .open ():
4852 payload1 = b"\x00 \x01 \x02 \x03 \x04 "
4953 conn1 .send (payload1 )
50- payload2 = conn2 .wait_frame (timeout = 0.3 )
54+ payload2 = conn2 .wait_frame (timeout = 0.3 , exception = True )
5155 self .assertEqual (payload1 , payload2 )
5256
57+
5358class TestSocketConnection (UdsTest ):
5459 def server_sock_thread_task (self ):
55- self .thread_started = True
60+ self .thread_started = True
5661 self .sock1 , addr = self .server_sock .accept ()
5762
5863 def setUp (self ):
@@ -112,6 +117,7 @@ def test_transmit(self):
112117 payload2 = conn2 .wait_frame (timeout = 1 , exception = True )
113118 self .assertEqual (payload1 , payload2 )
114119
120+
115121class TestQueueConnection (UdsTest ):
116122 def setUp (self ):
117123 self .conn = QueueConnection (name = 'unittest' )
@@ -136,7 +142,7 @@ def test_send(self):
136142 self .assertEqual (frame , payload )
137143
138144 def test_truncate (self ):
139- payload = b"\x00 \x01 \x02 \x03 " * 5000
145+ payload = b"\x00 \x01 \x02 \x03 " * 5000
140146 self .conn .send (payload )
141147 frame = self .conn .touserqueue .get ()
142148 self .assertEqual (len (frame ), 4095 )
@@ -161,48 +167,48 @@ def test_reopen(self):
161167
162168 self .assertTrue (self .conn .touserqueue .empty ())
163169
170+
164171@unittest .skipIf (_STACK_POSSIBLE == False , 'Cannot test TestPythonIsoTpConnection. %s' % _STACK_UNVAILABLE_REASON )
165172class TestPythonIsoTpConnection (UdsTest ):
166173 def __init__ (self , * args , ** kwargs ):
167174 UdsTest .__init__ (self , * args , ** kwargs )
168175 if not hasattr (self .__class__ , '_next_id' ):
169- self .__class__ ._next_id = 1
176+ self .__class__ ._next_id = 1
170177
171178 self .stack_txid = self .__class__ ._next_id
172- self .stack_rxid = self .__class__ ._next_id + 1
179+ self .stack_rxid = self .__class__ ._next_id + 1
173180 self .__class__ ._next_id += 2
174181
175182 def make_bus (self ):
176183 return can .interface .Bus (bustype = 'socketcan' , channel = 'vcan0' , bitrate = 500000 , receive_own_messages = True )
177184
178185 def setUp (self ):
179186 self .vcan0_bus = self .make_bus ()
187+ self .reader = can .BufferedReader ()
188+ self .notifier = can .Notifier (self .vcan0_bus , [self .reader ])
180189 addr = isotp .Address (isotp .AddressingMode .Normal_11bits , rxid = self .stack_rxid , txid = self .stack_txid )
181- self .conn = PythonIsoTpConnection (isotp .CanStack (bus = self .vcan0_bus , address = addr ), name = 'unittest' )
190+ self .conn = PythonIsoTpConnection (isotp .NotifierBasedCanStack (bus = self .vcan0_bus , notifier = self . notifier , address = addr ), name = 'unittest' )
182191 self .conn .open ()
183192
184193 def test_open (self ):
185194 self .assertTrue (self .conn .is_open ())
186195
187196 def test_receive (self ):
188- self .vcan0_bus .send (can .Message (arbitration_id = self .stack_rxid , data = b"\x03 \x01 \x02 \x03 " , is_extended_id = False ))
197+ self .vcan0_bus .send (can .Message (arbitration_id = self .stack_rxid , data = b"\x03 \x01 \x02 \x03 " , is_extended_id = False ))
189198 frame = self .conn .wait_frame (timeout = 1 )
190199 self .assertEqual (frame , b"\x01 \x02 \x03 " )
191200
192201 def test_send (self ):
193202 self .conn .send (b"\xAA \xBB \xCC \xDD \xEE \xFF " )
194- t1 = time .time ()
195- msg = self .vcan0_bus .recv (1 )
203+ msg = self .reader .get_message (1 )
196204 self .assertIsNotNone (msg )
197205 self .assertEqual (msg .data , b'\x06 \xAA \xBB \xCC \xDD \xEE \xFF ' )
198206
199207 def test_reopen (self ):
200208 self .conn .send (b"\x0A \x0B \x0C \x0D " )
201- self .vcan0_bus .send (can .Message (arbitration_id = self .stack_rxid , data = b"\x03 \x01 \x02 \x03 " , is_extended_id = False ))
209+ self .vcan0_bus .send (can .Message (arbitration_id = self .stack_rxid , data = b"\x03 \x01 \x02 \x03 " , is_extended_id = False ))
202210 self .conn .close ()
203- self .vcan0_bus .shutdown ()
204- self .vcan0_bus = self .make_bus ()
205- self .conn .open (bus = self .vcan0_bus )
211+ self .conn .open ()
206212
207213 with self .assertRaises (TimeoutException ):
208214 self .conn .wait_frame (timeout = 0.05 , exception = True )
@@ -211,8 +217,10 @@ def test_reopen(self):
211217
212218 def tearDown (self ):
213219 self .conn .close ()
220+ self .notifier .stop ()
214221 self .vcan0_bus .shutdown ()
215222
223+
216224@unittest .skipIf (_AISOTP_POSSIBLE == False , "aisotp module is not present." )
217225class TestSyncAioIsotpConnection (UdsTest ):
218226
@@ -232,7 +240,7 @@ def test_open(self):
232240 def test_transmit (self ):
233241 conn0 = SyncAioIsotpConnection (interface = "virtual" , channel = 0 , bitrate = 500000 , rx_id = 0x123 , tx_id = 0x456 , name = "unittest" )
234242 conn1 = SyncAioIsotpConnection (interface = "virtual" , channel = 0 , bitrate = 500000 , rx_id = 0x456 , tx_id = 0x123 , name = "unittest" )
235-
243+
236244 with conn0 .open ():
237245 with conn1 .open ():
238246 tx_data = bytes ([i for i in range (256 )])
0 commit comments