1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #30451 -- Added ASGI handler and coroutine-safety.

This adds an ASGI handler, asgi.py file for the default project layout,
a few async utilities and adds async-safety to many parts of Django.
This commit is contained in:
Andrew Godwin
2019-04-12 06:15:18 -07:00
committed by Mariusz Felisiak
parent cce47ff65a
commit a415ce70be
38 changed files with 839 additions and 42 deletions

36
tests/async/tests.py Normal file
View File

@@ -0,0 +1,36 @@
from asgiref.sync import async_to_sync
from django.core.exceptions import SynchronousOnlyOperation
from django.test import SimpleTestCase
from django.utils.asyncio import async_unsafe
from .models import SimpleModel
class DatabaseConnectionTest(SimpleTestCase):
"""A database connection cannot be used in an async context."""
@async_to_sync
async def test_get_async_connection(self):
with self.assertRaises(SynchronousOnlyOperation):
list(SimpleModel.objects.all())
class AsyncUnsafeTest(SimpleTestCase):
"""
async_unsafe decorator should work correctly and returns the correct
message.
"""
@async_unsafe
def dangerous_method(self):
return True
@async_to_sync
async def test_async_unsafe(self):
# async_unsafe decorator catches bad access and returns the right
# message.
msg = (
'You cannot call this from an async context - use a thread or '
'sync_to_async.'
)
with self.assertRaisesMessage(SynchronousOnlyOperation, msg):
self.dangerous_method()