Skip to content

Commit

Permalink
Tests: Add a test that checks for gh issue 128 (missing device list c…
Browse files Browse the repository at this point in the history
…leanup)
  • Loading branch information
bnjmnp committed Feb 3, 2024
1 parent 2680b42 commit fba74fd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
32 changes: 22 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@


import time
import collections
import threading
import dataclasses
import pytest

import pysoem
Expand All @@ -20,6 +20,13 @@ class PySoemTestEnvironment:
EL3002_PRODUCT_CODE = 0x0bba3052
EL1259_PRODUCT_CODE = 0x04eb3052

@dataclasses.dataclass
class SlaveSet:
name: str
vendor_id: int
product_code: int
config_func: None

def __init__(self, ifname):
self._is_overlapping_enabled = None
self._ifname = ifname
Expand All @@ -32,12 +39,16 @@ def __init__(self, ifname):
self._ch_thread_stop_event = threading.Event()
self._actual_wkc = 0

self.SlaveSet = collections.namedtuple('SlaveSet', 'name vendor_id product_code config_func')

self.el3002_config_func = None
self.el1259_config_func = None
self.el1259_setup_func = None
self._expected_slave_layout = None

self._expected_slave_layout = {
0: self.SlaveSet('XMC43-Test-Device', 0, 0x12783456, None),
1: self.SlaveSet('EK1100', self.BECKHOFF_VENDOR_ID, self.EK1100_PRODUCT_CODE, None),
2: self.SlaveSet('EL3002', self.BECKHOFF_VENDOR_ID, self.EL3002_PRODUCT_CODE, None),
3: self.SlaveSet('EL1259', self.BECKHOFF_VENDOR_ID, self.EL1259_PRODUCT_CODE, None),
}

def config_init(self):
self._master.open(self._ifname)
Expand All @@ -64,12 +75,13 @@ def go_to_preop_state(self):
def config_map(self, overlapping_enable=False):
self._is_overlapping_enabled = overlapping_enable

self._expected_slave_layout = {
0: self.SlaveSet('XMC43-Test-Device', 0, 0x12783456, None),
1: self.SlaveSet('EK1100', self.BECKHOFF_VENDOR_ID, self.EK1100_PRODUCT_CODE, None),
2: self.SlaveSet('EL3002', self.BECKHOFF_VENDOR_ID, self.EL3002_PRODUCT_CODE, self.el3002_config_func),
3: self.SlaveSet('EL1259', self.BECKHOFF_VENDOR_ID, self.EL1259_PRODUCT_CODE, self.el1259_config_func),
}
# pull in the latest config_function into the _expected_slave_layout
for device in self._expected_slave_layout.values():
if device.name == 'EL3002':
device.config_func = self.el3002_config_func
elif device.name == 'EL1259':
device.config_func = self.el1259_config_func

self._master.config_dc()
for i, slave in enumerate(self._master.slaves):
assert slave.man == self._expected_slave_layout[i].vendor_id
Expand Down
13 changes: 12 additions & 1 deletion tests/pysoem_basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,15 @@ def el1259_setup_func(slave):

pysoem_env.config_map() # el1259_setup_func is expected to be called here

assert setup_func_was_called
assert setup_func_was_called


def test_call_config_init_twice(pysoem_env):
"""In older versions of pysoem there was an issue calling config_init() multiple times.
Every time config_init() was called again, the slaves list was extended not updated.
"""
pysoem_env.config_init()
assert len(pysoem_env.get_master().slaves) == len(pysoem_env._expected_slave_layout)
pysoem_env.config_init()
assert len(pysoem_env.get_master().slaves) == len(pysoem_env._expected_slave_layout)

0 comments on commit fba74fd

Please sign in to comment.