From a7901c2e090d720ef0427552387be7f14cbe2ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Goc=C5=82awski?= Date: Sun, 30 Aug 2015 14:27:38 +0200 Subject: [PATCH] Refs #25328 -- Refactored LiveServerTestCase to make it extensible. --- django/test/testcases.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/django/test/testcases.py b/django/test/testcases.py index 1ed3124d57..0c704ad951 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -1251,8 +1251,7 @@ class LiveServerThread(threading.Thread): # one that is free to use for the WSGI server. for index, port in enumerate(self.possible_ports): try: - self.httpd = WSGIServer( - (self.host, port), QuietWSGIRequestHandler) + self.httpd = self._create_server(port) except socket.error as e: if (index + 1 < len(self.possible_ports) and e.errno == errno.EADDRINUSE): @@ -1276,6 +1275,9 @@ class LiveServerThread(threading.Thread): self.error = e self.is_ready.set() + def _create_server(self, port): + return WSGIServer((self.host, port), QuietWSGIRequestHandler) + def terminate(self): if hasattr(self, 'httpd'): # Stop the WSGI server @@ -1338,9 +1340,7 @@ class LiveServerTestCase(TransactionTestCase): except Exception: msg = 'Invalid address ("%s") for live server.' % specified_address six.reraise(ImproperlyConfigured, ImproperlyConfigured(msg), sys.exc_info()[2]) - cls.server_thread = LiveServerThread(host, possible_ports, - cls.static_handler, - connections_override=connections_override) + cls.server_thread = cls._create_server_thread(host, possible_ports, connections_override) cls.server_thread.daemon = True cls.server_thread.start() @@ -1352,6 +1352,15 @@ class LiveServerTestCase(TransactionTestCase): cls._tearDownClassInternal() raise cls.server_thread.error + @classmethod + def _create_server_thread(cls, host, possible_ports, connections_override): + return LiveServerThread( + host, + possible_ports, + cls.static_handler, + connections_override=connections_override, + ) + @classmethod def _tearDownClassInternal(cls): # There may not be a 'server_thread' attribute if setUpClass() for some