Pagelet initialization tests: Added safeguards against non-terminating threads.

This commit is contained in:
s3lph 2018-08-19 01:06:38 +02:00
parent cd87695dd1
commit 0fe8ef55a2

View file

@ -32,7 +32,17 @@ def init(config: Dict[str, str],
class TestPageletInitialization(unittest.TestCase): class TestPageletInitialization(unittest.TestCase):
def setUp(self):
self.srv = MatematWebserver('::1', 0, '/nonexistent', '/nonexistent', {}, {},
logging.NOTSET, logging.NullHandler())
self.srv_port = int(self.srv._httpd.socket.getsockname()[1])
self.timer = threading.Timer(5.0, self.srv._httpd.shutdown)
self.timer.start()
def tearDown(self): def tearDown(self):
self.timer.cancel()
if self.srv is not None:
self.srv._httpd.socket.close()
global _INIT_FAIL global _INIT_FAIL
_INIT_FAIL = False _INIT_FAIL = False
@ -40,15 +50,12 @@ class TestPageletInitialization(unittest.TestCase):
""" """
Test successful pagelet initialization Test successful pagelet initialization
""" """
srv = MatematWebserver('::1', 0, '/nonexistent', '/nonexistent', {}, {}, logging.NOTSET, logging.NullHandler()) thread = threading.Thread(target=self.srv.start)
port = int(srv._httpd.socket.getsockname()[1])
thread = threading.Thread(target=srv.start)
thread.start() thread.start()
con = http.client.HTTPConnection(f'[::1]:{port}') con = http.client.HTTPConnection(f'[::1]:{self.srv_port}')
con.request('GET', '/just/testing/init') con.request('GET', '/just/testing/init')
response = con.getresponse().read() response = con.getresponse().read()
srv._httpd.shutdown() self.srv._httpd.shutdown()
srv._httpd.socket.close()
self.assertEqual(b'Pagelet Init Test', response) self.assertEqual(b'Pagelet Init Test', response)
def test_pagelet_init_fail(self): def test_pagelet_init_fail(self):
@ -57,7 +64,5 @@ class TestPageletInitialization(unittest.TestCase):
""" """
global _INIT_FAIL global _INIT_FAIL
_INIT_FAIL = True _INIT_FAIL = True
srv = MatematWebserver('::1', 0, '/nonexistent', '/nonexistent', {}, {}, logging.NOTSET, logging.NullHandler())
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
srv.start() self.srv.start()
srv._httpd.socket.close()