Skip to content

Commit

Permalink
Fixed now awaited coroutine warnings
Browse files Browse the repository at this point in the history
Made it returning tasks from startup and awaiting it on test
  • Loading branch information
timotta committed Feb 17, 2023
1 parent 06377aa commit 4e79ec6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
13 changes: 10 additions & 3 deletions asyncworker/signals/handlers/rabbitmq.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, List
from asyncio import Task

from asyncworker.connections import AMQPConnection
from asyncworker.consumer import Consumer
Expand All @@ -10,7 +11,9 @@


class RabbitMQ(SignalHandler):
async def startup(self, app: "App"):
async def startup(self, app: "App") -> List[Task]:
tasks = []

app[RouteTypes.AMQP_RABBITMQ]["consumers"] = []
for route_info in app.routes_registry.amqp_routes:
conn: AMQPConnection = app.get_connection_for_route(route_info)
Expand All @@ -24,4 +27,8 @@ async def startup(self, app: "App"):
)
app[RouteTypes.AMQP_RABBITMQ]["consumers"].append(consumer)
conn.register(consumer.queue)
app.loop.create_task(consumer.start())
task = app.loop.create_task(consumer.start())

tasks.append(task)

return tasks
10 changes: 7 additions & 3 deletions tests/rabbitmq/test_rabbitmq_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,12 @@ async def test_start_always_calls_sleep(self):
) as sleep_mock, patch.object(
consumer, "clock_task", side_effect=[True, True]
):
queue_mock = AsyncMock(
consume=AsyncMock(), connect=AsyncMock()
queue_mock = Mock(
consume=AsyncMock(),
connection=Mock(
connect=AsyncMock(),
has_channel_ready=Mock(),
),
)
consumer.queue = queue_mock

Expand Down Expand Up @@ -682,7 +686,7 @@ async def test_start_create_clock_flusher(self):
with patch.object(
consumer, "keep_runnig", side_effect=[True, True, True, False]
):
queue_mock = AsyncMock(
queue_mock = Mock(
consume=AsyncMock(),
connection=Mock(
connect=AsyncMock(side_effect=[AioamqpException, True]),
Expand Down
5 changes: 4 additions & 1 deletion tests/signals/handlers/test_rabbitmq.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, Mock, patch, call, ANY

Expand Down Expand Up @@ -52,7 +53,7 @@ async def test_startup_initializes_and_starts_one_consumer_per_route(
# )

app[RouteTypes.AMQP_RABBITMQ] = {"connections": [connection]}
await self.signal_handler.startup(app)
tasks = await self.signal_handler.startup(app)

Consumer.assert_has_calls(
[
Expand All @@ -79,6 +80,8 @@ async def test_startup_initializes_and_starts_one_consumer_per_route(
[Consumer.return_value, Consumer.return_value],
)

await asyncio.gather(*tasks)

@patch(
"asyncworker.signals.handlers.rabbitmq.AMQPConnection.register"
)
Expand Down

0 comments on commit 4e79ec6

Please sign in to comment.