mirror of
https://github.com/django/django.git
synced 2025-06-05 03:29:12 +00:00
Fixed #20238 -- Added threading support to LiveServerTestCase.
This commit is contained in:
parent
c4e2fc5d98
commit
bece837829
@ -77,6 +77,11 @@ class WSGIServer(simple_server.WSGIServer):
|
|||||||
super().handle_error(request, client_address)
|
super().handle_error(request, client_address)
|
||||||
|
|
||||||
|
|
||||||
|
class ThreadedWSGIServer(socketserver.ThreadingMixIn, WSGIServer):
|
||||||
|
"""A threaded version of the WSGIServer"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ServerHandler(simple_server.ServerHandler):
|
class ServerHandler(simple_server.ServerHandler):
|
||||||
def handle_error(self):
|
def handle_error(self):
|
||||||
# Ignore broken pipe errors, otherwise pass on
|
# Ignore broken pipe errors, otherwise pass on
|
||||||
|
@ -21,7 +21,7 @@ from django.core.handlers.wsgi import WSGIHandler, get_path_info
|
|||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
from django.core.management.color import no_style
|
from django.core.management.color import no_style
|
||||||
from django.core.management.sql import emit_post_migrate_signal
|
from django.core.management.sql import emit_post_migrate_signal
|
||||||
from django.core.servers.basehttp import WSGIRequestHandler, WSGIServer
|
from django.core.servers.basehttp import ThreadedWSGIServer, WSGIRequestHandler
|
||||||
from django.db import DEFAULT_DB_ALIAS, connection, connections, transaction
|
from django.db import DEFAULT_DB_ALIAS, connection, connections, transaction
|
||||||
from django.forms.fields import CharField
|
from django.forms.fields import CharField
|
||||||
from django.http import QueryDict
|
from django.http import QueryDict
|
||||||
@ -1248,7 +1248,7 @@ class LiveServerThread(threading.Thread):
|
|||||||
connections.close_all()
|
connections.close_all()
|
||||||
|
|
||||||
def _create_server(self, port):
|
def _create_server(self, port):
|
||||||
return WSGIServer((self.host, port), QuietWSGIRequestHandler, allow_reuse_address=False)
|
return ThreadedWSGIServer((self.host, port), QuietWSGIRequestHandler, allow_reuse_address=False)
|
||||||
|
|
||||||
def terminate(self):
|
def terminate(self):
|
||||||
if hasattr(self, 'httpd'):
|
if hasattr(self, 'httpd'):
|
||||||
|
@ -193,7 +193,7 @@ Templates
|
|||||||
Tests
|
Tests
|
||||||
~~~~~
|
~~~~~
|
||||||
|
|
||||||
* ...
|
* Added threading support to :class:`~django.test.LiveServerTestCase`.
|
||||||
|
|
||||||
URLs
|
URLs
|
||||||
~~~~
|
~~~~
|
||||||
|
@ -130,3 +130,19 @@ class LiveServerPort(LiveServerBase):
|
|||||||
finally:
|
finally:
|
||||||
if hasattr(TestCase, 'server_thread'):
|
if hasattr(TestCase, 'server_thread'):
|
||||||
TestCase.server_thread.terminate()
|
TestCase.server_thread.terminate()
|
||||||
|
|
||||||
|
|
||||||
|
class LiverServerThreadedTests(LiveServerBase):
|
||||||
|
"""If LiverServerTestCase isn't threaded, these tests will hang."""
|
||||||
|
|
||||||
|
def test_view_calls_subview(self):
|
||||||
|
url = '/subview_calling_view/?%s' % urlencode({'url': self.live_server_url})
|
||||||
|
with self.urlopen(url) as f:
|
||||||
|
self.assertEqual(f.read(), b'subview calling view: subview')
|
||||||
|
|
||||||
|
def test_check_model_instance_from_subview(self):
|
||||||
|
url = '/check_model_instance_from_subview/?%s' % urlencode({
|
||||||
|
'url': self.live_server_url,
|
||||||
|
})
|
||||||
|
with self.urlopen(url) as f:
|
||||||
|
self.assertIn(b'emily', f.read())
|
||||||
|
@ -7,4 +7,7 @@ urlpatterns = [
|
|||||||
url(r'^model_view/$', views.model_view),
|
url(r'^model_view/$', views.model_view),
|
||||||
url(r'^create_model_instance/$', views.create_model_instance),
|
url(r'^create_model_instance/$', views.create_model_instance),
|
||||||
url(r'^environ_view/$', views.environ_view),
|
url(r'^environ_view/$', views.environ_view),
|
||||||
|
url(r'^subview_calling_view/$', views.subview_calling_view),
|
||||||
|
url(r'^subview/$', views.subview),
|
||||||
|
url(r'^check_model_instance_from_subview/$', views.check_model_instance_from_subview),
|
||||||
]
|
]
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from urllib.request import urlopen
|
||||||
|
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
|
||||||
from .models import Person
|
from .models import Person
|
||||||
@ -20,3 +22,18 @@ def create_model_instance(request):
|
|||||||
|
|
||||||
def environ_view(request):
|
def environ_view(request):
|
||||||
return HttpResponse("\n".join("%s: %r" % (k, v) for k, v in request.environ.items()))
|
return HttpResponse("\n".join("%s: %r" % (k, v) for k, v in request.environ.items()))
|
||||||
|
|
||||||
|
|
||||||
|
def subview(request):
|
||||||
|
return HttpResponse('subview')
|
||||||
|
|
||||||
|
|
||||||
|
def subview_calling_view(request):
|
||||||
|
response = urlopen(request.GET['url'] + '/subview/')
|
||||||
|
return HttpResponse('subview calling view: {}'.format(response.read().decode()))
|
||||||
|
|
||||||
|
|
||||||
|
def check_model_instance_from_subview(request):
|
||||||
|
urlopen(request.GET['url'] + '/create_model_instance/')
|
||||||
|
response = urlopen(request.GET['url'] + '/model_view/')
|
||||||
|
return HttpResponse('subview calling view: {}'.format(response.read().decode()))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user