Skip to content

Commit

Permalink
Added container tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
javipalanca committed Oct 1, 2018
1 parent 6eef9ae commit aef36ce
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 7 deletions.
2 changes: 1 addition & 1 deletion spade/behaviour.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ async def send(self, msg):
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
Expand Down
13 changes: 11 additions & 2 deletions spade/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions tests/test_behaviour.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ async def run(self):
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()
Expand Down Expand Up @@ -331,7 +331,7 @@ async def run(self):
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()
Expand Down
106 changes: 106 additions & 0 deletions tests/test_container.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 4 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.