From 0fe8ef55a2418e1853a64a7ac0b8826d8cc96250 Mon Sep 17 00:00:00 2001 From: s3lph Date: Sun, 19 Aug 2018 01:06:38 +0200 Subject: [PATCH] Pagelet initialization tests: Added safeguards against non-terminating threads. --- matemat/webserver/test/test_pagelet_init.py | 23 +++++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) 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()