Skip to content

Commit ce402ff

Browse files
committed
Merge remote-tracking branch 'origin/master' into edge
2 parents a437fb1 + 83ad168 commit ce402ff

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

tests/support/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
from tests.support.assertover import AssertOver
6666
from tests.support.configsupp import FakeConfiguration
6767
from tests.support.loggersupp import FakeLogger
68+
from tests.support.serversupp import make_wrapped_server
6869

6970

7071
# Basic global logging configuration for testing

tests/support/serversupp.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# --- BEGIN_HEADER ---
5+
#
6+
# serversupp - server threading helpers for unit tests
7+
# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH
8+
#
9+
# This file is part of MiG.
10+
#
11+
# MiG is free software: you can redistribute it and/or modify
12+
# it under the terms of the GNU General Public License as published by
13+
# the Free Software Foundation; either version 2 of the License, or
14+
# (at your option) any later version.
15+
#
16+
# MiG is distributed in the hope that it will be useful,
17+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
# GNU General Public License for more details.
20+
#
21+
# You should have received a copy of the GNU General Public License
22+
# along with this program; if not, write to the Free Software
23+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24+
#
25+
# -- END_HEADER ---
26+
#
27+
28+
"""Server threading related details within the test support library"""
29+
30+
from threading import Thread, Event as ThreadEvent
31+
32+
33+
class ServerWithinThreadExecutor:
34+
"""Execute a server within a thread ensuring we are able to
35+
block until it is ready to recieve test requests.
36+
37+
The only requirements on being able to do so are the server
38+
supporting an on_start callback which is to be called when
39+
the server is ready to handle requests.
40+
"""
41+
42+
def __init__(self, ServerClass, *args, **kwargs):
43+
self._serverclass = ServerClass
44+
self._arguments = (args, kwargs)
45+
self._started = ThreadEvent()
46+
self._thread = None
47+
self._wrapped = None
48+
49+
def run(self):
50+
"""Mimic the same method from the standard thread API"""
51+
server_args, server_kwargs = self._arguments
52+
53+
server_kwargs['on_start'] = lambda _: self._started.set()
54+
55+
self._wrapped = self._serverclass(*server_args, **server_kwargs)
56+
57+
try:
58+
self._wrapped.serve_forever()
59+
except Exception as e:
60+
pass
61+
62+
def start(self):
63+
"""Mimic the same method from the standard thread API"""
64+
self._thread = Thread(target=self.run)
65+
self._thread.start()
66+
67+
def start_wait_until_ready(self):
68+
"""Start server thread and wait for it to be fully initialized"""
69+
self.start()
70+
self._started.wait()
71+
return self
72+
73+
def stop(self):
74+
"""Mimic the same method from the standard thread API"""
75+
self.stop_server()
76+
self._wrapped = None
77+
self._thread.join()
78+
self._thread = None
79+
80+
def stop_server(self):
81+
"""Stop server thread"""
82+
self._wrapped.shutdown()
83+
self._wrapped.server_close()
84+
85+
86+
def make_wrapped_server(ServerClass, *args, **kwargs):
87+
"""Run server wrapped in thread"""
88+
return ServerWithinThreadExecutor(ServerClass, *args, **kwargs)

0 commit comments

Comments
 (0)