Skip to content

Commit f515adf

Browse files
committed
add node id and baudrate configuration test
1 parent eaa209d commit f515adf

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

tests/example_based/test_lss.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
""" Testing LSS service """
22

3-
from durand import Node
3+
from durand import Node, scheduler
44

55
from ..mock_network import MockNetwork, TxMsg, RxMsg
66

77

8+
class MockScheduler(scheduler.AbstractScheduler):
9+
def add(self, delay: float, callback, args=(), kwargs=None) -> scheduler.TEntry:
10+
if kwargs is None:
11+
kwargs = {}
12+
callback(*args, **kwargs)
13+
14+
def cancel(self, entry: scheduler.TEntry):
15+
...
16+
17+
@property
18+
def lock(self):
19+
...
20+
21+
822
def test_global_selection():
923
"""Test example starts with a node with an undefined node id (0xFF).
1024
After the "switch state global" request to set every responder into configuration mode,
@@ -38,3 +52,59 @@ def test_global_selection():
3852
TxMsg(0x581, "43 00 10 00 00 00 00 00"), # receive the acknowledge
3953
]
4054
)
55+
56+
57+
def test_configuration():
58+
"""Test example starts with a node with node id 0x01.
59+
After the "switch state global" request to set every responder into configuration mode,
60+
the node id is set to 2.
61+
62+
Furthermore, the baudrate is set to 500 kbit/s and the configuration is stored.
63+
When the configuration is stored, the node sends an acknowledge message.
64+
65+
At last, activate bit timing is sent to the node. It is the responsibility of its callback
66+
to reinitialize the network with the new baudrate after a delay.
67+
The callback is tested with the baudrate and delay parameters.
68+
"""
69+
70+
scheduler_ = MockScheduler()
71+
scheduler.set_scheduler(scheduler_)
72+
73+
network = MockNetwork()
74+
75+
# create the node
76+
node = Node(network, node_id=0x01)
77+
78+
def baudrate_change_callback(baudrate: int, delay: float):
79+
# This callback is supposed to reinitialize the network with the new baudrate
80+
# after the delay
81+
# In this test, we just check if the callback is called with the correct parameters
82+
assert baudrate == 500_000
83+
assert delay == 1
84+
85+
node.lss.set_baudrate_change_callback(baudrate_change_callback)
86+
87+
def store_configuration_callback(baudrate: int | None, node_id: int | None):
88+
assert baudrate == 500_000
89+
assert node_id == 2
90+
91+
node.lss.set_store_configuration_callback(store_configuration_callback)
92+
93+
network.test(
94+
[
95+
RxMsg(
96+
0x7E5, "04 01 00 00 00 00 00 00"
97+
), # switch state global to configuration state
98+
99+
RxMsg(0x7E5, "11 02 00 00 00 00 00 00"), # set node id to 2
100+
TxMsg(0x7E4, "11 00 00 00 00 00 00 00"), # receive the acknowledge (success)
101+
102+
RxMsg(0x7E5, "13 00 02 00 00 00 00 00"), # set baudrate to 500 kbit/s
103+
TxMsg(0x7E4, "13 00 00 00 00 00 00 00"), # receive the acknowledge (success)
104+
105+
RxMsg(0x7E5, "17 00 00 00 00 00 00 00"), # store configuration
106+
TxMsg(0x7E4, "17 00 00 00 00 00 00 00"), # receive the acknowledge (success)
107+
108+
RxMsg(0x7E5, "15 E8 03 00 00 00 00 00"), # activate bit timing
109+
]
110+
)

0 commit comments

Comments
 (0)