mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #34865 -- Released memory earlier than garbage collection on database wrapping layers.
Thank you Florian Apolloner, Jake Howard and Patryk Zawadzki for the clarifying comments and reviews.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import gc
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from django.db import DEFAULT_DB_ALIAS, connection, connections, transaction
|
||||
@@ -60,6 +61,36 @@ class DatabaseWrapperTests(SimpleTestCase):
|
||||
with patch.object(connection.features, "minimum_database_version", None):
|
||||
connection.check_database_version_supported()
|
||||
|
||||
def test_release_memory_without_garbage_collection(self):
|
||||
# Schedule the restore of the garbage collection settings.
|
||||
self.addCleanup(gc.set_debug, 0)
|
||||
self.addCleanup(gc.enable)
|
||||
|
||||
# Disable automatic garbage collection to control when it's triggered,
|
||||
# then run a full collection cycle to ensure `gc.garbage` is empty.
|
||||
gc.disable()
|
||||
gc.collect()
|
||||
|
||||
# The garbage list isn't automatically populated to avoid CPU overhead,
|
||||
# so debugging needs to be enabled to track all unreachable items and
|
||||
# have them stored in `gc.garbage`.
|
||||
gc.set_debug(gc.DEBUG_SAVEALL)
|
||||
|
||||
# Create a new connection that will be closed during the test, and also
|
||||
# ensure that a `DatabaseErrorWrapper` is created for this connection.
|
||||
test_connection = connection.copy()
|
||||
with test_connection.wrap_database_errors:
|
||||
self.assertEqual(test_connection.queries, [])
|
||||
|
||||
# Close the connection and remove references to it. This will mark all
|
||||
# objects related to the connection as garbage to be collected.
|
||||
test_connection.close()
|
||||
test_connection = None
|
||||
|
||||
# Enforce garbage collection to populate `gc.garbage` for inspection.
|
||||
gc.collect()
|
||||
self.assertEqual(gc.garbage, [])
|
||||
|
||||
|
||||
class DatabaseWrapperLoggingTests(TransactionTestCase):
|
||||
available_apps = ["backends"]
|
||||
|
||||
Reference in New Issue
Block a user