diff --git a/tests/support/__init__.py b/tests/support/__init__.py index d3f47642d..8d9533067 100644 --- a/tests/support/__init__.py +++ b/tests/support/__init__.py @@ -62,6 +62,7 @@ from tests.support.configsupp import FakeConfiguration from tests.support.loggersupp import FakeLogger +from tests.support.serversupp import make_wrapped_server # Basic global logging configuration for testing diff --git a/tests/support/serversupp.py b/tests/support/serversupp.py new file mode 100644 index 000000000..efbce831c --- /dev/null +++ b/tests/support/serversupp.py @@ -0,0 +1,75 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# --- BEGIN_HEADER --- +# +# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH +# +# This file is part of MiG. +# +# MiG is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# MiG is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# -- END_HEADER --- +# + +"""Configuration related details within the test support library.""" + +import threading + + +class ServerWithinThreadExecutor(threading.Thread): + """Execute a server within a thread ensuring we are able to + block until it is ready to recieve test requests. + + The only requirements on being able to do so are the server + supporting an on_start callback which is to be called when + the server is ready to handle requests.""" + + def __init__(self, ServerClass, *args, **kwargs): + super(ServerWithinThreadExecutor, self).__init__() + self._serverclass = ServerClass + self._arguments = (args, kwargs) + self._started = threading.Event() + self._wrapped = None + + def run(self): + server_args, server_kwargs = self._arguments + + server_kwargs['on_start'] = lambda _: self._started.set() + + self._wrapped = self._serverclass(*server_args, **server_kwargs) + + try: + self._wrapped.serve_forever() + except Exception as e: + pass + + def start_wait_until_ready(self): + self.start() + if not self._started.is_set(): + self._started.wait() + return self + + def stop(self): + self.stop_server() + self.join() + + def stop_server(self): + self._wrapped.shutdown() + self._wrapped.server_close() + + +def make_wrapped_server(ServerClass, *args, **kwargs): + return ServerWithinThreadExecutor(ServerClass, *args, **kwargs) diff --git a/tests/test_mig_server_grid_openid.py b/tests/test_mig_server_grid_openid.py index 88412ed45..6f70c1ff9 100644 --- a/tests/test_mig_server_grid_openid.py +++ b/tests/test_mig_server_grid_openid.py @@ -3,7 +3,7 @@ import sys import threading -from tests.support import PY2, MIG_BASE, testmain, MigTestCase +from tests.support import PY2, MIG_BASE, MigTestCase, testmain, make_wrapped_server from mig.server.grid_openid import ThreadedOpenIDHTTPServer, _extend_configuration, main from mig.shared.conf import get_configuration_object @@ -21,48 +21,6 @@ from urllib.request import urlopen -class ServerWithinThreadExecutor(threading.Thread): - """Execute a server within a thread ensuring we are able to - block until it is ready to recieve test requests. - - The only requirements on being able to do so are the server - supporting an on_start callback which is to be called when - the server is ready to handle requests.""" - - def __init__(self, ServerClass, *args, **kwargs): - super(ServerWithinThreadExecutor, self).__init__() - self._serverclass = ServerClass - self._arguments = (args, kwargs) - self._started = threading.Event() - self._wrapped = None - - def run(self): - server_args, server_kwargs = self._arguments - - server_kwargs['on_start'] = lambda _: self._started.set() - - self._wrapped = self._serverclass(*server_args, **server_kwargs) - - try: - self._wrapped.serve_forever() - except Exception as e: - pass - - def start_wait_until_ready(self): - self.start() - if not self._started.is_set(): - self._started.wait() - return self - - def stop(self): - self.stop_server() - self.join() - - def stop_server(self): - self._wrapped.shutdown() - self._wrapped.server_close() - - class MigServerGrid_openid(MigTestCase): def before_each(self): self.server_addr = None @@ -121,7 +79,7 @@ def _make_configuration(test_logger, server_addr): @staticmethod def _make_server(configuration): - return ServerWithinThreadExecutor(ThreadedOpenIDHTTPServer, configuration) + return make_wrapped_server(ThreadedOpenIDHTTPServer, configuration) if __name__ == '__main__':