Code Monkey home page Code Monkey logo

Comments (5)

jlariza avatar jlariza commented on September 27, 2024

I created this repository to reproduce the issue https://github.com/jlariza/channels_testing_bug using cookiecutter

To test:

  • create a venv using python 3.11
  • run pip install -r requirements/local.txt to install dependencies
  • create a postgres database called channelstestingbug
  • run migrations
  • run pytest channels_testing_bug/myapp
  • You'll get the django.db.utils.OperationalError: the connection is closed error in channels_testing_bug/myapp/tests.py:20 in create_instance call.
  • If you change it to create the model in the setUp call, the error will be raised by channels_testing_bug/myapp/consumers.py:14: when it tries to filter the database.

The consumer works; I already tested it in another project. However, the tests fail because they cannot handle the async connection to the database.

Any ideas of what I'm missing or doing incorrectly?

Thank you,

from channels.

carltongibson avatar carltongibson commented on September 27, 2024

I suspect this is about how pytest manages the connection between tests.

I'd try inclining the model creation in the test case, to see if that makes progress and then adjust out from there.

from channels.

jlariza avatar jlariza commented on September 27, 2024

@carltongibson no luck. If you try to run the model creation in the test case directly, it raises django.core.exceptions.SynchronousOnlyOperation but if you use the database_sync_to_asyncdecorator, the OperationalError is raised again.

I tried creating the model using acreate in the testcase but then the OperationalError is raised in this line if MyModel.objects.filter(id=self.object_id).exists(): again.

from channels.

carltongibson avatar carltongibson commented on September 27, 2024

it raises django.core.exceptions.SynchronousOnlyOperation

Yes, you'd need to wrap it in sync_to_async().

The idea is to reduce the number of different context so you can identify when the DB connection is being closed.

from channels.

carltongibson avatar carltongibson commented on September 27, 2024

MyModel.objects.filter(id=self.object_id).exists()

This will be because of the transaction test case usage. From the other transaction the object isn't yet saved, I'd guess.

from channels.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.