From 6b93d92a0210ee3a97ff98aa7a49407fa70eeff0 Mon Sep 17 00:00:00 2001 From: Hangyu Fan Date: Tue, 18 Jul 2023 11:07:03 +0800 Subject: [PATCH 1/2] Bug fix for #1662 --- pymodbus/server/async_io.py | 1 + test/test_simulator.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/pymodbus/server/async_io.py b/pymodbus/server/async_io.py index db83c1d81..8c22cf52d 100644 --- a/pymodbus/server/async_io.py +++ b/pymodbus/server/async_io.py @@ -269,6 +269,7 @@ def __init__( broadcast_enable=False, response_manipulator=None, request_tracer=None, + **kwargs, # pylint: disable=unused-argument ): """Initialize the socket server. diff --git a/test/test_simulator.py b/test/test_simulator.py index 5366a5605..dc80d0c6b 100644 --- a/test/test_simulator.py +++ b/test/test_simulator.py @@ -1,10 +1,13 @@ """Test datastore.""" import copy +import json +from unittest.mock import mock_open, patch import pytest from pymodbus.datastore import ModbusSimulatorContext from pymodbus.datastore.simulator import Cell, CellType, Label +from pymodbus.server import ModbusSimulatorServer FX_READ_BIT = 1 @@ -94,6 +97,24 @@ class TestSimulator: "repeat": [{"addr": [0, 48], "to": [49, 147]}], } + default_server_config = { + "server": { + "comm": "tcp", + "host": "0.0.0.0", + "port": 5020, + "ignore_missing_slaves": False, + "framer": "socket", + "identity": { + "VendorName": "pymodbus", + "ProductCode": "PM", + "VendorUrl": "https://github.com/pymodbus-dev/pymodbus/", + "ProductName": "pymodbus Server", + "ModelName": "pymodbus Server", + "MajorMinorRevision": "3.1.0", + }, + }, + } + test_registers = [ Cell(), Cell(), @@ -539,3 +560,18 @@ def test_simulator_action_random(self, celltype, minval, maxval): regs, is_int ) assert minval <= new_value <= maxval + + async def test_init_simulator(self): + """Test init simulator server""" + with patch( + "builtins.open", + mock_open( + read_data=json.dumps( + { + "server_list": self.default_server_config, + "device_list": {"device": self.default_config}, + } + ) + ), + ): + ModbusSimulatorServer() From 23b487ebed8abcc0856bae88bb7f94edeba407fb Mon Sep 17 00:00:00 2001 From: Hangyu Fan Date: Tue, 18 Jul 2023 14:54:27 +0800 Subject: [PATCH 2/2] remove server["loop"] in ModbusSimulatorServer.__init__ --- pymodbus/server/async_io.py | 1 - pymodbus/server/simulator/http_server.py | 1 - test/test_simulator.py | 2 +- 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pymodbus/server/async_io.py b/pymodbus/server/async_io.py index 8c22cf52d..db83c1d81 100644 --- a/pymodbus/server/async_io.py +++ b/pymodbus/server/async_io.py @@ -269,7 +269,6 @@ def __init__( broadcast_enable=False, response_manipulator=None, request_tracer=None, - **kwargs, # pylint: disable=unused-argument ): """Initialize the socket server. diff --git a/pymodbus/server/simulator/http_server.py b/pymodbus/server/simulator/http_server.py index 02e3c4ea9..3ed05da64 100644 --- a/pymodbus/server/simulator/http_server.py +++ b/pymodbus/server/simulator/http_server.py @@ -149,7 +149,6 @@ def __init__( else: custom_actions_dict = None server = setup["server_list"][modbus_server] - server["loop"] = asyncio.get_running_loop() if server["comm"] != "serial": server["address"] = (server["host"], server["port"]) del server["host"] diff --git a/test/test_simulator.py b/test/test_simulator.py index d81e808ee..4962550a8 100644 --- a/test/test_simulator.py +++ b/test/test_simulator.py @@ -561,7 +561,7 @@ def test_simulator_action_random(self, celltype, minval, maxval): ) assert minval <= new_value <= maxval - async def test_init_simulator(self): + async def test_init_simulator_server_tcp(self): """Test init simulator server""" with patch( "builtins.open",