Тесты Django Channels: Задача x получила будущее y, присоединенное к другому циклу событий
При попытке протестировать потребителя websocket, я получаю эту ошибку:
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/sergei/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
С помощью следующего кода:
class CoreWebsocketTest(TransactionTestCase):
def setUp(self):
self.communicator = WebsocketCommunicator(CoreWebsocketConsumer.as_asgi(),
WEBSOCKET_PATH)
async def test_connection_with_login(self):
connected, _ = await self.communicator.connect()
assert connected
В настоящее время test_connection_with_login не работает с вышеуказанной ошибкой. TransactionTestCase необходим, потому что я хочу делать запросы к базе данных в моем тесте (настройка пользователей для тестирования)
Я использую Django Channels 4.0.0 и, с Django 4.0.5