From f6cd669ff203192c29495174e53da6b16883b039 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Fri, 19 Aug 2016 09:47:41 -0700 Subject: [PATCH] Fixed #22414 -- Ensured that LiveServerTestCase closes connections. --- django/test/testcases.py | 4 +++- tests/servers/test_liveserverthread.py | 28 ++++++++++++++++++++++++++ tests/servers/tests.py | 3 ++- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/servers/test_liveserverthread.py diff --git a/django/test/testcases.py b/django/test/testcases.py index 399c8adf57..8fd84b1423 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -1262,6 +1262,8 @@ class LiveServerThread(threading.Thread): except Exception as e: self.error = e self.is_ready.set() + finally: + connections.close_all() def _create_server(self, port): return WSGIServer((self.host, port), QuietWSGIRequestHandler, allow_reuse_address=False) @@ -1271,6 +1273,7 @@ class LiveServerThread(threading.Thread): # Stop the WSGI server self.httpd.shutdown() self.httpd.server_close() + self.join() class LiveServerTestCase(TransactionTestCase): @@ -1335,7 +1338,6 @@ class LiveServerTestCase(TransactionTestCase): if hasattr(cls, 'server_thread'): # Terminate the live server's thread cls.server_thread.terminate() - cls.server_thread.join() # Restore sqlite in-memory database connections' non-shareability for conn in connections.all(): diff --git a/tests/servers/test_liveserverthread.py b/tests/servers/test_liveserverthread.py new file mode 100644 index 0000000000..d39aac8183 --- /dev/null +++ b/tests/servers/test_liveserverthread.py @@ -0,0 +1,28 @@ +from django.db import DEFAULT_DB_ALIAS, connections +from django.test import LiveServerTestCase, TestCase + + +class LiveServerThreadTest(TestCase): + + def run_live_server_thread(self, connections_override=None): + thread = LiveServerTestCase._create_server_thread(connections_override) + thread.daemon = True + thread.start() + thread.is_ready.wait() + thread.terminate() + + def test_closes_connections(self): + conn = connections[DEFAULT_DB_ALIAS] + if conn.vendor == 'sqlite' and conn.is_in_memory_db(): + self.skipTest("the sqlite backend's close() method is a no-op when using an in-memory database") + # Pass a connection to the thread to check they are being closed. + connections_override = {DEFAULT_DB_ALIAS: conn} + + saved_sharing = conn.allow_thread_sharing + try: + conn.allow_thread_sharing = True + self.assertTrue(conn.is_usable()) + self.run_live_server_thread(connections_override) + self.assertFalse(conn.is_usable()) + finally: + conn.allow_thread_sharing = saved_sharing diff --git a/tests/servers/tests.py b/tests/servers/tests.py index aeddf86f62..3d959b4b23 100644 --- a/tests/servers/tests.py +++ b/tests/servers/tests.py @@ -153,4 +153,5 @@ class LiveServerPort(LiveServerBase): "Acquired duplicate server addresses for server threads: %s" % self.live_server_url ) finally: - TestCase.tearDownClass() + if hasattr(TestCase, 'server_thread'): + TestCase.server_thread.terminate()