Skip to content

Commit

Permalink
Merge pull request #122 from OFFIS-DAI/sync-registering
Browse files Browse the repository at this point in the history
Moving creation of inbox to on_start.
  • Loading branch information
rcschrg authored Oct 23, 2024
2 parents 97c31cf + b41e76a commit daa701b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 12 deletions.
10 changes: 7 additions & 3 deletions mango/agent/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,16 +420,20 @@ def on_register(self):
"""

def _do_register(self, container, aid):
self._check_inbox_task = asyncio.create_task(self._check_inbox())
self._check_inbox_task.add_done_callback(self._raise_exceptions)
self._stopped = asyncio.Future()
self._aid = aid
self.context = AgentContext(container)
self.scheduler = Scheduler(
suspendable=True, observable=True, clock=container.clock
)
self.on_register()

def _do_start(self):
self._check_inbox_task = asyncio.create_task(self._check_inbox())
self._check_inbox_task.add_done_callback(self._raise_exceptions)
self._stopped = asyncio.Future()

self.on_start()

def _raise_exceptions(self, fut: asyncio.Future):
"""
Inline function used as a callback to raise exceptions
Expand Down
2 changes: 1 addition & 1 deletion mango/container/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ async def start(self):

"""Start the container. It totally depends on the implementation for what is actually happening."""
for agent in self._agents.values():
agent.on_start()
agent._do_start()

def on_ready(self):
for agent in self._agents.values():
Expand Down
3 changes: 3 additions & 0 deletions mango/container/mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ async def start_agent_loop():
agent_creator(container)
process_initialized_event.set()

for agent in container._agents.values():
agent._do_start()
container.running = True
while not terminate_event.is_set():
await asyncio.sleep(WAIT_STEP)
await container.shutdown()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies = [
"paho-mqtt>=2.1.0",
"python-dateutil>=2.9.0",
"dill>=0.3.8",
"protobuf>=5.27.2",
"protobuf==5.27.2",
"networkx>=3.4.1"
]

Expand Down
19 changes: 18 additions & 1 deletion tests/unit_tests/core/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ async def increase_counter():

# THEN
assert len(l) == 2
await c.shutdown()


@pytest.mark.asyncio
Expand Down Expand Up @@ -102,3 +101,21 @@ async def test_schedule_acl_message():

# THEN
assert agent2.test_counter == 1


def test_sync_setup_agent():
# this test is not async and therefore does not provide a running event loop
c = create_tcp_container(addr=("127.0.0.1", 5555))
# registration without async context should not raise "no running event loop" error
agent = c.register(MyAgent())
agent2 = c.register(MyAgent())

async def run_this(c):
async with activate(c) as c:
await agent.schedule_instant_message(
create_acl("", receiver_addr=agent2.addr, sender_addr=agent.addr),
receiver_addr=agent2.addr,
) # THEN
assert agent2.test_counter == 1

asyncio.run(run_this(c))
8 changes: 4 additions & 4 deletions tests/unit_tests/core/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ async def test_send_message_no_copy():
agent1 = c.register(ExampleAgent())
message_to_send = Data()

await c.send_message(message_to_send, receiver_addr=agent1.addr)
await c.shutdown()
async with activate(c):
await c.send_message(message_to_send, receiver_addr=agent1.addr)

assert agent1.content is message_to_send

Expand All @@ -220,8 +220,8 @@ async def test_send_message_copy():
agent1 = c.register(ExampleAgent())
message_to_send = Data()

await c.send_message(message_to_send, receiver_addr=agent1.addr)
await c.shutdown()
async with activate(c):
await c.send_message(message_to_send, receiver_addr=agent1.addr)

assert agent1.content is not message_to_send

Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/core/test_external_scheduling_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(self):
self.current_ping = 0
self.tasks = []

def on_register(self):
def on_ready(self):
self.tasks.append(self.schedule_periodic_task(self.send_ping, delay=10))

async def send_ping(self):
Expand Down Expand Up @@ -216,9 +216,9 @@ async def test_send_internal_messages():
@pytest.mark.asyncio
async def test_step_with_replying_agent():
external_scheduling_container = create_ec_container(addr="external_eid_1")
reply_agent = external_scheduling_container.register(ReplyAgent())

async with activate(external_scheduling_container) as c:
reply_agent = external_scheduling_container.register(ReplyAgent())
new_acl_msg = ACLMessage()
new_acl_msg.content = "hello you"
new_acl_msg.receiver_addr = "external_eid_1"
Expand Down

0 comments on commit daa701b

Please sign in to comment.