From b319b73d8ea8a8abf2120352250351a2462a0204 Mon Sep 17 00:00:00 2001 From: Kuba Mazurek Date: Mon, 27 Sep 2021 13:53:31 +0200 Subject: [PATCH] Split Network lifecycle into separate state transitions --- tests/test_network.py | 4 ++-- yapapi/network.py | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/tests/test_network.py b/tests/test_network.py index 3ae636788..df39c5092 100644 --- a/tests/test_network.py +++ b/tests/test_network.py @@ -132,7 +132,7 @@ async def test_remove(self): @pytest.mark.asyncio async def test_remove_when_initialized(self): network = Network(mock.Mock(), f"192.168.0.0/24", "0xdeadbeef") - with pytest.raises(TransitionNotAllowed, match=".*Can't remove when in initialized.*") as e: + with pytest.raises(TransitionNotAllowed, match=".*Can't stop when in initialized.*") as e: await network.remove() @pytest.mark.asyncio @@ -141,7 +141,7 @@ async def test_remove_when_removed(self): await network.remove() - with pytest.raises(TransitionNotAllowed, match=".*Can't remove when in removed.*") as e: + with pytest.raises(TransitionNotAllowed, match=".*Can't stop when in removed.*") as e: await network.remove() @pytest.mark.asyncio diff --git a/yapapi/network.py b/yapapi/network.py index 0543a0036..3e8be8c2a 100644 --- a/yapapi/network.py +++ b/yapapi/network.py @@ -65,15 +65,17 @@ class NetworkState(StateMachine): removing = State("removing") removed = State("removed") - # transitions - add_address = creating.to.itself() | ready.to.itself() + # state-altering transitions (lifecycle) + create = initialized.to(creating) + start = creating.to(ready) + stop = ready.to(removing) + remove = removing.to(removed) + + # same-state transitions + add_owner_address = creating.to.itself() | ready.to.itself() add_node = ready.to.itself() get_id = creating.to.itself() | ready.to.itself() | removing.to.itself() - # lifecycle - create = initialized.to(creating) | creating.to(ready) - remove = ready.to(removing) | removing.to(removed) - class Network: """ @@ -111,7 +113,7 @@ async def create( # add requestor's own address to the network await network.add_owner_address(network.owner_ip) - network._state_machine.create() + network._state_machine.start() return network def __init__( @@ -223,7 +225,7 @@ async def add_owner_address(self, ip: str): :param ip: the IP address to assign to the requestor node. """ - self._state_machine.add_address() + self._state_machine.add_owner_address() self._ensure_ip_in_network(ip) async with self._nodes_lock: @@ -259,7 +261,7 @@ async def add_node(self, node_id: str, ip: Optional[str] = None) -> Node: async def remove(self) -> None: """Remove this network, terminating any connections it provides.""" - self._state_machine.remove() + self._state_machine.stop() await self._net_api.remove_network(self.network_id) self._state_machine.remove()