Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core/starter #501

Merged
merged 4 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions kytos/core/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def toggle_debug(self, name=None):
# enable debug
logger.setLevel(logging.DEBUG)

def start(self, restart=False):
async def start(self, restart=False):
"""Create pidfile and call start_controller method."""
self.enable_logs()
# pylint: disable=broad-except
Expand All @@ -275,7 +275,7 @@ def start(self, restart=False):
self.apm = init_apm(self.options.apm, app=self.api_server.app)
if not restart:
self.create_pidfile()
self.start_controller()
await self.start_controller()
except (KytosDBInitException, KytosAPMInitException) as exc:
message = f"Kytos couldn't start because of {str(exc)}"
sys.exit(message)
Expand Down Expand Up @@ -361,7 +361,7 @@ def buffers(self) -> KytosBuffers:
self._buffers = KytosBuffers()
return self._buffers

def start_controller(self):
async def start_controller(self):
"""Start the controller.

Starts the KytosServer (TCP Server) coroutine.
Expand All @@ -382,6 +382,8 @@ def start_controller(self):

task = self.loop.create_task(self.api_server.serve())
self._tasks.append(task)
while not self.api_server.server.started:
await asyncio.sleep(0.1)

# ASYNC TODO: ensure all threads started correctly
# This is critical, if any of them failed starting we should exit.
Expand Down
2 changes: 1 addition & 1 deletion kytos/core/kytosd.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def async_main(config):
if controller.options.debug:
loop.set_debug(True)

loop.call_soon(controller.start)
loop.run_until_complete(controller.start())

shell_task = None
if controller.options.foreground:
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/test_core/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ def test_debug_wrong_name(self):
@patch('kytos.core.controller.Controller.start_controller')
@patch('kytos.core.controller.Controller.create_pidfile')
@patch('kytos.core.controller.Controller.enable_logs')
def test_start(self, *args):
async def test_start(self, *args):
"""Test start method."""
(mock_enable_logs, mock_create_pidfile,
mock_start_controller, mock_db_conn_wait,
mock_init_apm) = args
self.controller.start()
await self.controller.start()

mock_enable_logs.assert_called()
mock_create_pidfile.assert_called()
Expand All @@ -190,13 +190,13 @@ def test_start(self, *args):
@patch('kytos.core.controller.Controller.start_controller')
@patch('kytos.core.controller.Controller.create_pidfile')
@patch('kytos.core.controller.Controller.enable_logs')
def test_start_error_broad_exception(self, *args):
async def test_start_error_broad_exception(self, *args):
"""Test start error handling broad exception."""
(mock_enable_logs, mock_create_pidfile,
mock_start_controller, mock_db_conn_wait,
mock_init_apm, mock_sys) = args
mock_start_controller.side_effect = Exception
self.controller.start()
await self.controller.start()

mock_enable_logs.assert_called()
mock_create_pidfile.assert_called()
Expand All @@ -210,14 +210,14 @@ def test_start_error_broad_exception(self, *args):
@patch('kytos.core.controller.Controller.start_controller')
@patch('kytos.core.controller.Controller.create_pidfile')
@patch('kytos.core.controller.Controller.enable_logs')
def test_start_with_mongodb_and_apm(self, *args):
async def test_start_with_mongodb_and_apm(self, *args):
"""Test start method with database and APM options set."""
(mock_enable_logs, mock_create_pidfile,
mock_start_controller, mock_db_conn_wait,
mock_init_apm) = args
self.controller.options.database = "mongodb"
self.controller.options.apm = "es"
self.controller.start()
await self.controller.start()

mock_enable_logs.assert_called()
mock_create_pidfile.assert_called()
Expand All @@ -229,11 +229,11 @@ def test_start_with_mongodb_and_apm(self, *args):
@patch('kytos.core.controller.sys.exit')
@patch('kytos.core.controller.Controller.create_pidfile')
@patch('kytos.core.controller.Controller.enable_logs')
def test_start_with_invalid_database_backend(self, *args):
async def test_start_with_invalid_database_backend(self, *args):
"""Test start method with unsupported database backend."""
(mock_enable_logs, _, mock_sys_exit) = args
self.controller.options.database = "invalid"
self.controller.start()
await self.controller.start()
mock_enable_logs.assert_called()
mock_sys_exit.assert_called()

Expand Down Expand Up @@ -722,7 +722,7 @@ async def test_start_controller(self, controller, monkeypatch):
controller.api_server = MagicMock()
controller.load_napps = MagicMock()
controller.options.napps_pre_installed = [napp]
controller.start_controller()
await controller.start_controller()
assert controller.buffers

controller.server.serve_forever.assert_called()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_core/test_kytosd.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_async_main(*args):

async_main(MagicMock())

event_loop.call_soon.assert_called_with(controller.start)
event_loop.run_until_complete.assert_called_with(controller.start())

@patch("builtins.open", create=True)
@patch('kytos.core.kytosd.os')
Expand Down
Loading