Skip to content

Commit

Permalink
Make Network.notifier timeouts customizable (fixes #530) (#536)
Browse files Browse the repository at this point in the history
* Add configurable timeouts for Network.notifier.

Move the hard-coded 1 second maximum cycle duration passed during
can.Notifier construction to a class variable NOTIFIER_CYCLE.
Explicitly specify the timeout to stop the notifier in another class
variable NOTIFIER_SHUTDOWN_TIMEOUT, matching the default used in
python-can.

These can be overridden per instance if needed.

* Do not wait for notifier thread shutdown during tests.

Explicitly set the NOTIFIER_SHUTDOWN_TIMEOUT to zero in all Network
instances created for unit tests.

This reduces total test running time from ~30 to ~9 seconds when run
locally.
  • Loading branch information
acolomb authored Aug 14, 2024
1 parent 932c621 commit 6ef4f5e
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions canopen/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
class Network(MutableMapping):
"""Representation of one CAN bus containing one or more nodes."""

NOTIFIER_CYCLE: float = 1.0 #: Maximum waiting time for one notifier iteration.
NOTIFIER_SHUTDOWN_TIMEOUT: float = 5.0 #: Maximum waiting time to stop notifiers.

def __init__(self, bus: Optional[can.BusABC] = None):
"""
:param can.BusABC bus:
Expand Down Expand Up @@ -105,7 +108,7 @@ def connect(self, *args, **kwargs) -> Network:
if self.bus is None:
self.bus = can.Bus(*args, **kwargs)
logger.info("Connected to '%s'", self.bus.channel_info)
self.notifier = can.Notifier(self.bus, self.listeners, 1)
self.notifier = can.Notifier(self.bus, self.listeners, self.NOTIFIER_CYCLE)
return self

def disconnect(self) -> None:
Expand All @@ -117,7 +120,7 @@ def disconnect(self) -> None:
if hasattr(node, "pdo"):
node.pdo.stop()
if self.notifier is not None:
self.notifier.stop()
self.notifier.stop(self.NOTIFIER_SHUTDOWN_TIMEOUT)
if self.bus is not None:
self.bus.shutdown()
self.bus = None
Expand Down
1 change: 1 addition & 0 deletions test/test_emcy.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def setUp(self):
self.txbus = can.Bus(interface="virtual")
self.rxbus = can.Bus(interface="virtual")
self.net = canopen.Network(self.txbus)
self.net.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
self.net.connect()
self.emcy = canopen.emcy.EmcyProducer(0x80 + 1)
self.emcy.network = self.net
Expand Down
4 changes: 4 additions & 0 deletions test/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class TestSDO(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.network1 = canopen.Network()
cls.network1.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
cls.network1.connect("test", interface="virtual")
cls.remote_node = cls.network1.add_node(2, SAMPLE_EDS)

cls.network2 = canopen.Network()
cls.network2.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
cls.network2.connect("test", interface="virtual")
cls.local_node = cls.network2.create_node(2, SAMPLE_EDS)

Expand Down Expand Up @@ -176,10 +178,12 @@ class TestPDO(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.network1 = canopen.Network()
cls.network1.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
cls.network1.connect("test", interface="virtual")
cls.remote_node = cls.network1.add_node(2, SAMPLE_EDS)

cls.network2 = canopen.Network()
cls.network2.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
cls.network2.connect("test", interface="virtual")
cls.local_node = cls.network2.create_node(2, SAMPLE_EDS)

Expand Down
3 changes: 3 additions & 0 deletions test/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class TestNetwork(unittest.TestCase):

def setUp(self):
self.network = canopen.Network()
self.network.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0

def test_network_add_node(self):
# Add using str.
Expand Down Expand Up @@ -328,6 +329,7 @@ def test_scanner_search(self):
self.addCleanup(txbus.shutdown)

net = canopen.Network(txbus)
net.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
net.connect()
self.addCleanup(net.disconnect)

Expand All @@ -347,6 +349,7 @@ def test_scanner_search(self):
def test_scanner_search_limit(self):
bus = can.Bus(interface="virtual", receive_own_messages=True)
net = canopen.Network(bus)
net.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
net.connect()
self.addCleanup(net.disconnect)

Expand Down
3 changes: 3 additions & 0 deletions test/test_nmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def setUp(self):
receive_own_messages=True,
)
net = canopen.Network(bus)
net.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
net.connect()
with self.assertLogs():
node = net.add_node(self.NODE_ID, SAMPLE_EDS)
Expand Down Expand Up @@ -127,11 +128,13 @@ def test_nmt_master_node_guarding(self):
class TestNmtSlave(unittest.TestCase):
def setUp(self):
self.network1 = canopen.Network()
self.network1.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
self.network1.connect("test", interface="virtual")
with self.assertLogs():
self.remote_node = self.network1.add_node(2, SAMPLE_EDS)

self.network2 = canopen.Network()
self.network2.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
self.network2.connect("test", interface="virtual")
with self.assertLogs():
self.local_node = self.network2.create_node(2, SAMPLE_EDS)
Expand Down
2 changes: 2 additions & 0 deletions test/test_sdo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def _send_message(self, can_id, data, remote=False):

def setUp(self):
network = canopen.Network()
network.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
network.send_message = self._send_message
node = network.add_node(2, SAMPLE_EDS)
node.sdo.RESPONSE_TIMEOUT = 0.01
Expand Down Expand Up @@ -181,6 +182,7 @@ def _send_message(self, can_id, data, remote=False):

def setUp(self):
network = canopen.Network()
network.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
network.send_message = self._send_message
node = network.add_node(2, DATATYPES_EDS)
node.sdo.RESPONSE_TIMEOUT = 0.01
Expand Down
1 change: 1 addition & 0 deletions test/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class TestSync(unittest.TestCase):
def setUp(self):
self.net = canopen.Network()
self.net.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
self.net.connect(interface="virtual")
self.sync = canopen.sync.SyncProducer(self.net)
self.rxbus = can.Bus(interface="virtual")
Expand Down
1 change: 1 addition & 0 deletions test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class TestTime(unittest.TestCase):

def test_time_producer(self):
network = canopen.Network()
network.NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
network.connect(interface="virtual", receive_own_messages=True)
producer = canopen.timestamp.TimeProducer(network)
producer.transmit(1486236238)
Expand Down

0 comments on commit 6ef4f5e

Please sign in to comment.