summaryrefslogtreecommitdiff
path: root/tests/servers
diff options
context:
space:
mode:
authorbaldychristophe <baldychristophe@gmail.com>2022-11-18 18:26:59 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-11-24 10:52:48 +0100
commit855f5a36e7c8e7a8ce3f62d6ef8c9ae3e073ae3d (patch)
treea991c947067edc1fcfd7d23fa9a49cf8d5ca8f19 /tests/servers
parent1297c0d0d76a708017fe196b61a0ab324df76954 (diff)
Fixed #29062 -- Prevented possibility of database lock when using LiveServerTestCase with in-memory SQLite database.
Thanks Chris Jerdonek for the implementation idea.
Diffstat (limited to 'tests/servers')
-rw-r--r--tests/servers/tests.py32
1 files changed, 29 insertions, 3 deletions
diff --git a/tests/servers/tests.py b/tests/servers/tests.py
index 91f766926b..66f0af1604 100644
--- a/tests/servers/tests.py
+++ b/tests/servers/tests.py
@@ -5,6 +5,7 @@ import errno
import os
import socket
import threading
+import unittest
from http.client import HTTPConnection
from urllib.error import HTTPError
from urllib.parse import urlencode
@@ -12,7 +13,7 @@ from urllib.request import urlopen
from django.conf import settings
from django.core.servers.basehttp import ThreadedWSGIServer, WSGIServer
-from django.db import DEFAULT_DB_ALIAS, connections
+from django.db import DEFAULT_DB_ALIAS, connection, connections
from django.test import LiveServerTestCase, override_settings
from django.test.testcases import LiveServerThread, QuietWSGIRequestHandler
@@ -107,8 +108,33 @@ class LiveServerTestCloseConnectionTest(LiveServerBase):
self.assertIsNone(conn.connection)
+@unittest.skipUnless(connection.vendor == "sqlite", "SQLite specific test.")
+class LiveServerInMemoryDatabaseLockTest(LiveServerBase):
+ def test_in_memory_database_lock(self):
+ """
+ With a threaded LiveServer and an in-memory database, an error can
+ occur when 2 requests reach the server and try to lock the database
+ at the same time, if the requests do not share the same database
+ connection.
+ """
+ conn = self.server_thread.connections_override[DEFAULT_DB_ALIAS]
+ # Open a connection to the database.
+ conn.connect()
+ # Create a transaction to lock the database.
+ cursor = conn.cursor()
+ cursor.execute("BEGIN IMMEDIATE TRANSACTION")
+ try:
+ with self.urlopen("/create_model_instance/") as f:
+ self.assertEqual(f.status, 200)
+ except HTTPError:
+ self.fail("Unexpected error due to a database lock.")
+ finally:
+ # Release the transaction.
+ cursor.execute("ROLLBACK")
+
+
class FailingLiveServerThread(LiveServerThread):
- def _create_server(self):
+ def _create_server(self, connections_override=None):
raise RuntimeError("Error creating server.")
@@ -150,7 +176,7 @@ class LiveServerAddress(LiveServerBase):
class LiveServerSingleThread(LiveServerThread):
- def _create_server(self):
+ def _create_server(self, connections_override=None):
return WSGIServer(
(self.host, self.port), QuietWSGIRequestHandler, allow_reuse_address=False
)