Skip to content

Commit

Permalink
[1.5.x] Fixed #19519 again -- Regression in LiveServerTestCase after f…
Browse files Browse the repository at this point in the history
…d1279a.

Backport of 328f5b5.
  • Loading branch information
aaugustin committed Jan 1, 2013
1 parent dfd8623 commit 56e5472
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
15 changes: 10 additions & 5 deletions django/test/client.py
Expand Up @@ -16,7 +16,9 @@
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
Expand Down Expand Up @@ -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):
Expand All @@ -91,14 +95,13 @@ def __init__(self, enforce_csrf_checks=True, *args, **kwargs):

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
Expand All @@ -112,7 +115,9 @@ def __call__(self, environ):
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

Expand Down
2 changes: 0 additions & 2 deletions django/test/testcases.py
Expand Up @@ -641,8 +641,6 @@ def assertContains(self, response, text, count=None, status_code=200,
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:")
Expand Down
8 changes: 1 addition & 7 deletions django/test/utils.py
Expand Up @@ -4,8 +4,6 @@

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
Expand Down Expand Up @@ -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.

This comment has been minimized.

Copy link
@bquinn

bquinn Jan 6, 2013

Contributor

why this change? if you wanted to change the tense of this line it should be changed for all the other lines as well, eg "install the instrumented..." It's a very small issue, but you want a quality release, right?! :-)

This comment has been minimized.

Copy link
@aaugustin

aaugustin Jan 7, 2013

Author Member

In this commit, I reverted all the changes made to this file in fd1279a, including this minor docstring fix. Indeed, I could have kept it.

This comment has been minimized.

Copy link
@bquinn

bquinn Jan 8, 2013

Contributor

oh I see, fair enough! Thanks for replying, and thanks for all your work on Django, we really appreciate it!

- 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
Expand All @@ -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:
Expand Down
1 change: 0 additions & 1 deletion tests/regressiontests/handlers/tests.py
Expand Up @@ -56,6 +56,5 @@ def test_request_signals(self):
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'])

0 comments on commit 56e5472

Please sign in to comment.