Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for NMT base and NMT master #504

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address review
erlend-aasland committed Jul 9, 2024
commit c2ff667bbb06a4fb2b6a10d5703ce5df211d29f9
22 changes: 13 additions & 9 deletions test/test_nmt.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

import can
import canopen
from canopen.nmt import NMT_STATES, NMT_COMMANDS
from canopen.nmt import NMT_STATES, NMT_COMMANDS, NmtError
from .util import SAMPLE_EDS


@@ -63,19 +63,22 @@ def tearDown(self):
self.net.disconnect()

def test_nmt_master_no_heartbeat(self):
with self.assertRaisesRegex(canopen.nmt.NmtError, "heartbeat"):
with self.assertRaisesRegex(NmtError, "heartbeat"):
self.node.nmt.wait_for_heartbeat(self.TIMEOUT)
with self.assertRaisesRegex(canopen.nmt.NmtError, "boot-up"):
with self.assertRaisesRegex(NmtError, "boot-up"):
self.node.nmt.wait_for_bootup(self.TIMEOUT)

def test_nmt_master_on_heartbeat(self):
# Skip the special INITIALISING case.
for code in [st for st in NMT_STATES if st != 0]:
with self.subTest(code=code):
task = self.net.send_periodic(self.COB_ID, [code], self.PERIOD)
self.addCleanup(task.stop)
actual = self.node.nmt.wait_for_heartbeat(self.TIMEOUT)
task.stop()
try:
actual = self.node.nmt.wait_for_heartbeat(self.TIMEOUT)
except NmtError:
self.fail("Timed out waiting for heartbeat")
finally:
task.stop()
expected = NMT_STATES[code]
self.assertEqual(actual, expected)

@@ -86,13 +89,14 @@ def test_nmt_master_on_heartbeat_initialising(self):
state = self.node.nmt.wait_for_heartbeat(self.TIMEOUT)
self.assertEqual(state, "PRE-OPERATIONAL")

@unittest.expectedFailure
def test_nmt_master_on_heartbeat_unknown_state(self):
task = self.net.send_periodic(self.COB_ID, [0xcb], self.PERIOD)
self.addCleanup(task.stop)
state = self.node.nmt.wait_for_heartbeat(self.TIMEOUT)
# Expect the high bit to be masked out, and the resulting integer
# returned as it is. See gh-500 for the data type inconsistency.
self.assertEqual(state, 0x4b)
# Expect the high bit to be masked out, and and unknown state string to
# be returned.
self.assertEqual(state, "UNKNOWN STATE '75'")

def test_nmt_master_add_heartbeat_callback(self):
from threading import Event