-
-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't actually close DB connections during tests (#2101)
- Loading branch information
1 parent
5d8ddd9
commit aa91c28
Showing
7 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from unittest import mock | ||
|
||
from asgiref.testing import ApplicationCommunicator as BaseApplicationCommunicator | ||
|
||
|
||
def no_op(): | ||
pass | ||
|
||
|
||
class ApplicationCommunicator(BaseApplicationCommunicator): | ||
async def send_input(self, message): | ||
with mock.patch("channels.db.close_old_connections", no_op): | ||
return await super().send_input(message) | ||
|
||
async def receive_output(self, timeout=1): | ||
with mock.patch("channels.db.close_old_connections", no_op): | ||
return await super().receive_output(timeout) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from django import db | ||
from django.test import TestCase | ||
|
||
from channels.db import database_sync_to_async | ||
from channels.generic.http import AsyncHttpConsumer | ||
from channels.generic.websocket import AsyncWebsocketConsumer | ||
from channels.testing import HttpCommunicator, WebsocketCommunicator | ||
|
||
|
||
@database_sync_to_async | ||
def basic_query(): | ||
with db.connections["default"].cursor() as cursor: | ||
cursor.execute("SELECT 1234") | ||
return cursor.fetchone()[0] | ||
|
||
|
||
class WebsocketConsumer(AsyncWebsocketConsumer): | ||
async def connect(self): | ||
await basic_query() | ||
await self.accept("fun") | ||
|
||
|
||
class HttpConsumer(AsyncHttpConsumer): | ||
async def handle(self, body): | ||
await basic_query() | ||
await self.send_response( | ||
200, | ||
b"", | ||
headers={b"Content-Type": b"text/plain"}, | ||
) | ||
|
||
|
||
class ConnectionClosingTests(TestCase): | ||
async def test_websocket(self): | ||
self.assertNotRegex( | ||
db.connections["default"].settings_dict.get("NAME"), | ||
"memorydb", | ||
"This bug only occurs when the database is materialized on disk", | ||
) | ||
communicator = WebsocketCommunicator(WebsocketConsumer.as_asgi(), "/") | ||
connected, subprotocol = await communicator.connect() | ||
self.assertTrue(connected) | ||
self.assertEqual(subprotocol, "fun") | ||
|
||
async def test_http(self): | ||
self.assertNotRegex( | ||
db.connections["default"].settings_dict.get("NAME"), | ||
"memorydb", | ||
"This bug only occurs when the database is materialized on disk", | ||
) | ||
communicator = HttpCommunicator( | ||
HttpConsumer.as_asgi(), method="GET", path="/test/" | ||
) | ||
connected = await communicator.get_response() | ||
self.assertTrue(connected) |