File: contextmanager_test.py

package info (click to toggle)
python-can 4.6.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,428 kB
  • sloc: python: 27,154; makefile: 32; sh: 16
file content (43 lines) | stat: -rw-r--r-- 1,175 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python

"""
This module tests the context manager of Bus and Notifier classes
"""

import unittest

import can


class ContextManagerTest(unittest.TestCase):
    def setUp(self):
        data = [0, 1, 2, 3, 4, 5, 6, 7]
        self.msg_send = can.Message(
            is_extended_id=False, arbitration_id=0x100, data=data
        )

    def test_open_buses(self):
        with (
            can.Bus(interface="virtual") as bus_send,
            can.Bus(interface="virtual") as bus_recv,
        ):
            bus_send.send(self.msg_send)
            msg_recv = bus_recv.recv()

            # Receiving a frame with data should evaluate msg_recv to True
            self.assertTrue(msg_recv)

    def test_use_closed_bus(self):
        with (
            can.Bus(interface="virtual") as bus_send,
            can.Bus(interface="virtual") as bus_recv,
        ):
            bus_send.send(self.msg_send)

        # Receiving a frame after bus has been closed should raise a CanException
        self.assertRaises(can.CanError, bus_recv.recv)
        self.assertRaises(can.CanError, bus_send.send, self.msg_send)


if __name__ == "__main__":
    unittest.main()