| 1 |
from threading import Thread |
| 2 |
|
| 3 |
from django.db import connection, transaction |
| 4 |
from django.test import skipUnlessDBFeature |
| 5 |
|
| 6 |
from .models import Test |
| 7 |
from .test_utils import TestUtilsMixin, FilteredTransactionTestCase |
| 8 |
|
| 9 |
|
| 10 |
class TestThread(Thread): |
| 11 |
def start_and_join(self): |
| 12 |
self.start() |
| 13 |
self.join() |
| 14 |
return self.t |
| 15 |
|
| 16 |
def run(self): |
| 17 |
self.t = Test.objects.first() |
| 18 |
connection.close() |
| 19 |
|
| 20 |
|
| 21 |
@skipUnlessDBFeature('test_db_allows_multiple_connections') |
| 22 |
class ThreadSafetyTestCase(TestUtilsMixin, FilteredTransactionTestCase): |
| 23 |
def test_concurrent_caching(self): |
| 24 |
t1 = TestThread().start_and_join() |
| 25 |
t = Test.objects.create(name='test') |
| 26 |
t2 = TestThread().start_and_join() |
| 27 |
|
| 28 |
self.assertEqual(t1, None) |
| 29 |
self.assertEqual(t2, t) |
| 30 |
|
| 31 |
def test_concurrent_caching_during_atomic(self): |
| 32 |
with self.assertNumQueries(1): |
| 33 |
with transaction.atomic(): |
| 34 |
t1 = TestThread().start_and_join() |
| 35 |
t = Test.objects.create(name='test') |
| 36 |
t2 = TestThread().start_and_join() |
| 37 |
|
| 38 |
self.assertEqual(t1, None) |
| 39 |
self.assertEqual(t2, None) |
| 40 |
|
| 41 |
with self.assertNumQueries(1): |
| 42 |
data = Test.objects.first() |
| 43 |
self.assertEqual(data, t) |
| 44 |
|
| 45 |
def test_concurrent_caching_before_and_during_atomic_1(self): |
| 46 |
t1 = TestThread().start_and_join() |
| 47 |
|
| 48 |
with self.assertNumQueries(1): |
| 49 |
with transaction.atomic(): |
| 50 |
t2 = TestThread().start_and_join() |
| 51 |
t = Test.objects.create(name='test') |
| 52 |
|
| 53 |
self.assertEqual(t1, None) |
| 54 |
self.assertEqual(t2, None) |
| 55 |
|
| 56 |
with self.assertNumQueries(1): |
| 57 |
data = Test.objects.first() |
| 58 |
self.assertEqual(data, t) |
| 59 |
|
| 60 |
def test_concurrent_caching_before_and_during_atomic_2(self): |
| 61 |
t1 = TestThread().start_and_join() |
| 62 |
|
| 63 |
with self.assertNumQueries(1): |
| 64 |
with transaction.atomic(): |
| 65 |
t = Test.objects.create(name='test') |
| 66 |
t2 = TestThread().start_and_join() |
| 67 |
|
| 68 |
self.assertEqual(t1, None) |
| 69 |
self.assertEqual(t2, None) |
| 70 |
|
| 71 |
with self.assertNumQueries(1): |
| 72 |
data = Test.objects.first() |
| 73 |
self.assertEqual(data, t) |
| 74 |
|
| 75 |
def test_concurrent_caching_during_and_after_atomic_1(self): |
| 76 |
with self.assertNumQueries(1): |
| 77 |
with transaction.atomic(): |
| 78 |
t1 = TestThread().start_and_join() |
| 79 |
t = Test.objects.create(name='test') |
| 80 |
|
| 81 |
t2 = TestThread().start_and_join() |
| 82 |
|
| 83 |
self.assertEqual(t1, None) |
| 84 |
self.assertEqual(t2, t) |
| 85 |
|
| 86 |
with self.assertNumQueries(0): |
| 87 |
data = Test.objects.first() |
| 88 |
self.assertEqual(data, t) |
| 89 |
|
| 90 |
def test_concurrent_caching_during_and_after_atomic_2(self): |
| 91 |
with self.assertNumQueries(1): |
| 92 |
with transaction.atomic(): |
| 93 |
t = Test.objects.create(name='test') |
| 94 |
t1 = TestThread().start_and_join() |
| 95 |
|
| 96 |
t2 = TestThread().start_and_join() |
| 97 |
|
| 98 |
self.assertEqual(t1, None) |
| 99 |
self.assertEqual(t2, t) |
| 100 |
|
| 101 |
with self.assertNumQueries(0): |
| 102 |
data = Test.objects.first() |
| 103 |
self.assertEqual(data, t) |
| 104 |
|
| 105 |
def test_concurrent_caching_during_and_after_atomic_3(self): |
| 106 |
with self.assertNumQueries(1): |
| 107 |
with transaction.atomic(): |
| 108 |
t1 = TestThread().start_and_join() |
| 109 |
t = Test.objects.create(name='test') |
| 110 |
t2 = TestThread().start_and_join() |
| 111 |
|
| 112 |
t3 = TestThread().start_and_join() |
| 113 |
|
| 114 |
self.assertEqual(t1, None) |
| 115 |
self.assertEqual(t2, None) |
| 116 |
self.assertEqual(t3, t) |
| 117 |
|
| 118 |
with self.assertNumQueries(0): |
| 119 |
data = Test.objects.first() |
| 120 |
self.assertEqual(data, t) |