8000 Fixed ResourceWarning from unclosed SQLite connection. · felixxm/django@7224e39 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7224e39

Browse files
committed
Fixed ResourceWarning from unclosed SQLite connection.
- backends.sqlite.tests.ThreadSharing.test_database_sharing_in_threads - backends.tests.ThreadTests.test_default_connection_thread_local: on SQLite, close() doesn't explicitly close in-memory connections. - servers.tests.LiveServerInMemoryDatabaseLockTest - test_runner.tests.SQLiteInMemoryTestDbs.test_transaction_support Check out python/cpython#108015.
1 parent a9e0f3d commit 7224e39

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

tests/backends/sqlite/tests.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
from pathlib import Path
77
from unittest import mock
88

9-
from django.db import NotSupportedError, connection, transaction
9+
from django.db import (
10+
DEFAULT_DB_ALIAS,
11+
NotSupportedError,
12+
connection,
13+
connections,
14+
transaction,
15+
)
1016
from django.db.models import Aggregate, Avg, StdDev, Sum, Variance
1117
from django.db.utils import ConnectionHandler
1218
from django.test import TestCase, TransactionTestCase, override_settings
@@ -222,11 +228,20 @@ class ThreadSharing(TransactionTestCase):
222228
available_apps = ["backends"]
223229

224230
def test_database_sharing_in_threads(self):
231+
thread_connections = []
232+
225233
def create_object():
226234
Object.objects.create()
227-
228-
create_object()
229-
thread = threading.Thread(target=create_object)
230-
thread.start()
231-
thread.join()
232-
self.assertEqual(Object.objects.count(), 2)
235+
thread_connections.append(connections[DEFAULT_DB_ALIAS].connection)
236+
237+
main_connection = connections[DEFAULT_DB_ALIAS].connection
238+
try:
239+
create_object()
240+
thread = threading.Thread(target=create_object)
241+
thread.start()
242+
thread.join()
243+
self.assertEqual(Object.objects.count(), 2)
244+
finally:
245+
for conn in thread_connections:
246+
if conn is not main_connection:
247+
conn.close()

tests/backends/tests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,8 @@ def runner():
792792
# closed on teardown).
793793
for conn in connections_dict.values():
794794
if conn is not connection and conn.allow_thread_sharing:
795-
conn.close()
795+
conn.validate_thread_sharing()
796+
conn._close()
796797
conn.dec_thread_sharing()
797798

798799
def test_connections_thread_local(self):

tests/servers/tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def test_in_memory_database_lock(self):
115115
connection.
116116
"""
117117
conn = self.server_thread.connections_override[DEFAULT_DB_ALIAS]
118+
source_connection = conn.connection
118119
# Open a connection to the database.
119120
conn.connect()
120121
# Create a transaction to lock the database.
@@ -128,6 +129,7 @@ def test_in_memory_database_lock(self):
128129
finally:
129130
# Release the transaction.
130131
cursor.execute("ROLLBACK")
132+
source_connection.close()
131133

132134

133135
class FailingLiveServerThread(LiveServerThread):

tests/test_runner/tests.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -779,16 +779,21 @@ def test_transaction_support(self):
779779
)
780780
with mock.patch("django.test.utils.connections", new=tested_connections):
781781
other = tested_connections["other"]
782-
DiscoverRunner(verbosity=0).setup_databases()
783-
msg = (
784-
"DATABASES setting '%s' option set to sqlite3's ':memory:' value "
785-
"shouldn't interfere with transaction support detection."
786-
% option_key
787-
)
788-
# Transaction support is properly initialized for the 'other' DB.
789-
self.assertTrue(other.features.supports_transactions, msg)
790-
# And all the DBs report that they support transactions.
791-
self.assertTrue(connections_support_transactions(), msg)
782+
try:
783+
new_test_connections = DiscoverRunner(verbosity=0).setup_databases()
784+
msg = (
785+
f"DATABASES setting '{option_key}' option set to sqlite3's "
786+
"':memory:' value shouldn't interfere with transaction support "
787+
"detection."
788+
)
789+
# Transaction support is properly initialized for the
790+
# 'other' DB.
791+
self.assertTrue(other.features.supports_transactions, msg)
792+
# And all the DBs report that they support transactions.
793+
self.assertTrue(connections_support_transactions(), msg)
794+
finally:
795+
for test_connection, _, _ in new_test_connections:
796+
test_connection._close()
792797

793798

794799
class DummyBackendTest(unittest.TestCase):

0 commit comments

Comments
 (0)
0