Skip to content

Commit 9cce290

Browse files
committed
modify test_code
1 parent 15c0375 commit 9cce290

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

instrumentation/opentelemetry-instrumentation-asyncio/tests/test_asyncio_ensure_future.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from unittest.mock import patch
1616

1717
import pytest
18-
from opentelemetry.sdk.metrics._internal.point import HistogramDataPoint, NumberDataPoint
1918
from opentelemetry.test.test_base import TestBase
2019
from opentelemetry.trace import get_tracer
2120

@@ -54,28 +53,39 @@ def tearDown(self):
5453
AsyncioInstrumentor().uninstrument()
5554

5655
@pytest.mark.asyncio
57-
async def test_asyncio_loop_ensure_future(self):
56+
def test_asyncio_loop_ensure_future(self):
5857
"""
5958
async_func is not traced because it is not set in the environment variable
6059
"""
61-
task = asyncio.ensure_future(async_func())
62-
await task
60+
61+
async def test():
62+
task = asyncio.ensure_future(async_func())
63+
await task
64+
65+
asyncio.run(test())
6366

6467
spans = self.memory_exporter.get_finished_spans()
6568
self.assertEqual(len(spans), 0)
6669

6770
@pytest.mark.asyncio
68-
async def test_asyncio_ensure_future_with_future(self):
69-
with self._tracer.start_as_current_span("root") as root:
71+
def test_asyncio_ensure_future_with_future(self):
72+
async def test():
73+
with self._tracer.start_as_current_span("root") as root:
74+
future = asyncio.Future()
75+
future.set_result(1)
76+
task = asyncio.ensure_future(future)
77+
await task
7078

71-
future = asyncio.Future()
72-
future.set_result(1)
73-
task = asyncio.ensure_future(future)
74-
await task
79+
asyncio.run(test())
7580

7681
spans = self.memory_exporter.get_finished_spans()
7782
self.assertEqual(len(spans), 2)
78-
self.assertEqual(spans[0].name, "asyncio.future")
83+
for span in spans:
84+
if span.name == "root":
85+
self.assertEqual(span.parent, None)
86+
if span.name == "asyncio.future":
87+
self.assertNotEquals(span.parent.trace_id, 0)
88+
7989
for metric in self.memory_metrics_reader.get_metrics_data().resource_metrics[0].scope_metrics[0].metrics:
8090
if metric.name == ASYNCIO_FUTURES_DURATION:
8191
self.assertEquals(metric.data.data_points[0].count, 1)

0 commit comments

Comments
 (0)