Skip to content

Commit

Permalink
Fix asyncio related warnings in tests (#2335)
Browse files Browse the repository at this point in the history
* aiopg: fix runtime warnings when running tests

Fixes:
RuntimeWarning: coroutine 'MockCursor.execute' was never awaited
    cursor.execute(query)

* aio-pika: no need to return an awaitable in mocked method

No need to return an awaitable since the mocked method awaits
itself.
Fixes:
    RuntimeWarning: coroutine 'sleep' was never awaited

---------

Co-authored-by: Leighton Chen <lechen@microsoft.com>
  • Loading branch information
xrmx and lzchen authored Mar 14, 2024
1 parent 9b3d0b4 commit 3273d8c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ def _test_publish(self, exchange_type: Type[Exchange]):
with mock.patch.object(
PublishDecorator, "_get_publish_span"
) as mock_get_publish_span:
with mock.patch.object(
Exchange, "publish", return_value=asyncio.sleep(0)
) as mock_publish:
with mock.patch.object(Exchange, "publish") as mock_publish:
decorated_publish = PublishDecorator(
self.tracer, exchange
).decorate(mock_publish)
Expand All @@ -101,9 +99,7 @@ def _test_publish_works_with_not_recording_span(self, exchange_type):
mocked_not_recording_span = MagicMock()
mocked_not_recording_span.is_recording.return_value = False
mock_get_publish_span.return_value = mocked_not_recording_span
with mock.patch.object(
Exchange, "publish", return_value=asyncio.sleep(0)
) as mock_publish:
with mock.patch.object(Exchange, "publish") as mock_publish:
with mock.patch(
"opentelemetry.instrumentation.aio_pika.publish_decorator.propagate.inject"
) as mock_inject:
Expand Down Expand Up @@ -158,9 +154,7 @@ def _test_publish(self, exchange_type: Type[Exchange]):
with mock.patch.object(
PublishDecorator, "_get_publish_span"
) as mock_get_publish_span:
with mock.patch.object(
Exchange, "publish", return_value=asyncio.sleep(0)
) as mock_publish:
with mock.patch.object(Exchange, "publish") as mock_publish:
decorated_publish = PublishDecorator(
self.tracer, exchange
).decorate(mock_publish)
Expand All @@ -184,9 +178,7 @@ def _test_publish_works_with_not_recording_span(self, exchange_type):
mocked_not_recording_span = MagicMock()
mocked_not_recording_span.is_recording.return_value = False
mock_get_publish_span.return_value = mocked_not_recording_span
with mock.patch.object(
Exchange, "publish", return_value=asyncio.sleep(0)
) as mock_publish:
with mock.patch.object(Exchange, "publish") as mock_publish:
with mock.patch(
"opentelemetry.instrumentation.aio_pika.publish_decorator.propagate.inject"
) as mock_inject:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_instrumentor_connect(self):
cnx = async_call(aiopg.connect(database="test"))
cursor = async_call(cnx.cursor())
query = "SELECT * FROM test"
cursor.execute(query)
async_call(cursor.execute(query))

spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
Expand Down Expand Up @@ -127,7 +127,7 @@ def test_instrumentor_create_pool(self):
cnx = async_call(pool.acquire())
cursor = async_call(cnx.cursor())
query = "SELECT * FROM test"
cursor.execute(query)
async_call(cursor.execute(query))

spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
Expand Down

0 comments on commit 3273d8c

Please sign in to comment.