fix: await Dynamips.create_nio (it is async, unlike sync base)

Dynamips.create_nio is async def while BaseManager.create_nio is a sync
def.  The previous fix only added the extra 'node' argument but did not
await the resulting coroutine, causing 'was never awaited' warnings and
passing a coroutine object instead of an NIO instance to the binding
dispatch.  Add 'await' on the Dynamips branch.  Test updated to verify
both the async nature and the parameter count.
This commit is contained in:
YueGuobin 2026-08-11 01:00:01 +08:00
parent 09a9555d02
commit 4e30b6905a
No known key found for this signature in database
2 changed files with 19 additions and 15 deletions

View File

@ -189,15 +189,12 @@ async def create_batch_nios(
for entry in batch.nios: for entry in batch.nios:
node = project.get_node(entry.node_id) node = project.get_node(entry.node_id)
nio_settings = jsonable_encoder(entry.nio, exclude_unset=True) nio_settings = jsonable_encoder(entry.nio, exclude_unset=True)
# Dynamips.create_nio takes an extra positional `node` argument that # Dynamips.create_nio(self, node, nio_settings) is async and takes an
# the base signature does not include. Detect it via parameter count. # extra positional 'node'. Detect via bound-method parameter count:
# Dynamips.create_nio(self, node, nio_settings) exposes one extra # standard == 1, Dynamips == 2. Await the async variant.
# positional on the bound method compared to the standard signature
# (self, nio_settings). Detect it: standard == 1 bound param,
# Dynamips == 2 bound params (node + nio_settings).
sig = inspect.signature(node.manager.create_nio) sig = inspect.signature(node.manager.create_nio)
if len(sig.parameters) >= 2: if len(sig.parameters) >= 2:
nio = node.manager.create_nio(node, nio_settings) nio = await node.manager.create_nio(node, nio_settings)
else: else:
nio = node.manager.create_nio(nio_settings) nio = node.manager.create_nio(nio_settings)
await _add_nio_binding(node, entry.adapter_number, entry.port_number, nio) await _add_nio_binding(node, entry.adapter_number, entry.port_number, nio)

View File

@ -258,27 +258,34 @@ class TestBatchNIOEdgeCases:
node.add_nio.assert_called_once_with(nio, 0) node.add_nio.assert_called_once_with(nio, 0)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_dynamips_create_nio_passes_node_arg(self): async def test_dynamips_create_nio_is_async_and_needs_await(self):
""" """
Dynamips.create_nio(self, node, nio_settings) exposes 2 bound-method Dynamips.create_nio is async (returns a coroutine) unlike the sync
params vs. the standard 1. The batch handler detects this via the base version. The batch handler must await it.
parameter count on the bound method and passes the extra 'node'
argument.
""" """
import inspect import inspect
import asyncio as _asyncio
class _FakeDynamips: class _FakeDynamips:
async def create_nio(self, node, nio_settings): async def create_nio(self, node, nio_settings):
pass return {"type": "nio_udp", "node": node}
class _FakeBase: class _FakeBase:
async def create_nio(self, nio_settings): def create_nio(self, nio_settings):
pass return {"type": "nio_udp"}
dyn = _FakeDynamips() dyn = _FakeDynamips()
base = _FakeBase() base = _FakeBase()
assert len(inspect.signature(dyn.create_nio).parameters) == 2 # Dynamips assert len(inspect.signature(dyn.create_nio).parameters) == 2 # Dynamips
assert len(inspect.signature(base.create_nio).parameters) == 1 # standard assert len(inspect.signature(base.create_nio).parameters) == 1 # standard
assert inspect.iscoroutinefunction(dyn.create_nio)
assert not inspect.iscoroutinefunction(base.create_nio)
# Verify the batch logic: 2 params → await, 1 param → no await
d_result = await dyn.create_nio("r1", {"type": "nio_udp"})
b_result = base.create_nio({"type": "nio_udp"})
assert d_result["node"] == "r1"
assert b_result["type"] == "nio_udp"
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_qemu_dispatch_to_adapter_add_nio_binding(self): async def test_qemu_dispatch_to_adapter_add_nio_binding(self):