|
13 | 13 | # limitations under the License.
|
14 | 14 | import asyncio
|
15 | 15 | import sys
|
| 16 | +from unittest import skipIf |
16 | 17 | from unittest.mock import patch
|
17 | 18 |
|
18 | 19 | # pylint: disable=no-name-in-module
|
@@ -40,34 +41,34 @@ def tearDown(self):
|
40 | 41 | super().tearDown()
|
41 | 42 | AsyncioInstrumentor().uninstrument()
|
42 | 43 |
|
| 44 | + @skipIf( |
| 45 | + sys.version_info < (3, 9), "to_thread is only available in Python 3.9+" |
| 46 | + ) |
43 | 47 | def test_to_thread(self):
|
44 |
| - # to_thread is only available in Python 3.9+ |
45 |
| - if sys.version_info >= (3, 9): |
46 |
| - |
47 |
| - def multiply(x, y): |
48 |
| - return x * y |
| 48 | + def multiply(x, y): |
| 49 | + return x * y |
49 | 50 |
|
50 |
| - async def to_thread(): |
51 |
| - result = await asyncio.to_thread(multiply, 2, 3) |
52 |
| - assert result == 6 |
| 51 | + async def to_thread(): |
| 52 | + result = await asyncio.to_thread(multiply, 2, 3) |
| 53 | + assert result == 6 |
53 | 54 |
|
54 |
| - with self._tracer.start_as_current_span("root"): |
55 |
| - asyncio.run(to_thread()) |
56 |
| - spans = self.memory_exporter.get_finished_spans() |
| 55 | + with self._tracer.start_as_current_span("root"): |
| 56 | + asyncio.run(to_thread()) |
| 57 | + spans = self.memory_exporter.get_finished_spans() |
57 | 58 |
|
58 |
| - self.assertEqual(len(spans), 2) |
59 |
| - assert spans[0].name == "asyncio to_thread-multiply" |
60 |
| - for metric in ( |
61 |
| - self.memory_metrics_reader.get_metrics_data() |
62 |
| - .resource_metrics[0] |
63 |
| - .scope_metrics[0] |
64 |
| - .metrics |
65 |
| - ): |
66 |
| - if metric.name == "asyncio.process.duration": |
67 |
| - for point in metric.data.data_points: |
68 |
| - self.assertEqual(point.attributes["type"], "to_thread") |
69 |
| - self.assertEqual(point.attributes["name"], "multiply") |
70 |
| - if metric.name == "asyncio.process.created": |
71 |
| - for point in metric.data.data_points: |
72 |
| - self.assertEqual(point.attributes["type"], "to_thread") |
73 |
| - self.assertEqual(point.attributes["name"], "multiply") |
| 59 | + self.assertEqual(len(spans), 2) |
| 60 | + assert spans[0].name == "asyncio to_thread-multiply" |
| 61 | + for metric in ( |
| 62 | + self.memory_metrics_reader.get_metrics_data() |
| 63 | + .resource_metrics[0] |
| 64 | + .scope_metrics[0] |
| 65 | + .metrics |
| 66 | + ): |
| 67 | + if metric.name == "asyncio.process.duration": |
| 68 | + for point in metric.data.data_points: |
| 69 | + self.assertEqual(point.attributes["type"], "to_thread") |
| 70 | + self.assertEqual(point.attributes["name"], "multiply") |
| 71 | + if metric.name == "asyncio.process.created": |
| 72 | + for point in metric.data.data_points: |
| 73 | + self.assertEqual(point.attributes["type"], "to_thread") |
| 74 | + self.assertEqual(point.attributes["name"], "multiply") |
0 commit comments