diff --git a/matemat/webserver/test/test_pagelet_init.py b/matemat/webserver/test/test_pagelet_init.py index 21c30e4..0b2c45a 100644 --- a/matemat/webserver/test/test_pagelet_init.py +++ b/matemat/webserver/test/test_pagelet_init.py @@ -32,7 +32,17 @@ def init(config: Dict[str, str], 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): + self.timer.cancel() + if self.srv is not None: + self.srv._httpd.socket.close() global _INIT_FAIL _INIT_FAIL = False @@ -40,15 +50,12 @@ class TestPageletInitialization(unittest.TestCase): """ Test successful pagelet initialization """ - srv = MatematWebserver('::1', 0, '/nonexistent', '/nonexistent', {}, {}, logging.NOTSET, logging.NullHandler()) - port = int(srv._httpd.socket.getsockname()[1]) - thread = threading.Thread(target=srv.start) + thread = threading.Thread(target=self.srv.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') response = con.getresponse().read() - srv._httpd.shutdown() - srv._httpd.socket.close() + self.srv._httpd.shutdown() self.assertEqual(b'Pagelet Init Test', response) def test_pagelet_init_fail(self): @@ -57,7 +64,5 @@ class TestPageletInitialization(unittest.TestCase): """ global _INIT_FAIL _INIT_FAIL = True - srv = MatematWebserver('::1', 0, '/nonexistent', '/nonexistent', {}, {}, logging.NOTSET, logging.NullHandler()) with self.assertRaises(ValueError): - srv.start() - srv._httpd.socket.close() + self.srv.start()