From 681380e2377aba038aaa22e1992dea54d6697a70 Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Thu, 23 Nov 2023 12:49:40 -0800 Subject: [PATCH] fetch: test: Hold reference to webserver --- paperdoorknob_test.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/paperdoorknob_test.py b/paperdoorknob_test.py index b1d41d4..cf945d6 100644 --- a/paperdoorknob_test.py +++ b/paperdoorknob_test.py @@ -33,27 +33,30 @@ class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler): class TestFetch(unittest.TestCase): + def _port(self) -> int: + port = self._web_server.socket.getsockname()[1] + assert isinstance(port, int) + return port + def setUp(self) -> None: - web_server = HTTPServer(('', 0), FakeGlowficHTTPRequestHandler) - self._port = web_server.socket.getsockname()[1] - self._thread = threading.Thread(target=web_server.serve_forever) + self._web_server = HTTPServer(('', 0), FakeGlowficHTTPRequestHandler) + self._thread = threading.Thread(target=self._web_server.serve_forever) self._thread.start() - self._stop_server = web_server.shutdown def tearDown(self) -> None: - self._stop_server() + self._web_server.shutdown() self._thread.join() def testFetch(self) -> None: - paperdoorknob.fetch(f"http://localhost:{self._port}", TIMEOUT) + paperdoorknob.fetch(f"http://localhost:{self._port()}", TIMEOUT) def testFetchErrors(self) -> None: with self.assertRaises(requests.HTTPError): paperdoorknob.fetch( - f"http://localhost:{self._port}/not_found", TIMEOUT) + f"http://localhost:{self._port()}/not_found", TIMEOUT) with self.assertRaises(requests.HTTPError): paperdoorknob.fetch( - f"http://localhost:{self._port}/server_error", TIMEOUT) + f"http://localhost:{self._port()}/server_error", TIMEOUT) if __name__ == '__main__': -- 2.44.1