diff --git a/django/test/client.py b/django/test/client.py index 77d4de05248..6bdc1cf3d37 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -16,7 +16,9 @@ from django.conf import settings from django.contrib.auth import authenticate, login from django.core.handlers.base import BaseHandler from django.core.handlers.wsgi import WSGIRequest -from django.core.signals import got_request_exception +from django.core.signals import (request_started, request_finished, + got_request_exception) +from django.db import close_connection from django.http import SimpleCookie, HttpRequest, QueryDict from django.template import TemplateDoesNotExist from django.test import signals @@ -76,7 +78,9 @@ def closing_iterator_wrapper(iterable, close): for item in iterable: yield item finally: - close() + request_finished.disconnect(close_connection) + close() # will fire request_finished + request_finished.connect(close_connection) class ClientHandler(BaseHandler): @@ -91,14 +95,13 @@ class ClientHandler(BaseHandler): def __call__(self, environ): from django.conf import settings - from django.core import signals # Set up middleware if needed. We couldn't do this earlier, because # settings weren't available. if self._request_middleware is None: self.load_middleware() - signals.request_started.send(sender=self.__class__) + request_started.send(sender=self.__class__) request = WSGIRequest(environ) # sneaky little hack so that we can easily get round # CsrfViewMiddleware. This makes life easier, and is probably @@ -112,7 +115,9 @@ class ClientHandler(BaseHandler): response.streaming_content = closing_iterator_wrapper( response.streaming_content, response.close) else: - response.close() + request_finished.disconnect(close_connection) + response.close() # will fire request_finished + request_finished.connect(close_connection) return response diff --git a/django/test/testcases.py b/django/test/testcases.py index bb555fbecb0..5ba87caed26 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -641,8 +641,6 @@ class TransactionTestCase(SimpleTestCase): else: content = response.content content = content.decode(response._charset) - # Avoid ResourceWarning about unclosed files. - response.close() if html: content = assert_and_parse_html(self, content, None, "Response's content is not valid HTML:") diff --git a/django/test/utils.py b/django/test/utils.py index a1ff826d6ea..8114ae0e6a0 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -4,8 +4,6 @@ from xml.dom.minidom import parseString, Node from django.conf import settings, UserSettingsHolder from django.core import mail -from django.core.signals import request_finished -from django.db import close_connection from django.test.signals import template_rendered, setting_changed from django.template import Template, loader, TemplateDoesNotExist from django.template.loaders import cached @@ -70,10 +68,8 @@ def setup_test_environment(): """Perform any global pre-test setup. This involves: - Installing the instrumented test renderer - - Setting the email backend to the locmem email backend. + - Set the email backend to the locmem email backend. - Setting the active locale to match the LANGUAGE_CODE setting. - - Disconnecting the request_finished signal to avoid closing - the database connection within tests. """ Template.original_render = Template._render Template._render = instrumented_test_render @@ -85,8 +81,6 @@ def setup_test_environment(): deactivate() - request_finished.disconnect(close_connection) - def teardown_test_environment(): """Perform any global post-test teardown. This involves: diff --git a/tests/regressiontests/handlers/tests.py b/tests/regressiontests/handlers/tests.py index 3cab2aca574..3557a63fd50 100644 --- a/tests/regressiontests/handlers/tests.py +++ b/tests/regressiontests/handlers/tests.py @@ -56,6 +56,5 @@ class SignalsTests(TestCase): def test_request_signals_streaming_response(self): response = self.client.get('/streaming/') self.assertEqual(self.signals, ['started']) - # Avoid self.assertContains, because it explicitly calls response.close() self.assertEqual(b''.join(response.streaming_content), b"streaming content") self.assertEqual(self.signals, ['started', 'finished'])