-
-
Notifications
You must be signed in to change notification settings - Fork 817
Fix DatabaseSyncToAsync #1290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Fix DatabaseSyncToAsync #1290
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
680486e
Fix DatabaseSyncToAsync
blueyed e113ecb
squash! Fix DatabaseSyncToAsync
blueyed 046d6a1
squash! squash! Fix DatabaseSyncToAsync
blueyed e1fd3a6
fixup! squash! squash! Fix DatabaseSyncToAsync
blueyed 9a1031f
add test(s)
blueyed 784fbca
Travis: install psycopg2
blueyed 2d60402
fixup! add test(s)
blueyed 6e2017f
test: use_fix
blueyed 0a22cb5
fixup! squash! squash! Fix DatabaseSyncToAsync
blueyed ede6f8f
fixup! add test(s)
blueyed c4335ea
pytest: addopts: -ra
blueyed f785c96
test_database_sync_to_async: result.ret == 1 without fix
blueyed ace21eb
Fix DatabaseSyncToAsync / remove DatabaseSyncToAsyncForTests
blueyed 9a37f68
Use allow_thread_sharing for Django < 2.2
blueyed 5d4737f
ci: Travis: services: postgresql
blueyed fe86622
improve restore_allow_thread_sharing
blueyed 9a12b87
black
blueyed 53e3706
move _close_old_connections to module level
blueyed 41ed3a1
use _close_old_connections in channels.signals
blueyed 56e31e0
update test: use @database_sync_to_async outside of async def
blueyed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
from django.db import close_old_connections | ||
from django.dispatch import Signal | ||
|
||
from channels.db import _close_old_connections | ||
|
||
consumer_started = Signal(providing_args=["environ"]) | ||
consumer_finished = Signal() | ||
|
||
# Connect connection closer to consumer finished as well | ||
consumer_finished.connect(close_old_connections) | ||
consumer_finished.connect(_close_old_connections) |
This file contains hidden or 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 |
---|---|---|
|
@@ -15,3 +15,4 @@ universal=1 | |
|
||
[tool:pytest] | ||
testpaths = tests | ||
addopts = -ra |
This file contains hidden or 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,71 @@ | ||
import pytest | ||
|
||
pytest_plugins = ["pytester"] | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.asyncio | ||
@pytest.mark.parametrize( | ||
"db_engine", ("django.db.backends.sqlite3", "django.db.backends.postgresql") | ||
) | ||
@pytest.mark.parametrize("conn_max_age", (0, 600)) | ||
async def test_database_sync_to_async(db_engine, conn_max_age, testdir): | ||
if db_engine == "django.db.backends.postgresql": | ||
pytest.importorskip("psycopg2") | ||
|
||
testdir.makeconftest( | ||
""" | ||
from django.conf import settings | ||
|
||
settings.configure( | ||
DATABASES={ | ||
"default": { | ||
"ENGINE": %r, | ||
"CONN_MAX_AGE": %d, | ||
"NAME": "channels_tests", | ||
} | ||
}, | ||
INSTALLED_APPS=[ | ||
"django.contrib.auth", | ||
"django.contrib.contenttypes", | ||
"django.contrib.sessions", | ||
"django.contrib.admin", | ||
"channels", | ||
], | ||
) | ||
""" | ||
% (db_engine, conn_max_age) | ||
) | ||
p1 = testdir.makepyfile( | ||
""" | ||
import pytest | ||
from django.contrib.auth.models import User | ||
|
||
from channels.db import database_sync_to_async | ||
|
||
@database_sync_to_async | ||
def create_obj(**kwargs): | ||
User.objects.create(**kwargs) | ||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.django_db | ||
async def test_inner(): | ||
from django.db import connections | ||
|
||
conn = connections["default"] | ||
assert conn.in_atomic_block | ||
|
||
assert User.objects.count() == 0 | ||
|
||
await create_obj(username="alice") | ||
await create_obj(username="bob") | ||
assert User.objects.count() == 2 | ||
|
||
@pytest.mark.django_db | ||
def test_check_rolled_back(): | ||
from django.contrib.auth.models import User | ||
assert User.objects.count() == 0 | ||
""" | ||
) | ||
result = testdir.runpytest_subprocess(str(p1)) | ||
assert result.ret == 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.