Made some tests behave nicer re connection handling

This commit is contained in:
Anssi Kääriäinen 2012-11-26 22:52:44 +02:00
parent 905ea9619b
commit 64f6e0370a
2 changed files with 15 additions and 17 deletions

View File

@ -559,21 +559,27 @@ class ThreadTests(TestCase):
""" """
connections_set = set() connections_set = set()
connection.cursor() connection.cursor()
connections_set.add(connection.connection) connections_set.add(connection)
def runner(): 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() connection.cursor()
connections_set.add(connection.connection) connections_set.add(connection)
for x in range(2): for x in range(2):
t = threading.Thread(target=runner) t = threading.Thread(target=runner)
t.start() t.start()
t.join() 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 # Finish by closing the connections opened by the other threads (the
# connection opened in the main thread will automatically be closed on # connection opened in the main thread will automatically be closed on
# teardown). # teardown).
for conn in connections_set: for conn in connections_set:
if conn != connection.connection: if conn != connection:
conn.close() conn.close()
def test_connections_thread_local(self): def test_connections_thread_local(self):

View File

@ -3,7 +3,8 @@ from __future__ import absolute_import
import datetime import datetime
from django.conf import settings from django.conf import settings
from django.db import backend, transaction, DEFAULT_DB_ALIAS, models from django.db import transaction, DEFAULT_DB_ALIAS, models
from django.db.utils import ConnectionHandler
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
from .models import (Book, Award, AwardNote, Person, Child, Toy, PlayedWith, from .models import (Book, Award, AwardNote, Person, Child, Toy, PlayedWith,
@ -17,17 +18,8 @@ from .models import (Book, Award, AwardNote, Person, Child, Toy, PlayedWith,
class DeleteLockingTest(TransactionTestCase): class DeleteLockingTest(TransactionTestCase):
def setUp(self): def setUp(self):
# Create a second connection to the default database # Create a second connection to the default database
conn_settings = settings.DATABASES[DEFAULT_DB_ALIAS] new_connections = ConnectionHandler(settings.DATABASES)
self.conn2 = backend.DatabaseWrapper({ self.conn2 = new_connections[DEFAULT_DB_ALIAS]
'HOST': conn_settings['HOST'],
'NAME': conn_settings['NAME'],
'OPTIONS': conn_settings['OPTIONS'],
'PASSWORD': conn_settings['PASSWORD'],
'PORT': conn_settings['PORT'],
'USER': conn_settings['USER'],
'TIME_ZONE': settings.TIME_ZONE,
})
# Put both DB connections into managed transaction mode # Put both DB connections into managed transaction mode
transaction.enter_transaction_management() transaction.enter_transaction_management()
transaction.managed(True) transaction.managed(True)