|
| 1 | +import unittest |
| 2 | + |
| 3 | +import canopen |
| 4 | + |
| 5 | + |
| 6 | +def count_subscribers(network: canopen.Network) -> int: |
| 7 | + """Count the number of subscribers in the network.""" |
| 8 | + return sum(len(n) for n in network.subscribers.values()) |
| 9 | + |
| 10 | + |
| 11 | +class TestLocalNode(unittest.TestCase): |
| 12 | + |
| 13 | + @classmethod |
| 14 | + def setUpClass(cls): |
| 15 | + cls.network = canopen.Network() |
| 16 | + cls.network.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0 |
| 17 | + cls.network.connect(interface="virtual") |
| 18 | + |
| 19 | + cls.node = canopen.LocalNode(2, canopen.objectdictionary.ObjectDictionary()) |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def tearDownClass(cls): |
| 23 | + cls.network.disconnect() |
| 24 | + |
| 25 | + def test_associate_network(self): |
| 26 | + # Need to store the number of subscribers before associating because the |
| 27 | + # network implementation automatically adds subscribers to the list |
| 28 | + n_subscribers = count_subscribers(self.network) |
| 29 | + |
| 30 | + # Associating the network with the local node |
| 31 | + self.node.associate_network(self.network) |
| 32 | + self.assertIs(self.node.network, self.network) |
| 33 | + self.assertIs(self.node.sdo.network, self.network) |
| 34 | + self.assertIs(self.node.tpdo.network, self.network) |
| 35 | + self.assertIs(self.node.rpdo.network, self.network) |
| 36 | + self.assertIs(self.node.nmt.network, self.network) |
| 37 | + self.assertIs(self.node.emcy.network, self.network) |
| 38 | + |
| 39 | + # Test that its not possible to associate the network multiple times |
| 40 | + with self.assertRaises(RuntimeError) as cm: |
| 41 | + self.node.associate_network(self.network) |
| 42 | + self.assertIn("already associated with a network", str(cm.exception)) |
| 43 | + |
| 44 | + # Test removal of the network. The count of subscribers should |
| 45 | + # be the same as before the association |
| 46 | + self.node.remove_network() |
| 47 | + uninitalized = canopen.network._UNINITIALIZED_NETWORK |
| 48 | + self.assertIs(self.node.network, uninitalized) |
| 49 | + self.assertIs(self.node.sdo.network, uninitalized) |
| 50 | + self.assertIs(self.node.tpdo.network, uninitalized) |
| 51 | + self.assertIs(self.node.rpdo.network, uninitalized) |
| 52 | + self.assertIs(self.node.nmt.network, uninitalized) |
| 53 | + self.assertIs(self.node.emcy.network, uninitalized) |
| 54 | + self.assertEqual(count_subscribers(self.network), n_subscribers) |
| 55 | + |
| 56 | + # Test that its possible to deassociate the network multiple times |
| 57 | + self.node.remove_network() |
| 58 | + |
| 59 | + |
| 60 | +class TestRemoteNode(unittest.TestCase): |
| 61 | + |
| 62 | + @classmethod |
| 63 | + def setUpClass(cls): |
| 64 | + cls.network = canopen.Network() |
| 65 | + cls.network.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0 |
| 66 | + cls.network.connect(interface="virtual") |
| 67 | + |
| 68 | + cls.node = canopen.RemoteNode(2, canopen.objectdictionary.ObjectDictionary()) |
| 69 | + |
| 70 | + @classmethod |
| 71 | + def tearDownClass(cls): |
| 72 | + cls.network.disconnect() |
| 73 | + |
| 74 | + def test_associate_network(self): |
| 75 | + # Need to store the number of subscribers before associating because the |
| 76 | + # network implementation automatically adds subscribers to the list |
| 77 | + n_subscribers = count_subscribers(self.network) |
| 78 | + |
| 79 | + # Associating the network with the local node |
| 80 | + self.node.associate_network(self.network) |
| 81 | + self.assertIs(self.node.network, self.network) |
| 82 | + self.assertIs(self.node.sdo.network, self.network) |
| 83 | + self.assertIs(self.node.tpdo.network, self.network) |
| 84 | + self.assertIs(self.node.rpdo.network, self.network) |
| 85 | + self.assertIs(self.node.nmt.network, self.network) |
| 86 | + |
| 87 | + # Test that its not possible to associate the network multiple times |
| 88 | + with self.assertRaises(RuntimeError) as cm: |
| 89 | + self.node.associate_network(self.network) |
| 90 | + self.assertIn("already associated with a network", str(cm.exception)) |
| 91 | + |
| 92 | + # Test removal of the network. The count of subscribers should |
| 93 | + # be the same as before the association |
| 94 | + self.node.remove_network() |
| 95 | + uninitalized = canopen.network._UNINITIALIZED_NETWORK |
| 96 | + self.assertIs(self.node.network, uninitalized) |
| 97 | + self.assertIs(self.node.sdo.network, uninitalized) |
| 98 | + self.assertIs(self.node.tpdo.network, uninitalized) |
| 99 | + self.assertIs(self.node.rpdo.network, uninitalized) |
| 100 | + self.assertIs(self.node.nmt.network, uninitalized) |
| 101 | + self.assertEqual(count_subscribers(self.network), n_subscribers) |
| 102 | + |
| 103 | + # Test that its possible to deassociate the network multiple times |
| 104 | + self.node.remove_network() |
0 commit comments