Skip to content

Commit

Permalink
Fixed ResourceWarning from unclosed SQLite connection.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
felixxm committed Aug 23, 2023
1 parent a9e0f3d commit 7224e39
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
29 changes: 22 additions & 7 deletions tests/backends/sqlite/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
from pathlib import Path
from unittest import mock

from django.db import NotSupportedError, connection, transaction
from django.db import (
DEFAULT_DB_ALIAS,
NotSupportedError,
connection,
connections,
transaction,
)
from django.db.models import Aggregate, Avg, StdDev, Sum, Variance
from django.db.utils import ConnectionHandler
from django.test import TestCase, TransactionTestCase, override_settings
Expand Down Expand Up @@ -222,11 +228,20 @@ class ThreadSharing(TransactionTestCase):
available_apps = ["backends"]

def test_database_sharing_in_threads(self):
thread_connections = []

def create_object():
Object.objects.create()

create_object()
thread = threading.Thread(target=create_object)
thread.start()
thread.join()
self.assertEqual(Object.objects.count(), 2)
thread_connections.append(connections[DEFAULT_DB_ALIAS].connection)

main_connection = connections[DEFAULT_DB_ALIAS].connection
try:
create_object()
thread = threading.Thread(target=create_object)
thread.start()
thread.join()
self.assertEqual(Object.objects.count(), 2)
finally:
for conn in thread_connections:
if conn is not main_connection:
conn.close()
3 changes: 2 additions & 1 deletion tests/backends/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,8 @@ def runner():
# closed on teardown).
for conn in connections_dict.values():
if conn is not connection and conn.allow_thread_sharing:
conn.close()
conn.validate_thread_sharing()
conn._close()
conn.dec_thread_sharing()

def test_connections_thread_local(self):
Expand Down
2 changes: 2 additions & 0 deletions tests/servers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def test_in_memory_database_lock(self):
connection.
"""
conn = self.server_thread.connections_override[DEFAULT_DB_ALIAS]
source_connection = conn.connection
# Open a connection to the database.
conn.connect()
# Create a transaction to lock the database.
Expand All @@ -128,6 +129,7 @@ def test_in_memory_database_lock(self):
finally:
# Release the transaction.
cursor.execute("ROLLBACK")
source_connection.close()


class FailingLiveServerThread(LiveServerThread):
Expand Down
25 changes: 15 additions & 10 deletions tests/test_runner/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,16 +779,21 @@ def test_transaction_support(self):
)
with mock.patch("django.test.utils.connections", new=tested_connections):
other = tested_connections["other"]
DiscoverRunner(verbosity=0).setup_databases()
msg = (
"DATABASES setting '%s' option set to sqlite3's ':memory:' value "
"shouldn't interfere with transaction support detection."
% option_key
)
# Transaction support is properly initialized for the 'other' DB.
self.assertTrue(other.features.supports_transactions, msg)
# And all the DBs report that they support transactions.
self.assertTrue(connections_support_transactions(), msg)
try:
new_test_connections = DiscoverRunner(verbosity=0).setup_databases()
msg = (
f"DATABASES setting '{option_key}' option set to sqlite3's "
"':memory:' value shouldn't interfere with transaction support "
"detection."
)
# Transaction support is properly initialized for the
# 'other' DB.
self.assertTrue(other.features.supports_transactions, msg)
# And all the DBs report that they support transactions.
self.assertTrue(connections_support_transactions(), msg)
finally:
for test_connection, _, _ in new_test_connections:
test_connection._close()


class DummyBackendTest(unittest.TestCase):
Expand Down

0 comments on commit 7224e39

Please sign in to comment.