From bae67401d98d6366ddb99a7ebab199dc1aa81299 Mon Sep 17 00:00:00 2001 From: Rico Schrage Date: Tue, 30 Jan 2024 14:39:32 +0100 Subject: [PATCH 1/2] Adding remove_role to context and role_handler. Make it possible to add and remove roles from the role_context. --- mango/agent/role.py | 27 +++++++++++++++---- tests/unit_tests/role_agent_test.py | 40 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/mango/agent/role.py b/mango/agent/role.py index 844460fb..8c48325e 100644 --- a/mango/agent/role.py +++ b/mango/agent/role.py @@ -165,6 +165,15 @@ def add_role(self, role: Role) -> None: self._roles.append(role) self._role_to_active[role] = True + def remove_role(self, role: Role) -> None: + """Remove a given role + + Args: + role ([type]): the role + """ + self._roles.remove(role) + del self._role_to_active[role] + @property def roles(self) -> List[Role]: """Returns all roles @@ -340,8 +349,21 @@ def add_role(self, role: Role): :param role: the Role """ + role.bind(self) self._role_handler.add_role(role) + # Setup role + role.setup() + + def remove_role(self, role: Role): + """Remove a role and call on_stop for clean up + + :param role: the role to remove + :type role: Role + """ + self._role_handler.remove_role(role) + asyncio.create_task(role.on_stop()) + def handle_message(self, content, meta: Dict[str, Any]): """Handle an incoming message, delegating it to all applicable subscribers for role, message_condition, method, _ in self._message_subs: @@ -435,12 +457,8 @@ def add_role(self, role: Role): :param role: the role to add """ - role.bind(self._role_context) self._role_context.add_role(role) - # Setup role - role.setup() - def remove_role(self, role: Role): """Remove a role permanently from the agent. @@ -448,7 +466,6 @@ def remove_role(self, role: Role): :type role: Role """ self._role_context.remove_role(role) - asyncio.create_task(role.on_stop()) @property def roles(self) -> List[Role]: diff --git a/tests/unit_tests/role_agent_test.py b/tests/unit_tests/role_agent_test.py index 3e7f5143..c1f75197 100644 --- a/tests/unit_tests/role_agent_test.py +++ b/tests/unit_tests/role_agent_test.py @@ -127,6 +127,15 @@ def setup(self): self.context.deactivate(r) +class TestRole(Role): + def __init__(self): + self.setup_called = False + + def setup(self): + assert self.context is not None + self.setup_called = True + + @pytest.mark.asyncio @pytest.mark.parametrize( "num_agents,num_containers", [(1, 1), (2, 1), (2, 2), (10, 2), (10, 10)] @@ -213,3 +222,34 @@ async def test_send_ping_pong_deactivated_pong(num_agents, num_containers): await c.shutdown() assert len(asyncio.all_tasks()) == 1 + + +@pytest.mark.asyncio +async def test_role_add_remove(): + c = await container_factory.create(addr=("127.0.0.2", 5555)) + agent = RoleAgent(c) + role = TestRole() + agent.add_role(role) + + assert agent._role_handler.roles[0] == role + + agent.remove_role(role) + + assert len(agent._role_handler.roles) == 0 + await c.shutdown() + + +@pytest.mark.asyncio +async def test_role_add_remove_context(): + c = await container_factory.create(addr=("127.0.0.2", 5555)) + agent = RoleAgent(c) + role = TestRole() + agent._role_context.add_role(role) + + assert role.setup_called + assert agent._role_handler.roles[0] == role + + agent._role_context.remove_role(role) + + assert len(agent._role_handler.roles) == 0 + await c.shutdown() From 27dce4a7fdf5f74ca4aa9735b7ca50af56057023 Mon Sep 17 00:00:00 2001 From: Rico Schrage Date: Tue, 30 Jan 2024 14:43:13 +0100 Subject: [PATCH 2/2] Adding remove_role to context and role_handler. Make it possible to add and remove roles from the role_context. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 2b4caf06..08eec260 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ EMAIL = "mango@offis.de" AUTHOR = "mango Team" REQUIRES_PYTHON = ">=3.7.0" -VERSION = "1.1.2" +VERSION = "1.1.3" # What packages are required for this module to be executed? REQUIRED = [