Re: Channels: Test case fails on async.sleep(X>0)

2018-11-13 Thread Andrew Godwin
Couple of points:

- Did you install pytest-django? That should help the settings thing.
- There's a default 1-second timeout on get_response; pass timeout=4 to it
and you should see that error go away.

We have a default timeout because otherwise we can't tell if an async app
has died/crashed and it'd hang forever!

Andrew

On Mon, Nov 12, 2018 at 3:43 PM Zhiyu (Drew) Li  wrote:

> Hi there,
>
> I was trying to write a test for a consumer. It is just a very simply
> AsyncHttpConsumer subclass that awaits on asyncio.sleep(3) and returns a
> "OK" in plain text.
>
> The test case is:
> @pytest.mark.asyncio
> async def test_my_consumer():
> communicator = HttpCommunicator(BasicHttpConsumer, "GET", "asynchttp")
> response = await communicator.get_response()
> assert response["status"] == 200
> assert response["body"] == b"OK"
>
> 1) Error 1:
> I run it with command "pytest .py"
> The first error I got was:
> django.core.exceptions.ImproperlyConfigured: Requested settings, but
> settings are not configured. You must either define the environment
> variable DJANGO_SETTINGS_MODULE or call settings.configure() before
> accessing settings.
>
> I google online and found this fix:
> os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MODULE_PATH_TO.settings")
>
>
> 2) Error 2:
> The new error I am getting is:
> self = , exc_type =  'concurrent.futures._base.CancelledError'>
>
> def _do_exit(self, exc_type: Type[BaseException]) -> None:
> if exc_type is asyncio.CancelledError and self._cancelled:
> self._cancel_handler = None
> self._task = None
> >   raise asyncio.TimeoutError
> E   concurrent.futures._base.TimeoutError
>
> But If I change async.sleep(3) to  async.sleep(0) the test passed. Not
> sure why
>
> Also how to test url mapping works correctly?
>
> Thanks
> Drew
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMmsbUk-3AemjcTxxidf4GBvKeWG7b72qwfUcs5owReTeOEMiw%40mail.gmail.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uq02V6p53P84z0kJo13jrVWDtAr7_qoZ69L1cntQXww6w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Channels: Test case fails on async.sleep(X>0)

2018-11-12 Thread Zhiyu (Drew) Li
Hi there,

I was trying to write a test for a consumer. It is just a very simply
AsyncHttpConsumer subclass that awaits on asyncio.sleep(3) and returns a
"OK" in plain text.

The test case is:
@pytest.mark.asyncio
async def test_my_consumer():
communicator = HttpCommunicator(BasicHttpConsumer, "GET", "asynchttp")
response = await communicator.get_response()
assert response["status"] == 200
assert response["body"] == b"OK"

1) Error 1:
I run it with command "pytest .py"
The first error I got was:
django.core.exceptions.ImproperlyConfigured: Requested settings, but
settings are not configured. You must either define the environment
variable DJANGO_SETTINGS_MODULE or call settings.configure() before
accessing settings.

I google online and found this fix:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MODULE_PATH_TO.settings")


2) Error 2:
The new error I am getting is:
self = , exc_type = 

def _do_exit(self, exc_type: Type[BaseException]) -> None:
if exc_type is asyncio.CancelledError and self._cancelled:
self._cancel_handler = None
self._task = None
>   raise asyncio.TimeoutError
E   concurrent.futures._base.TimeoutError

But If I change async.sleep(3) to  async.sleep(0) the test passed. Not sure
why

Also how to test url mapping works correctly?

Thanks
Drew

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAMmsbUk-3AemjcTxxidf4GBvKeWG7b72qwfUcs5owReTeOEMiw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.