mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Made some tests behave nicer re connection handling
This commit is contained in:
@@ -559,21 +559,27 @@ class ThreadTests(TestCase):
|
||||
"""
|
||||
connections_set = set()
|
||||
connection.cursor()
|
||||
connections_set.add(connection.connection)
|
||||
connections_set.add(connection)
|
||||
def runner():
|
||||
from django.db import connection
|
||||
# Passing django.db.connection between threads doesn't work while
|
||||
# connections[DEFAULT_DB_ALIAS] does.
|
||||
from django.db import connections
|
||||
connection = connections[DEFAULT_DB_ALIAS]
|
||||
connection.cursor()
|
||||
connections_set.add(connection.connection)
|
||||
connections_set.add(connection)
|
||||
for x in range(2):
|
||||
t = threading.Thread(target=runner)
|
||||
t.start()
|
||||
t.join()
|
||||
self.assertEqual(len(connections_set), 3)
|
||||
# Check that each created connection got different inner connection.
|
||||
self.assertEqual(
|
||||
len(set([conn.connection for conn in connections_set])),
|
||||
3)
|
||||
# Finish by closing the connections opened by the other threads (the
|
||||
# connection opened in the main thread will automatically be closed on
|
||||
# teardown).
|
||||
for conn in connections_set:
|
||||
if conn != connection.connection:
|
||||
if conn != connection:
|
||||
conn.close()
|
||||
|
||||
def test_connections_thread_local(self):
|
||||
|
||||
Reference in New Issue
Block a user