From fba74fdabda803c744e7d51788ce161dfb346e5d Mon Sep 17 00:00:00 2001 From: bnjmnp Date: Sat, 3 Feb 2024 15:14:55 +0100 Subject: [PATCH] Tests: Add a test that checks for gh issue 128 (missing device list cleanup) --- tests/conftest.py | 32 ++++++++++++++++++++++---------- tests/pysoem_basic_test.py | 13 ++++++++++++- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 705223c..936f40e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,8 @@ import time -import collections import threading +import dataclasses import pytest import pysoem @@ -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 @@ -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) @@ -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 diff --git a/tests/pysoem_basic_test.py b/tests/pysoem_basic_test.py index ba9b577..ce8a8d8 100644 --- a/tests/pysoem_basic_test.py +++ b/tests/pysoem_basic_test.py @@ -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 \ No newline at end of file + 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)