From 0500f3b7993819b88af94e1c3bda4b08d3172201 Mon Sep 17 00:00:00 2001 From: Mateus Meyer Jiacomelli Date: Mon, 14 Nov 2022 16:14:51 +0000 Subject: [PATCH] Starting web services later to fix #1210 --- openlp/core/api/http/server.py | 6 +++--- openlp/core/api/websockets.py | 7 +++++-- openlp/core/ui/mainwindow.py | 6 +++--- .../openlp_core/api/http_server/test_http.py | 20 +++++++++++++------ tests/openlp_core/ui/test_mainwindow.py | 8 ++++---- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/openlp/core/api/http/server.py b/openlp/core/api/http/server.py index 8473129f0..88ccbf60c 100644 --- a/openlp/core/api/http/server.py +++ b/openlp/core/api/http/server.py @@ -78,9 +78,6 @@ class HttpServer(RegistryBase, RegistryProperties, LogMixin): """ super(HttpServer, self).__init__(parent) Registry().register('authentication_token', token_hex()) - if not Registry().get_flag('no_web_server'): - worker = HttpWorker() - run_thread(worker, 'http_server') def bootstrap_post_set_up(self): """ @@ -89,3 +86,6 @@ class HttpServer(RegistryBase, RegistryProperties, LogMixin): create_paths(AppLocation.get_section_data_path('remotes')) self.poller = Poller() Registry().register('poller', self.poller) + if not Registry().get_flag('no_web_server'): + worker = HttpWorker() + run_thread(worker, 'http_server') diff --git a/openlp/core/api/websockets.py b/openlp/core/api/websockets.py index 34d545179..51f59e992 100644 --- a/openlp/core/api/websockets.py +++ b/openlp/core/api/websockets.py @@ -33,7 +33,7 @@ import time from websockets import serve from openlp.core.common.mixins import LogMixin, RegistryProperties -from openlp.core.common.registry import Registry +from openlp.core.common.registry import Registry, RegistryBase from openlp.core.threading import ThreadWorker, run_thread from openlp.core.api.websocketspoll import WebSocketPoller @@ -165,7 +165,7 @@ class WebSocketWorker(ThreadWorker, RegistryProperties, LogMixin): self.event_loop.call_soon_threadsafe(queue.put_nowait, state) -class WebSocketServer(RegistryProperties, QtCore.QObject, LogMixin): +class WebSocketServer(RegistryBase, RegistryProperties, QtCore.QObject, LogMixin): """ Wrapper round a server instance """ @@ -176,6 +176,9 @@ class WebSocketServer(RegistryProperties, QtCore.QObject, LogMixin): super(WebSocketServer, self).__init__() self.worker = None + def bootstrap_post_set_up(self): + self.start() + def start(self): """ Starts the WebSockets server diff --git a/openlp/core/ui/mainwindow.py b/openlp/core/ui/mainwindow.py index 61a73f3d3..583ef9a5c 100644 --- a/openlp/core/ui/mainwindow.py +++ b/openlp/core/ui/mainwindow.py @@ -480,9 +480,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, LogMixin, RegistryPropert self.copy_data = False self.settings.set_up_default_values() self.about_form = AboutForm(self) - self.ws_server = WebSocketServer() - self.ws_server.start() - self.http_server = HttpServer(self) SettingsForm(self) self.formatting_tag_form = FormattingTagForm(self) self.shortcut_form = ShortcutListForm(self) @@ -526,6 +523,9 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, LogMixin, RegistryPropert Registry().register_function('bootstrap_post_set_up', self.bootstrap_post_set_up) # Reset the cursor self.application.set_normal_cursor() + # Starting up web services + self.http_server = HttpServer(self) + self.ws_server = WebSocketServer() def _wait_for_threads(self): """ diff --git a/tests/openlp_core/api/http_server/test_http.py b/tests/openlp_core/api/http_server/test_http.py index c5f36c1a5..2a90401d0 100644 --- a/tests/openlp_core/api/http_server/test_http.py +++ b/tests/openlp_core/api/http_server/test_http.py @@ -21,6 +21,7 @@ """ Functional tests to test the Http Server Class. """ +from pathlib import Path from unittest.mock import patch from openlp.core.api.http.server import HttpServer @@ -29,14 +30,17 @@ from openlp.core.common.registry import Registry @patch('openlp.core.api.http.server.HttpWorker') @patch('openlp.core.api.http.server.run_thread') -def test_server_start(mocked_run_thread, MockHttpWorker, registry): +@patch('openlp.core.api.deploy.AppLocation.get_section_data_path') +def test_server_start(mocked_get_section_data_path, mocked_run_thread, MockHttpWorker, registry): """ Test the starting of the Waitress Server with the disable flag set off """ - # GIVEN: A new httpserver + # GIVEN: A new httpserver and mocked get_section_data_path + mocked_get_section_data_path.return_value = Path('.') # WHEN: I start the server Registry().set_flag('no_web_server', False) - HttpServer() + server = HttpServer() + server.bootstrap_post_set_up() # THEN: the api environment should have been created assert mocked_run_thread.call_count == 1, 'The qthread should have been called once' @@ -45,14 +49,18 @@ def test_server_start(mocked_run_thread, MockHttpWorker, registry): @patch('openlp.core.api.http.server.HttpWorker') @patch('openlp.core.api.http.server.run_thread') -def test_server_start_not_required(mocked_run_thread, MockHttpWorker, registry): +@patch('openlp.core.api.deploy.AppLocation.get_section_data_path') +def test_server_start_not_required(mocked_get_section_data_path, mocked_run_thread, MockHttpWorker, registry): """ Test the starting of the Waitress Server with the disable flag set off """ - # GIVEN: A new httpserver + # GIVEN: A new httpserver and mocked get_section_data_path + mocked_get_section_data_path.return_value = Path('.') + # WHEN: I start the server Registry().set_flag('no_web_server', True) - HttpServer() + server = HttpServer() + server.bootstrap_post_set_up() # THEN: the api environment should have been created assert mocked_run_thread.call_count == 0, 'The qthread should not have have been called' diff --git a/tests/openlp_core/ui/test_mainwindow.py b/tests/openlp_core/ui/test_mainwindow.py index fa822fd43..1e5f7efe8 100644 --- a/tests/openlp_core/ui/test_mainwindow.py +++ b/tests/openlp_core/ui/test_mainwindow.py @@ -349,10 +349,10 @@ def test_mainwindow_configuration(main_window): # WHEN: you check the started functions # THEN: the following registry functions should have been registered - expected_service_list = ['settings', 'settings_thread', 'application', 'main_window', 'http_server', - 'authentication_token', 'settings_form', 'service_manager', 'theme_manager', - 'projector_manager'] - expected_functions_list = ['bootstrap_initialise', 'bootstrap_post_set_up', 'bootstrap_completion', + expected_service_list = ['settings', 'settings_thread', 'application', 'main_window', 'settings_form', + 'service_manager', 'theme_manager', 'projector_manager', 'http_server', + 'authentication_token', 'web_socket_server'] + expected_functions_list = ['bootstrap_post_set_up', 'bootstrap_initialise', 'bootstrap_completion', 'config_screen_changed', 'theme_change_global'] assert list(Registry().service_list.keys()) == expected_service_list, \ 'The service list should have been {}'.format(Registry().service_list.keys())