fix: unit tests fixes and ruff formating
N25B-205
This commit is contained in:
@@ -3,60 +3,84 @@ import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock, patch, ANY
|
||||
from control_backend.agents.ri_communication_agent import RICommunicationAgent
|
||||
|
||||
|
||||
def fake_json_correct_negototiate_1():
|
||||
return AsyncMock(return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "main", "port": 5555, "bind": False},
|
||||
{"id": "actuation", "port": 5556, "bind": True},
|
||||
]})
|
||||
return AsyncMock(
|
||||
return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "main", "port": 5555, "bind": False},
|
||||
{"id": "actuation", "port": 5556, "bind": True},
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def fake_json_correct_negototiate_2():
|
||||
return AsyncMock(return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "main", "port": 5555, "bind": False},
|
||||
{"id": "actuation", "port": 5557, "bind": True},
|
||||
]})
|
||||
return AsyncMock(
|
||||
return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "main", "port": 5555, "bind": False},
|
||||
{"id": "actuation", "port": 5557, "bind": True},
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def fake_json_correct_negototiate_3():
|
||||
return AsyncMock(return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "main", "port": 5555, "bind": True},
|
||||
{"id": "actuation", "port": 5557, "bind": True},
|
||||
]})
|
||||
return AsyncMock(
|
||||
return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "main", "port": 5555, "bind": True},
|
||||
{"id": "actuation", "port": 5557, "bind": True},
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def fake_json_correct_negototiate_4():
|
||||
# Different port, do bind
|
||||
return AsyncMock(return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "main", "port": 4555, "bind": True},
|
||||
{"id": "actuation", "port": 5557, "bind": True},
|
||||
]})
|
||||
return AsyncMock(
|
||||
return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "main", "port": 4555, "bind": True},
|
||||
{"id": "actuation", "port": 5557, "bind": True},
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def fake_json_correct_negototiate_5():
|
||||
# Different port, dont bind.
|
||||
return AsyncMock(return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "main", "port": 4555, "bind": False},
|
||||
{"id": "actuation", "port": 5557, "bind": True},
|
||||
]})
|
||||
return AsyncMock(
|
||||
return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "main", "port": 4555, "bind": False},
|
||||
{"id": "actuation", "port": 5557, "bind": True},
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def fake_json_wrong_negototiate_1():
|
||||
return AsyncMock(return_value={
|
||||
"endpoint": "ping",
|
||||
"data": ""})
|
||||
return AsyncMock(return_value={"endpoint": "ping", "data": ""})
|
||||
|
||||
|
||||
def fake_json_invalid_id_negototiate():
|
||||
return AsyncMock(return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "banana", "port": 4555, "bind": False},
|
||||
{"id": "tomato", "port": 5557, "bind": True},
|
||||
]})
|
||||
return AsyncMock(
|
||||
return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "banana", "port": 4555, "bind": False},
|
||||
{"id": "tomato", "port": 5557, "bind": True},
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_1(monkeypatch):
|
||||
@@ -67,20 +91,23 @@ async def test_setup_creates_socket_and_negotiate_1(monkeypatch):
|
||||
fake_socket = MagicMock()
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_correct_negototiate_1()
|
||||
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr("control_backend.agents.ri_communication_agent.context.socket",
|
||||
lambda _: fake_socket)
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
with patch("control_backend.agents.ri_communication_agent.RICommandAgent",
|
||||
autospec=True) as MockCommandAgent:
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
) as MockCommandAgent:
|
||||
fake_agent_instance = MockCommandAgent.return_value
|
||||
fake_agent_instance.start = AsyncMock()
|
||||
|
||||
# --- Act ---
|
||||
agent = RICommunicationAgent("test@server", "password",
|
||||
address="tcp://localhost:5555", bind=False)
|
||||
agent = RICommunicationAgent(
|
||||
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||
)
|
||||
await agent.setup()
|
||||
|
||||
# --- Assert ---
|
||||
@@ -89,14 +116,15 @@ async def test_setup_creates_socket_and_negotiate_1(monkeypatch):
|
||||
fake_socket.recv_json.assert_awaited()
|
||||
fake_agent_instance.start.assert_awaited()
|
||||
MockCommandAgent.assert_called_once_with(
|
||||
ANY, # Server Name
|
||||
ANY, # Server Password
|
||||
ANY, # Server Name
|
||||
ANY, # Server Password
|
||||
address="tcp://*:5556", # derived from the 'port' value in negotiation
|
||||
bind=True
|
||||
bind=True,
|
||||
)
|
||||
# Ensure the agent attached a ListenBehaviour
|
||||
assert any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_2(monkeypatch):
|
||||
"""
|
||||
@@ -106,20 +134,23 @@ async def test_setup_creates_socket_and_negotiate_2(monkeypatch):
|
||||
fake_socket = MagicMock()
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_correct_negototiate_2()
|
||||
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr("control_backend.agents.ri_communication_agent.context.socket",
|
||||
lambda _: fake_socket)
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
with patch("control_backend.agents.ri_communication_agent.RICommandAgent",
|
||||
autospec=True) as MockCommandAgent:
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
) as MockCommandAgent:
|
||||
fake_agent_instance = MockCommandAgent.return_value
|
||||
fake_agent_instance.start = AsyncMock()
|
||||
|
||||
# --- Act ---
|
||||
agent = RICommunicationAgent("test@server", "password",
|
||||
address="tcp://localhost:5555", bind=False)
|
||||
agent = RICommunicationAgent(
|
||||
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||
)
|
||||
await agent.setup()
|
||||
|
||||
# --- Assert ---
|
||||
@@ -128,14 +159,15 @@ async def test_setup_creates_socket_and_negotiate_2(monkeypatch):
|
||||
fake_socket.recv_json.assert_awaited()
|
||||
fake_agent_instance.start.assert_awaited()
|
||||
MockCommandAgent.assert_called_once_with(
|
||||
ANY, # Server Name
|
||||
ANY, # Server Password
|
||||
ANY, # Server Name
|
||||
ANY, # Server Password
|
||||
address="tcp://*:5557", # derived from the 'port' value in negotiation
|
||||
bind=True
|
||||
bind=True,
|
||||
)
|
||||
# Ensure the agent attached a ListenBehaviour
|
||||
assert any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_3(monkeypatch, caplog):
|
||||
"""
|
||||
@@ -145,25 +177,27 @@ async def test_setup_creates_socket_and_negotiate_3(monkeypatch, caplog):
|
||||
fake_socket = MagicMock()
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_wrong_negototiate_1()
|
||||
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr("control_backend.agents.ri_communication_agent.context.socket",
|
||||
lambda _: fake_socket)
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
|
||||
|
||||
# We are sending wrong negotiation info to the communication agent, so we should retry and expect a
|
||||
# better response, within a limited time.
|
||||
with patch("control_backend.agents.ri_communication_agent.RICommandAgent",
|
||||
autospec=True) as MockCommandAgent:
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
) as MockCommandAgent:
|
||||
fake_agent_instance = MockCommandAgent.return_value
|
||||
fake_agent_instance.start = AsyncMock()
|
||||
|
||||
# --- Act ---
|
||||
with caplog.at_level("ERROR"):
|
||||
agent = RICommunicationAgent("test@server", "password",
|
||||
address="tcp://localhost:5555", bind=False)
|
||||
agent = RICommunicationAgent(
|
||||
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||
)
|
||||
await agent.setup(max_retries=1)
|
||||
|
||||
# --- Assert ---
|
||||
@@ -173,10 +207,11 @@ async def test_setup_creates_socket_and_negotiate_3(monkeypatch, caplog):
|
||||
# Since it failed, there should not be any command agent.
|
||||
fake_agent_instance.start.assert_not_awaited()
|
||||
assert "Failed to set up RICommunicationAgent" in caplog.text
|
||||
|
||||
|
||||
# Ensure the agent did not attach a ListenBehaviour
|
||||
assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_4(monkeypatch):
|
||||
"""
|
||||
@@ -186,20 +221,23 @@ async def test_setup_creates_socket_and_negotiate_4(monkeypatch):
|
||||
fake_socket = MagicMock()
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_correct_negototiate_3()
|
||||
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr("control_backend.agents.ri_communication_agent.context.socket",
|
||||
lambda _: fake_socket)
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
with patch("control_backend.agents.ri_communication_agent.RICommandAgent",
|
||||
autospec=True) as MockCommandAgent:
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
) as MockCommandAgent:
|
||||
fake_agent_instance = MockCommandAgent.return_value
|
||||
fake_agent_instance.start = AsyncMock()
|
||||
|
||||
# --- Act ---
|
||||
agent = RICommunicationAgent("test@server", "password",
|
||||
address="tcp://localhost:5555", bind=True)
|
||||
agent = RICommunicationAgent(
|
||||
"test@server", "password", address="tcp://localhost:5555", bind=True
|
||||
)
|
||||
await agent.setup()
|
||||
|
||||
# --- Assert ---
|
||||
@@ -208,14 +246,15 @@ async def test_setup_creates_socket_and_negotiate_4(monkeypatch):
|
||||
fake_socket.recv_json.assert_awaited()
|
||||
fake_agent_instance.start.assert_awaited()
|
||||
MockCommandAgent.assert_called_once_with(
|
||||
ANY, # Server Name
|
||||
ANY, # Server Password
|
||||
ANY, # Server Name
|
||||
ANY, # Server Password
|
||||
address="tcp://*:5557", # derived from the 'port' value in negotiation
|
||||
bind=True
|
||||
bind=True,
|
||||
)
|
||||
# Ensure the agent attached a ListenBehaviour
|
||||
assert any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_5(monkeypatch):
|
||||
"""
|
||||
@@ -225,20 +264,23 @@ async def test_setup_creates_socket_and_negotiate_5(monkeypatch):
|
||||
fake_socket = MagicMock()
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_correct_negototiate_4()
|
||||
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr("control_backend.agents.ri_communication_agent.context.socket",
|
||||
lambda _: fake_socket)
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
with patch("control_backend.agents.ri_communication_agent.RICommandAgent",
|
||||
autospec=True) as MockCommandAgent:
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
) as MockCommandAgent:
|
||||
fake_agent_instance = MockCommandAgent.return_value
|
||||
fake_agent_instance.start = AsyncMock()
|
||||
|
||||
# --- Act ---
|
||||
agent = RICommunicationAgent("test@server", "password",
|
||||
address="tcp://localhost:5555", bind=False)
|
||||
agent = RICommunicationAgent(
|
||||
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||
)
|
||||
await agent.setup()
|
||||
|
||||
# --- Assert ---
|
||||
@@ -247,14 +289,15 @@ async def test_setup_creates_socket_and_negotiate_5(monkeypatch):
|
||||
fake_socket.recv_json.assert_awaited()
|
||||
fake_agent_instance.start.assert_awaited()
|
||||
MockCommandAgent.assert_called_once_with(
|
||||
ANY, # Server Name
|
||||
ANY, # Server Password
|
||||
ANY, # Server Name
|
||||
ANY, # Server Password
|
||||
address="tcp://*:5557", # derived from the 'port' value in negotiation
|
||||
bind=True
|
||||
bind=True,
|
||||
)
|
||||
# Ensure the agent attached a ListenBehaviour
|
||||
assert any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_6(monkeypatch):
|
||||
"""
|
||||
@@ -264,20 +307,23 @@ async def test_setup_creates_socket_and_negotiate_6(monkeypatch):
|
||||
fake_socket = MagicMock()
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_correct_negototiate_5()
|
||||
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr("control_backend.agents.ri_communication_agent.context.socket",
|
||||
lambda _: fake_socket)
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
with patch("control_backend.agents.ri_communication_agent.RICommandAgent",
|
||||
autospec=True) as MockCommandAgent:
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
) as MockCommandAgent:
|
||||
fake_agent_instance = MockCommandAgent.return_value
|
||||
fake_agent_instance.start = AsyncMock()
|
||||
|
||||
# --- Act ---
|
||||
agent = RICommunicationAgent("test@server", "password",
|
||||
address="tcp://localhost:5555", bind=False)
|
||||
agent = RICommunicationAgent(
|
||||
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||
)
|
||||
await agent.setup()
|
||||
|
||||
# --- Assert ---
|
||||
@@ -286,14 +332,15 @@ async def test_setup_creates_socket_and_negotiate_6(monkeypatch):
|
||||
fake_socket.recv_json.assert_awaited()
|
||||
fake_agent_instance.start.assert_awaited()
|
||||
MockCommandAgent.assert_called_once_with(
|
||||
ANY, # Server Name
|
||||
ANY, # Server Password
|
||||
ANY, # Server Name
|
||||
ANY, # Server Password
|
||||
address="tcp://*:5557", # derived from the 'port' value in negotiation
|
||||
bind=True
|
||||
bind=True,
|
||||
)
|
||||
# Ensure the agent attached a ListenBehaviour
|
||||
assert any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_7(monkeypatch, caplog):
|
||||
"""
|
||||
@@ -303,25 +350,27 @@ async def test_setup_creates_socket_and_negotiate_7(monkeypatch, caplog):
|
||||
fake_socket = MagicMock()
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_invalid_id_negototiate()
|
||||
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr("control_backend.agents.ri_communication_agent.context.socket",
|
||||
lambda _: fake_socket)
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
|
||||
|
||||
# We are sending wrong negotiation info to the communication agent, so we should retry and expect a
|
||||
# better response, within a limited time.
|
||||
with patch("control_backend.agents.ri_communication_agent.RICommandAgent",
|
||||
autospec=True) as MockCommandAgent:
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
) as MockCommandAgent:
|
||||
fake_agent_instance = MockCommandAgent.return_value
|
||||
fake_agent_instance.start = AsyncMock()
|
||||
|
||||
# --- Act ---
|
||||
with caplog.at_level("WARNING"):
|
||||
agent = RICommunicationAgent("test@server", "password",
|
||||
address="tcp://localhost:5555", bind=False)
|
||||
agent = RICommunicationAgent(
|
||||
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||
)
|
||||
await agent.setup(max_retries=1)
|
||||
|
||||
# --- Assert ---
|
||||
@@ -342,32 +391,36 @@ async def test_setup_creates_socket_and_negotiate_timeout(monkeypatch, caplog):
|
||||
fake_socket = MagicMock()
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = AsyncMock(side_effect=asyncio.TimeoutError)
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr("control_backend.agents.ri_communication_agent.context.socket",
|
||||
lambda _: fake_socket)
|
||||
|
||||
with patch("control_backend.agents.ri_communication_agent.RICommandAgent",
|
||||
autospec=True) as MockCommandAgent:
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
) as MockCommandAgent:
|
||||
fake_agent_instance = MockCommandAgent.return_value
|
||||
fake_agent_instance.start = AsyncMock()
|
||||
|
||||
# --- Act ---
|
||||
with caplog.at_level("WARNING"):
|
||||
agent = RICommunicationAgent("test@server", "password",
|
||||
address="tcp://localhost:5555", bind=False)
|
||||
agent = RICommunicationAgent(
|
||||
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||
)
|
||||
await agent.setup(max_retries=1)
|
||||
|
||||
# --- Assert ---
|
||||
fake_socket.connect.assert_any_call("tcp://localhost:5555")
|
||||
|
||||
|
||||
# Since it failed, there should not be any command agent.
|
||||
fake_agent_instance.start.assert_not_awaited()
|
||||
assert "No connection established in 20 seconds" in caplog.text
|
||||
|
||||
|
||||
# Ensure the agent did not attach a ListenBehaviour
|
||||
assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_listen_behaviour_ping_correct(caplog):
|
||||
fake_socket = AsyncMock()
|
||||
@@ -389,6 +442,7 @@ async def test_listen_behaviour_ping_correct(caplog):
|
||||
fake_socket.recv_json.assert_awaited()
|
||||
assert "Received message" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_listen_behaviour_ping_wrong_endpoint(caplog):
|
||||
"""
|
||||
@@ -398,12 +452,15 @@ async def test_listen_behaviour_ping_wrong_endpoint(caplog):
|
||||
fake_socket.send_json = AsyncMock()
|
||||
|
||||
# This is a message for the wrong endpoint >:(
|
||||
fake_socket.recv_json = AsyncMock(return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "main", "port": 5555, "bind": False},
|
||||
{"id": "actuation", "port": 5556, "bind": True},
|
||||
]})
|
||||
fake_socket.recv_json = AsyncMock(
|
||||
return_value={
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [
|
||||
{"id": "main", "port": 5555, "bind": False},
|
||||
{"id": "actuation", "port": 5556, "bind": True},
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
agent = RICommunicationAgent("test@server", "password")
|
||||
agent.req_socket = fake_socket
|
||||
@@ -415,11 +472,11 @@ async def test_listen_behaviour_ping_wrong_endpoint(caplog):
|
||||
with caplog.at_level("INFO"):
|
||||
await behaviour.run()
|
||||
|
||||
|
||||
assert "Received message with topic different than ping, while ping expected." in caplog.text
|
||||
fake_socket.send_json.assert_awaited()
|
||||
fake_socket.recv_json.assert_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_listen_behaviour_timeout(caplog):
|
||||
fake_socket = AsyncMock()
|
||||
@@ -438,6 +495,7 @@ async def test_listen_behaviour_timeout(caplog):
|
||||
|
||||
assert "No ping retrieved in 3 seconds" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_listen_behaviour_ping_no_endpoint(caplog):
|
||||
"""
|
||||
@@ -447,9 +505,11 @@ async def test_listen_behaviour_ping_no_endpoint(caplog):
|
||||
fake_socket.send_json = AsyncMock()
|
||||
|
||||
# This is a message without endpoint >:(
|
||||
fake_socket.recv_json = AsyncMock(return_value={
|
||||
"data": "I dont have an endpoint >:)",
|
||||
})
|
||||
fake_socket.recv_json = AsyncMock(
|
||||
return_value={
|
||||
"data": "I dont have an endpoint >:)",
|
||||
}
|
||||
)
|
||||
|
||||
agent = RICommunicationAgent("test@server", "password")
|
||||
agent.req_socket = fake_socket
|
||||
@@ -465,6 +525,7 @@ async def test_listen_behaviour_ping_no_endpoint(caplog):
|
||||
fake_socket.send_json.assert_awaited()
|
||||
fake_socket.recv_json.assert_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_unexpected_exception(monkeypatch, caplog):
|
||||
fake_socket = MagicMock()
|
||||
@@ -473,12 +534,12 @@ async def test_setup_unexpected_exception(monkeypatch, caplog):
|
||||
fake_socket.recv_json = AsyncMock(side_effect=Exception("boom!"))
|
||||
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket",
|
||||
lambda _: fake_socket
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
agent = RICommunicationAgent("test@server", "password",
|
||||
address="tcp://localhost:5555", bind=False)
|
||||
agent = RICommunicationAgent(
|
||||
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||
)
|
||||
|
||||
with caplog.at_level("ERROR"):
|
||||
await agent.setup(max_retries=1)
|
||||
@@ -486,6 +547,7 @@ async def test_setup_unexpected_exception(monkeypatch, caplog):
|
||||
# Ensure that the error was logged
|
||||
assert "Unexpected error during negotiation: boom!" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_unpacking_exception(monkeypatch, caplog):
|
||||
# --- Arrange ---
|
||||
@@ -493,24 +555,27 @@ async def test_setup_unpacking_exception(monkeypatch, caplog):
|
||||
fake_socket.send_json = AsyncMock()
|
||||
|
||||
# Make recv_json return malformed negotiation data to trigger unpacking exception
|
||||
malformed_data = {"endpoint": "negotiate/ports",
|
||||
"data": [ {"id": "main"} ]} # missing 'port' and 'bind'
|
||||
malformed_data = {
|
||||
"endpoint": "negotiate/ports",
|
||||
"data": [{"id": "main"}],
|
||||
} # missing 'port' and 'bind'
|
||||
fake_socket.recv_json = AsyncMock(return_value=malformed_data)
|
||||
|
||||
# Patch context.socket
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket",
|
||||
lambda _: fake_socket
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Patch RICommandAgent so it won't actually start
|
||||
with patch("control_backend.agents.ri_communication_agent.RICommandAgent",
|
||||
autospec=True) as MockCommandAgent:
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
) as MockCommandAgent:
|
||||
fake_agent_instance = MockCommandAgent.return_value
|
||||
fake_agent_instance.start = AsyncMock()
|
||||
|
||||
agent = RICommunicationAgent("test@server", "password",
|
||||
address="tcp://localhost:5555", bind=False)
|
||||
agent = RICommunicationAgent(
|
||||
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||
)
|
||||
|
||||
# --- Act & Assert ---
|
||||
with caplog.at_level("ERROR"):
|
||||
@@ -523,4 +588,4 @@ async def test_setup_unpacking_exception(monkeypatch, caplog):
|
||||
fake_agent_instance.start.assert_not_awaited()
|
||||
|
||||
# Ensure no behaviour was attached
|
||||
assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||
assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||
|
||||
Reference in New Issue
Block a user