Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion pymodbus/server/simulator/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class CallTypeResponse:
clear_after: int = 1


class ModbusSimulatorServer:
class ModbusSimulatorServer: # pylint: disable=too-many-instance-attributes
"""**ModbusSimulatorServer**.

:param modbus_server: Server name in json file (default: "server")
Expand Down Expand Up @@ -219,6 +219,7 @@ def __init__(
self.call_response = CallTypeResponse()
app_key = getattr(web, 'AppKey', str) # fall back to str for aiohttp < 3.9.0
self.api_key = app_key("modbus_server")
self.ready_event = asyncio.Event()

async def start_modbus_server(self, app):
"""Start Modbus server as asyncio task."""
Expand Down Expand Up @@ -253,6 +254,7 @@ async def run_forever(self, only_start=False):
await self.runner.setup()
self.site = web.TCPSite(self.runner, self.http_host, self.http_port)
await self.site.start()
self.ready_event.set()
except Exception as exc:
Log.error("Error starting http server, reason: {}", exc)
raise exc
Expand Down
21 changes: 8 additions & 13 deletions test/server/test_simulator_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Test simulator API."""
import asyncio
import json

import pytest
Expand Down Expand Up @@ -127,20 +126,16 @@ async def simulator(self, tmp_path):
json_file = config_path
)

# Run the simulator in the current event loop. Store the task so they live
# until the test is done.
loop = asyncio.get_running_loop()
task = loop.create_task(simulator.run_forever(only_start=True))
# this will finish almost immediately; no need to keep a task
await simulator.run_forever(only_start=True)

# TODO: Make a better way to wait for the simulator to start
await asyncio.sleep(1)
# wait for the simulator to start
await simulator.ready_event.wait()

yield simulator

# Stop the simulator after the test is done
task.cancel()
await task
await simulator.stop()
try:
yield simulator
finally:
await simulator.stop()

@pytest.mark.asyncio
async def test_registers_json_valid(self, client, simulator):
Expand Down