What changed, and why it matters
This commit only moves the code that starts and stops a small test server into the test server class itself. It is a cleanup of the test code and does not change any user-facing behavior or fix a security issue.
No security action needed; this is a normal test refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors test infrastructure: tests/toyserver.py gains start() and stop() methods that wrap aiorpcx.serve_rs and asyncio.Server.close/wait_closed, while tests/test_interface.py replaces inline server setup/teardown with calls to those methods. The server still binds to 127.0.0.1 and is only used in unit tests. No production code is modified.
Changed components
tests/toyserver.pytests/test_interface.pyInspect captured patch +18 / −7
diff --git a/tests/test_interface.py b/tests/test_interface.py
index 6087a00..d1dccd1 100644
--- a/tests/test_interface.py
+++ b/tests/test_interface.py
@@ -110,21 +110,17 @@ class TestInterface(ElectrumTestCase):
async def asyncSetUp(self):
await super().asyncSetUp()
self._toyserver = ToyServer()
- session_factory = partial(ToyServerSession, toyserver=self._toyserver)
- self._server: asyncio.base_events.Server = await aiorpcx.serve_rs(session_factory, "127.0.0.1")
- server_socket_addr = self._server.sockets[0].getsockname()
- self._server_port = server_socket_addr[1]
+ await self._toyserver.start()
self.network = MockNetwork(config=self.config)
async def asyncTearDown(self):
if self.network.interface:
await self.network.interface.close()
- self._server.close()
- await self._server.wait_closed()
+ await self._toyserver.stop()
await super().asyncTearDown()
async def _start_iface_and_wait_for_sync(self):
- interface = Interface(network=self.network, server=ServerAddr(host="127.0.0.1", port=self._server_port, protocol="t"))
+ interface = Interface(network=self.network, server=ServerAddr(host="127.0.0.1", port=self._toyserver.server_port, protocol="t"))
interface.client_name = lambda: "alice"
self.network.interface = interface
async with util.async_timeout(5):
diff --git a/tests/toyserver.py b/tests/toyserver.py
index 7dfce4a..e19be22 100644
--- a/tests/toyserver.py
+++ b/tests/toyserver.py
@@ -2,7 +2,9 @@
# Distributed under the MIT software license, see the accompanying
# file LICENCE or http://www.opensource.org/licenses/mit-license.php
+import asyncio
import collections
+from functools import partial
from typing import Optional, Sequence, Iterable, List, Set
import aiorpcx
@@ -32,6 +34,9 @@ _BLOCK_HEADERS: List[bytes] = [
class ToyServer:
"""Electrum Server backend"""
+ asyncio_server: asyncio.base_events.Server
+ server_port: int
+
def __init__(self):
self.sessions = set() # type: Set[ToyServerSession]
self._block_headers = _BLOCK_HEADERS[:]
@@ -40,6 +45,16 @@ class ToyServer:
self.sh_to_spending_txids = collections.defaultdict(set) # type: dict[str, set[str]]
self.txs = {} # type: dict[str, bytes]
+ async def start(self):
+ session_factory = partial(ToyServerSession, toyserver=self)
+ self.asyncio_server = await aiorpcx.serve_rs(session_factory, "127.0.0.1")
+ server_socket_addr = self.asyncio_server.sockets[0].getsockname()
+ self.server_port = server_socket_addr[1]
+
+ async def stop(self):
+ self.asyncio_server.close()
+ await self.asyncio_server.wait_closed()
+
def get_session_by_name(self, client_name: str) -> 'ToyServerSession':
found_sessions = [
session for session in self.sessions
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.