Permalink
Browse files

Added container tests.

  • Loading branch information...
javipalanca committed Oct 1, 2018
1 parent 6eef9ae commit aef36ce7d561a3d57a34c106eeb01e7f4ea50687
Showing with 124 additions and 7 deletions.
  1. +1 −1 spade/behaviour.py
  2. +11 −2 spade/container.py
  3. +2 −2 tests/test_behaviour.py
  4. +106 −0 tests/test_container.py
  5. +4 −2 tests/utils.py
View
@@ -258,7 +258,7 @@ def mailbox_size(self):
msg.sender = str(self.agent.jid)
logger.debug(f"Adding agent's jid as sender to message: {msg}")
if self.agent.container:
self.agent.container.send(msg, self)
await self.agent.container.send(msg, self)
else:
await self._xmpp_send(msg)
msg.sent = True
View
@@ -6,13 +6,22 @@ class Container(object):
def __init__(self):
self.__agents = {}
def reset(self):
self.__agents = {}
def register(self, agent):
self.__agents[str(agent.jid)] = agent
agent.set_container(self)
def send(self, msg, behaviour):
def has_agent(self, jid):
return jid in self.__agents
def get_agent(self, jid):
return self.__agents[jid]
async def send(self, msg, behaviour):
to = str(msg.to)
if to in self.__agents:
self.__agents[to].dispatch(msg)
else:
behaviour._xmpp_send(msg)
await behaviour._xmpp_send(msg)
View
@@ -298,7 +298,7 @@ class SendBehaviour(OneShotBehaviour):
await self.send(message)
self.kill()
agent = make_connected_agent()
agent = make_connected_agent(use_container=False)
agent.start(auto_register=False)
agent.aiothread.client = MagicMock()
@@ -331,7 +331,7 @@ class SendBehaviour(OneShotBehaviour):
await self.send(msg)
self.kill()
agent = make_connected_agent()
agent = make_connected_agent(use_container=False)
agent.start(auto_register=False)
agent.aiothread.client = MagicMock()
View
@@ -0,0 +1,106 @@
import aioxmpp
import pytest
from asynctest import MagicMock, CoroutineMock
from spade.behaviour import OneShotBehaviour
from spade.container import Container
from spade.message import Message
from tests.test_behaviour import wait_for_behaviour_is_killed
from tests.utils import make_connected_agent
def test_use_container():
container = Container()
container.reset()
agent = make_connected_agent(use_container=True)
assert agent.container == Container()
assert container.has_agent(str(agent.jid))
assert container.get_agent(str(agent.jid)) == agent
agent.stop()
def test_use_container_false():
container = Container()
container.reset()
agent = make_connected_agent(use_container=False)
assert agent.container is None
assert not container.has_agent(str(agent.jid))
with pytest.raises(KeyError):
container.get_agent(str(agent.jid))
agent.stop()
def test_send_message_with_container():
class FakeReceiverAgent:
def __init__(self):
self.jid = "fake_receiver_agent@server"
def set_container(self, c): pass
class SendBehaviour(OneShotBehaviour):
async def run(self):
message = Message(to="fake_receiver_agent@server")
await self.send(message)
self.kill()
container = Container()
container.reset()
fake_receiver_agent = FakeReceiverAgent()
container.register(fake_receiver_agent)
fake_receiver_agent.dispatch = MagicMock()
agent = make_connected_agent(use_container=True)
agent.start(auto_register=False)
agent.aiothread.client = MagicMock()
agent.client.send = CoroutineMock()
behaviour = SendBehaviour()
agent.add_behaviour(behaviour)
wait_for_behaviour_is_killed(behaviour)
assert agent.client.send.await_count == 0
assert fake_receiver_agent.dispatch.call_count == 1
assert str(fake_receiver_agent.dispatch.call_args[0][0].to) == "fake_receiver_agent@server"
agent.stop()
def test_send_message_to_outer_with_container():
class SendBehaviour(OneShotBehaviour):
async def run(self):
message = Message(to="to@outerhost")
await self.send(message)
self.kill()
container = Container()
container.reset()
agent = make_connected_agent(use_container=True)
agent.start(auto_register=False)
behaviour = SendBehaviour()
behaviour._xmpp_send = CoroutineMock()
agent.add_behaviour(behaviour)
wait_for_behaviour_is_killed(behaviour)
assert container.has_agent(str(agent.jid))
assert not container.has_agent("to@outerhost")
assert behaviour._xmpp_send.await_count == 1
msg_arg = behaviour._xmpp_send.await_args[0][0]
assert msg_arg.to == aioxmpp.JID.fromstr("to@outerhost")
agent.stop()
View
@@ -15,8 +15,8 @@ def __init__(self, *args, **kwargs):
self.aiothread.stream = Mock()
def make_connected_agent(jid="fake@jid", password="fake_password", loop=None):
return MockedConnectedAgent(jid, password, loop=loop)
def make_connected_agent(jid="fake@jid", password="fake_password", use_container=True, loop=None):
return MockedConnectedAgent(jid, password, use_container=use_container, loop=loop)
class MockedPresenceConnectedAgent(Agent):
@@ -43,11 +43,13 @@ def make_presence_connected_agent(jid="fake@jid", password="fake_password",
show=None,
status=None,
priority=0,
use_container=True,
loop=None):
status = {} if status is None else status
return MockedPresenceConnectedAgent(jid=jid, password=password,
available=available,
show=show,
status=status,
priority=priority,
use_container=use_container,
loop=loop)

0 comments on commit aef36ce

Please sign in to comment.