Skip to content

Commit d02d5ee

Browse files
committed
Updated from review
1 parent 7eb8fbe commit d02d5ee

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

canopen/network.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def unsubscribe(self, can_id, callback=None) -> None:
7979
del self.subscribers[can_id]
8080
else:
8181
self.subscribers[can_id].remove(callback)
82+
# Remove empty list entry
83+
if not self.subscribers[can_id]:
84+
del self.subscribers[can_id]
8285

8386
def connect(self, *args, **kwargs) -> Network:
8487
"""Connect to CAN bus using python-can.

canopen/node/local.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ def __init__(
3939
self.emcy = EmcyProducer(0x80 + self.id)
4040

4141
def associate_network(self, network: canopen.network.Network):
42-
if self.network is not canopen.network._UNINITIALIZED_NETWORK:
43-
# Unsubscribe from old network (to prevent double subscription)
44-
self.remove_network()
42+
if self.has_network():
43+
raise RuntimeError("Node is already associated with a network")
4544
self.network = network
4645
self.sdo.network = network
4746
self.tpdo.network = network
@@ -53,7 +52,7 @@ def associate_network(self, network: canopen.network.Network):
5352

5453
def remove_network(self) -> None:
5554
# Make it safe to call this method multiple times
56-
if self.network is canopen.network._UNINITIALIZED_NETWORK:
55+
if not self.has_network():
5756
return
5857
self.network.unsubscribe(self.sdo.rx_cobid, self.sdo.on_request)
5958
self.network.unsubscribe(0, self.nmt.on_command)

canopen/node/remote.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ def __init__(
5151
self.load_configuration()
5252

5353
def associate_network(self, network: canopen.network.Network):
54-
if self.network is not canopen.network._UNINITIALIZED_NETWORK:
55-
# Unsubscribe from old network (to prevent double subscriptions)
56-
self.remove_network()
54+
if self.has_network():
55+
raise RuntimeError("Node is already associated with a network")
5756
self.network = network
5857
self.sdo.network = network
5958
self.pdo.network = network
@@ -68,7 +67,7 @@ def associate_network(self, network: canopen.network.Network):
6867

6968
def remove_network(self) -> None:
7069
# Make it safe to call this method multiple times
71-
if self.network is canopen.network._UNINITIALIZED_NETWORK:
70+
if not self.has_network():
7271
return
7372
for sdo in self.sdo_channels:
7473
self.network.unsubscribe(sdo.tx_cobid, sdo.on_response)

test/test_node.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33

44
import canopen
5+
import canopen.network
56

67
from .util import SAMPLE_EDS
78

@@ -14,15 +15,13 @@ def count_subscribers(network: canopen.Network) -> int:
1415

1516

1617
class TestLocalNode(unittest.TestCase):
17-
"""
18-
Test local node.
19-
"""
18+
"""Unit tests for the LocalNode class."""
2019

2120
@classmethod
2221
def setUpClass(cls):
2322
cls.network = canopen.Network()
2423
cls.network.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
25-
cls.network.connect("test", interface="virtual")
24+
cls.network.connect(interface="virtual")
2625

2726
cls.node = canopen.LocalNode(2, SAMPLE_EDS)
2827

@@ -46,11 +45,10 @@ def test_associate_network(self):
4645
self.assertIs(self.node.nmt.network, self.network)
4746
self.assertIs(self.node.emcy.network, self.network)
4847

49-
# Test that its possible to associate the network multiple times
50-
# by checking that the number of subscribers remains the same
51-
count = count_subscribers(self.network)
52-
self.node.associate_network(self.network)
53-
self.assertEqual(count_subscribers(self.network), count)
48+
# Test that its not possible to associate the network multiple times
49+
with self.assertRaises(RuntimeError) as cm:
50+
self.node.associate_network(self.network)
51+
self.assertIn("already associated with a network", str(cm.exception))
5452

5553
# Test removal of the network. The count of subscribers should
5654
# be the same as before the association
@@ -69,15 +67,13 @@ def test_associate_network(self):
6967

7068

7169
class TestRemoteNode(unittest.TestCase):
72-
"""
73-
Test remote node.
74-
"""
70+
"""Unittests for the RemoteNode class."""
7571

7672
@classmethod
7773
def setUpClass(cls):
7874
cls.network = canopen.Network()
7975
cls.network.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
80-
cls.network.connect("test", interface="virtual")
76+
cls.network.connect(interface="virtual")
8177

8278
cls.node = canopen.RemoteNode(2, SAMPLE_EDS)
8379

@@ -100,11 +96,10 @@ def test_associate_network(self):
10096
self.assertIs(self.node.rpdo.network, self.network)
10197
self.assertIs(self.node.nmt.network, self.network)
10298

103-
# Test that its possible to associate the network multiple times
104-
# by checking that the number of subscribers remains the same
105-
count = count_subscribers(self.network)
106-
self.node.associate_network(self.network)
107-
self.assertEqual(count_subscribers(self.network), count)
99+
# Test that its not possible to associate the network multiple times
100+
with self.assertRaises(RuntimeError) as cm:
101+
self.node.associate_network(self.network)
102+
self.assertIn("already associated with a network", str(cm.exception))
108103

109104
# Test removal of the network. The count of subscribers should
110105
# be the same as before the association

0 commit comments

Comments
 (0)