From 91c243f80f36b6aa4ae20461b6914b63db81df17 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Thu, 11 Feb 2021 18:32:09 -0800 Subject: [PATCH] Refs #32416 -- Added LiveServerThread.server_class to ease subclassing. --- django/test/testcases.py | 8 +++++++- tests/servers/test_liveserverthread.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/django/test/testcases.py b/django/test/testcases.py index 114ca85012..d064b47e9b 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -1448,6 +1448,8 @@ class _MediaFilesHandler(FSFilesHandler): class LiveServerThread(threading.Thread): """Thread for running a live http server while the tests are running.""" + server_class = ThreadedWSGIServer + def __init__(self, host, static_handler, connections_override=None, port=0): self.host = host self.port = port @@ -1484,7 +1486,11 @@ class LiveServerThread(threading.Thread): connections.close_all() def _create_server(self): - return ThreadedWSGIServer((self.host, self.port), QuietWSGIRequestHandler, allow_reuse_address=False) + return self.server_class( + (self.host, self.port), + QuietWSGIRequestHandler, + allow_reuse_address=False, + ) def terminate(self): if hasattr(self, 'httpd'): diff --git a/tests/servers/test_liveserverthread.py b/tests/servers/test_liveserverthread.py index a2af459edc..8dc213137a 100644 --- a/tests/servers/test_liveserverthread.py +++ b/tests/servers/test_liveserverthread.py @@ -1,5 +1,6 @@ from django.db import DEFAULT_DB_ALIAS, connections from django.test import LiveServerTestCase, TestCase +from django.test.testcases import LiveServerThread class LiveServerThreadTest(TestCase): @@ -23,3 +24,18 @@ class LiveServerThreadTest(TestCase): self.assertFalse(conn.is_usable()) finally: conn.dec_thread_sharing() + + def test_server_class(self): + class FakeServer: + def __init__(*args, **kwargs): + pass + + class MyServerThread(LiveServerThread): + server_class = FakeServer + + class MyServerTestCase(LiveServerTestCase): + server_thread_class = MyServerThread + + thread = MyServerTestCase._create_server_thread(None) + server = thread._create_server() + self.assertIs(type(server), FakeServer)