Code

Ticket #9479: 9479-r10858.2.diff

File 9479-r10858.2.diff, 2.5 KB (added by russellm, 5 years ago)

Test case demonstrating problem

Line 
1diff --git a/tests/regressiontests/delete_regress/__init__.py b/tests/regressiontests/delete_regress/__init__.py
2new file mode 100644
3index 0000000..8b13789
4--- /dev/null
5+++ b/tests/regressiontests/delete_regress/__init__.py
6@@ -0,0 +1 @@
7+
8diff --git a/tests/regressiontests/delete_regress/models.py b/tests/regressiontests/delete_regress/models.py
9new file mode 100644
10index 0000000..dfb18d4
11--- /dev/null
12+++ b/tests/regressiontests/delete_regress/models.py
13@@ -0,0 +1,51 @@
14+from django.conf import settings
15+from django.db import models, backend, connection, transaction
16+from django.db.models import sql, query
17+from django.test import TransactionTestCase
18+
19+class Book(models.Model):
20+    pagecount = models.IntegerField()
21+
22+if settings.DATABASE_ENGINE != 'sqlite3':
23+    class DeleteLockingTest(TransactionTestCase):
24+        def setUp(self):
25+            # Create a second connection to the database
26+            self.conn2 = backend.DatabaseWrapper({
27+                'DATABASE_HOST': settings.DATABASE_HOST,
28+                'DATABASE_NAME': settings.DATABASE_NAME,
29+                'DATABASE_OPTIONS': settings.DATABASE_OPTIONS,
30+                'DATABASE_PASSWORD': settings.DATABASE_PASSWORD,
31+                'DATABASE_PORT': settings.DATABASE_PORT,
32+                'DATABASE_USER': settings.DATABASE_USER,
33+                'TIME_ZONE': settings.TIME_ZONE,
34+            })
35+
36+            # Put both DB connections into managed transaction mode
37+            transaction.enter_transaction_management(True)
38+            self.conn2._enter_transaction_management(True)
39+
40+        def tearDown(self):
41+            # Close down the second connection.
42+            self.conn2.close()
43+
44+        def test_concurrent_delete(self):
45+            "Deletes on concurrent transactions don't collide and lock the database"
46+
47+            # Create some dummy data
48+            b1 = Book(id=1, pagecount=100)
49+            b2 = Book(id=2, pagecount=200)
50+            b1.save()
51+            b2.save()
52+
53+            transaction.commit()
54+
55+            self.assertEquals(2, Book.objects.count())
56+
57+            # Now with connection 1 committed, delete something using connection 2
58+            # This causes an infinite loop under MySQL InnoDB if we don't keep track
59+            # of already deleted objects.
60+            cursor2 = self.conn2.cursor()
61+            q = sql.Query(Book, self.conn2)
62+            query.QuerySet(Book, q).filter(pk=1).delete()
63+
64+            self.assertEquals(1, Book.objects.count())