From ff2e24a5b76dc5a16c774e903cfa06cdefa854ad Mon Sep 17 00:00:00 2001 From: Vinicius Arcanjo Date: Thu, 7 Nov 2024 13:33:46 -0300 Subject: [PATCH 1/4] fix: avoid blocking setup() when loading topology --- main.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index c576330..ba4dffe 100644 --- a/main.py +++ b/main.py @@ -88,6 +88,9 @@ def load_sdx_topology(self): "version": 1, "timestamp": get_timestamp(), } + + def load_kytos_topology(self): + """Load topology from Kytos-ng.""" with self._topo_lock: self._topo_dict = self.get_kytos_topology() self._converted_topo = self.convert_topology_v2() @@ -111,10 +114,15 @@ def get_kytos_topology(): ) return topology - @listen_to( - "kytos/topology.updated", - "kytos/topology.topology_loaded", - ) + @listen_to("kytos/topology.topology_loaded") + def on_topology_loaded(self, event: KytosEvent): + """Handler for on topology_loaded.""" + self.handler_on_topology_loaded() + + def handler_on_topology_loaded(self): + self.load_kytos_topology() + + @listen_to("kytos/topology.updated") def on_topology_updated_event(self, event: KytosEvent): """Handler for topology updated events.""" self.handler_on_topology_updated_event(event) From e8ad4cbc9b9ea480bac97a5f21d52d4b22bb603a Mon Sep 17 00:00:00 2001 From: Vinicius Arcanjo Date: Thu, 7 Nov 2024 13:39:57 -0300 Subject: [PATCH 2/4] tests: covered with unit tests accordingly --- main.py | 3 ++- tests/unit/test_main.py | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index ba4dffe..3fa0fa7 100644 --- a/main.py +++ b/main.py @@ -115,11 +115,12 @@ def get_kytos_topology(): return topology @listen_to("kytos/topology.topology_loaded") - def on_topology_loaded(self, event: KytosEvent): + def on_topology_loaded(self, _event: KytosEvent): """Handler for on topology_loaded.""" self.handler_on_topology_loaded() def handler_on_topology_loaded(self): + """Hnalder on_topology_loaded.""" self.load_kytos_topology() @listen_to("kytos/topology.updated") diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 844a8d5..1b928a0 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -115,6 +115,15 @@ async def test_create_l2vpn(self, requests_mock): assert response.status_code == 201 assert response.json() == {"service_id": "a123"} + def test_handler_on_topology_loaded(self): + """Test handler_on_topology_loaded.""" + self.napp.get_kytos_topology = MagicMock() + some_topo = {"switches": {"dpid1": {}}, "links": {"link1": {}}} + self.napp.get_kytos_topology.return_value = some_topo + self.napp.convert_topology_v2 = MagicMock() + self.napp.handler_on_topology_loaded() + assert self.napp._topo_dict == some_topo + @patch("requests.post") @patch("requests.patch") async def test_update_l2vpn(self, req_patch_mock, req_post_mock): From b6a0c3c5050a96afe08d72e917dc149c6113b45b Mon Sep 17 00:00:00 2001 From: Vinicius Arcanjo Date: Thu, 7 Nov 2024 13:41:23 -0300 Subject: [PATCH 3/4] docs: updated CHANGELOG.rst --- CHANGELOG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 97cc8e4..26b08f3 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -20,4 +20,4 @@ Changed Fixed ===== -- +- Avoid blocking setup() when loading topology From 6627a473be77c7245e2b1b2332e9485abd187b76 Mon Sep 17 00:00:00 2001 From: Vinicius Arcanjo Date: Thu, 7 Nov 2024 13:45:05 -0300 Subject: [PATCH 4/4] chore: fix typo --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 3fa0fa7..fd7b29a 100644 --- a/main.py +++ b/main.py @@ -120,7 +120,7 @@ def on_topology_loaded(self, _event: KytosEvent): self.handler_on_topology_loaded() def handler_on_topology_loaded(self): - """Hnalder on_topology_loaded.""" + """Handler on_topology_loaded.""" self.load_kytos_topology() @listen_to("kytos/topology.updated")