You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi everyone, I'm posting this issue to talk about the WebsocketCommunicator class. I'm unable to get it to work using the documentation provided. I'm unsure if this is just a gap in the documentation, or actually a bug. At least it should be an actionable problem. I am expecting the first test's .connect() to fail in the caught way (not quite sure if TimeoutError is the correct exception though), and the second to just succeed, as the docs suggest.
consumers.py:
from<mydjangoapp>.asgiimportapplicationWEBSOCKET_PATH="api/ws"classCoreWebsocketTest(TestCase):
defsetUp(self):
self.communicator=WebsocketCommunicator(application,
WEBSOCKET_PATH)
asyncdeftest_connection_no_login(self):
try:
awaitself.communicator.connect()
exceptTimeoutError:
passelse:
raiseException("Client connected with no auth.")
asyncdeftest_connection(self):
connected, _=awaitself.communicator.connect()
self.assertTrue(connected)
(I understand that it's not actually logging in right now to actually test that stuff. I wanted to make it as stripped down as possible to test)
I've tried versions v3.0.5 and v4.0.0, both suffered from the same issue. I think I narrowed the problem down to line 36 of channels.testing.websocket:
asyncdefconnect(self, timeout=1):
""" Trigger the connection code. On an accepted connection, returns (True, <chosen-subprotocol>) On a rejected connection, returns (False, <close-code>) """awaitself.send_input({"type": "websocket.connect"})
response=awaitself.receive_output(timeout) # <-----ifresponse["type"] =="websocket.close":
return (False, response.get("code", 1000))
else:
return (True, response.get("subprotocol", None))
This also suggests the issue is inside asgiref, as receive_output is from asgiref. Please let me know if that's a better place to post this :).
Error
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/asgiref/testing.py", line 74, in receive_output
return await self.output_queue.get()
File "/usr/local/lib/python3.10/asyncio/queues.py", line 159, in get
await getter
asyncio.exceptions.CancelledError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/asgiref/testing.py", line 73, in receive_output
async with async_timeout(timeout):
File "/usr/local/lib/python3.10/site-packages/asgiref/timeout.py", line 65, in __aexit__
self._do_exit(exc_type)
File "/usr/local/lib/python3.10/site-packages/asgiref/timeout.py", line 102, in _do_exit
raise asyncio.TimeoutError
asyncio.exceptions.TimeoutError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/asgiref/sync.py", line 218, in __call__
return call_result.result()
File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 439, in result
return self.__get_result()
File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 391, in __get_result
raise self._exception
File "/usr/local/lib/python3.10/site-packages/asgiref/sync.py", line 284, in main_wrap
result = await self.awaitable(*args, **kwargs)
File "/opt/project/backend/src/<my django app>/websocket/tests.py", line 57, in test_connection_with_login
connected, _ = await self.communicator.connect()
File "/usr/local/lib/python3.10/site-packages/channels/testing/websocket.py", line 36, in connect
response = await self.receive_output(timeout)
File "/usr/local/lib/python3.10/site-packages/asgiref/testing.py", line 82, in receive_output
await self.future
RuntimeError: Task <Task pending name='Task-14' coro=<AsyncToSync.main_wrap() running at /usr/local/lib/python3.10/site-packages/asgiref/sync.py:284> cb=[_run_until_complete_cb() at /usr/local/lib/python3.10/asyncio/base_events.py:184]> got Future <Task cancelling name='Task-13' coro=<CoreWebsocketConsumer() running at /usr/local/lib/python3.10/site-packages/channels/consumer.py:92>> attached to a different loop
The problem is that you created communicator() in sync setUp() method and tried to reuse it in async test.
The communicator inits its input and output Queue with one event loop but the async tests are being executed in the another one. Hope it helps someone.
How would I fix this? I still want to use my setUp method to create the communicator, as putting it in every function would not be that elegant. I'm not able to make my setup async, right?
How would I fix this? I still want to use my setUp method to create the communicator, as putting it in every function would not be that elegant. I'm not able to make my setup async, right?
The only worked solution for me was to init it in smth like async def _create_communicator() and use explicitly in the beginning of each test.
For example, I tried to use async_to_sync(_init_communicator_in_setUp)() right in setUp() but was out of luck with the other errors.
Hi everyone, I'm posting this issue to talk about the WebsocketCommunicator class. I'm unable to get it to work using the documentation provided. I'm unsure if this is just a gap in the documentation, or actually a bug. At least it should be an actionable problem. I am expecting the first test's
.connect()
to fail in the caught way (not quite sure ifTimeoutError
is the correct exception though), and the second to just succeed, as the docs suggest.consumers.py:
(I understand that it's not actually logging in right now to actually test that stuff. I wanted to make it as stripped down as possible to test)
routing.py:
I've tried versions v3.0.5 and v4.0.0, both suffered from the same issue. I think I narrowed the problem down to line 36 of
channels.testing.websocket
:This also suggests the issue is inside asgiref, as receive_output is from asgiref. Please let me know if that's a better place to post this :).
asgi.py
I get this error:
pip freeze
output:The text was updated successfully, but these errors were encountered: