Skip to content

Commit

Permalink
Carve out the server testing facility as a separate support lib file.
Browse files Browse the repository at this point in the history
  • Loading branch information
albu-diku committed Aug 28, 2024
1 parent 924652e commit e9516e6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 44 deletions.
1 change: 1 addition & 0 deletions tests/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 75 additions & 0 deletions tests/support/serversupp.py
Original file line number Diff line number Diff line change
@@ -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)
46 changes: 2 additions & 44 deletions tests/test_mig_server_grid_openid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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__':
Expand Down

0 comments on commit e9516e6

Please sign in to comment.