gleb-sevruk / pycrunch-engine

NCrunch inspired tool for continuous testing Python
https://pycrunch.com
Other
57 stars 8 forks source link

[Django] You cannot call this from an async context - use a thread or sync_to_async. #36

Closed valeedmalik closed 3 years ago

valeedmalik commented 3 years ago

If I run manage.py test, that will work as expected however when running via pycrunch IJ plugin I see this: django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

django==3.1.4

actual test:

from django.test import TestCase

class Entire_Upload(TestCase):
  def setUp(self):
    pass

  def test_mov(self):
    pass

full error output:

E
==================================== ERRORS ====================================
___________________ ERROR at setup of Entire_Upload.test_mov ___________________

cls = <class 'tests.test_video_upload.Entire_Upload'>

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        if not cls._databases_support_transactions():
            return
>       cls.cls_atomics = cls._enter_atomics()

/usr/local/lib/python3.7/site-packages/django/test/testcases.py:1112: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/local/lib/python3.7/site-packages/django/test/testcases.py:1093: in _enter_atomics
    atomics[db_name].__enter__()
/usr/local/lib/python3.7/site-packages/django/db/transaction.py:175: in __enter__
    if not connection.get_autocommit():
/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py:389: in get_autocommit
    self.ensure_connection()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

args = (<django.db.backends.postgresql.base.DatabaseWrapper object at 0x7f0c4139fdd0>,)
kwargs = {}
event_loop = <_UnixSelectorEventLoop running=True closed=False debug=False>

    @functools.wraps(func)
    def inner(*args, **kwargs):
        if not os.environ.get('DJANGO_ALLOW_ASYNC_UNSAFE'):
            # Detect a running event loop in this thread.
            try:
                event_loop = asyncio.get_event_loop()
            except RuntimeError:
                pass
            else:
                if event_loop.is_running():
>                   raise SynchronousOnlyOperation(message)
E                   django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py:24: SynchronousOnlyOperation
=========================== short test summary info ============================
ERROR apps/src/tests/test_video_upload.py::Entire_Upload::test_mov - django.c...
1 error in 0.19s
valeedmalik commented 3 years ago
python-engineio==3.12.1
python-socketio==4.5.1

as per https://plugins.jetbrains.com/plugin/13264-pycrunch--live-testing/reviews

valeedmalik commented 3 years ago

upon triggering test

INFO Running tests...
INFO collecting 1 tests for run
DEBUG Received task in queue
INFO tests_will_run
INFO notify_clients_about_tests_change
INFO total_tests_to_run 1
INFO process_single_message - handshake
[25] [task_id: afed567f-0fcd-427a-98fa-2ac767d96218] Data received: test-run-task;
INFO process_single_message - test_run_results
INFO process_single_message - timings
INFO process_single_message - close
[25] [task_id: afed567f-0fcd-427a-98fa-2ac767d96218] - The connection to parent pycrunch-engine process lost
WARNING Executing <Handle BaseSelectorEventLoop._read_from_self()> took 0.188 seconds
INFO notify_clients_about_tests_change
gleb-sevruk commented 3 years ago

Hello, thanks for detailed report.

I was able to reproduce the issue using the latest Django (3.1.4)

I will provide more details after investigation.

gleb-sevruk commented 3 years ago

Quick workaround would be setting DJANGO_ALLOW_ASYNC_UNSAFE env variable in .pycrunch-config.yaml, such as:

engine:
    runtime: django
env:
  DJANGO_SETTINGS_MODULE: django31.settings
  DJANGO_ALLOW_ASYNC_UNSAFE: "true"
valeedmalik commented 3 years ago

Thanks that will work my purposes. I appreciate the quick response.

shakori999 commented 2 years ago

I have the same error , but I don't have the file! how can I solve it?

gleb-sevruk commented 2 years ago

@shakori999 this file will be created at the first run of the engine in the root directory of the project.

You might have an old version of package (when configuration file is not created automatically), make sure to update via pip install --upgrade pycrunch-engine

more at: https://pycrunch.com/docs/configuration-file