|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import asyncio |
15 | 16 | import types |
16 | 17 | from unittest import IsolatedAsyncioTestCase, mock |
17 | 18 |
|
@@ -50,10 +51,13 @@ def __init__(self, *args, **kwargs): |
50 | 51 | pass |
51 | 52 |
|
52 | 53 | # pylint: disable=unused-argument, no-self-use |
53 | | - async def execute(self, query, params=None, throw_exception=False): |
| 54 | + async def execute(self, query, params=None, throw_exception=False, delay=0.0): |
54 | 55 | if throw_exception: |
55 | 56 | raise psycopg.Error("Test Exception") |
56 | 57 |
|
| 58 | + if delay: |
| 59 | + await asyncio.sleep(delay) |
| 60 | + |
57 | 61 | # pylint: disable=unused-argument, no-self-use |
58 | 62 | async def executemany(self, query, params=None, throw_exception=False): |
59 | 63 | if throw_exception: |
@@ -492,3 +496,27 @@ async def test_not_recording_async(self): |
492 | 496 | self.assertFalse(mock_span.set_status.called) |
493 | 497 |
|
494 | 498 | PsycopgInstrumentor().uninstrument() |
| 499 | + |
| 500 | + async def test_tracing_is_async(self): |
| 501 | + PsycopgInstrumentor().instrument() |
| 502 | + |
| 503 | + # before this async fix cursor.execute would take 14000 ns, delaying for |
| 504 | + # 100,000ns |
| 505 | + delay = 0.0001 |
| 506 | + |
| 507 | + async def test_async_connection(): |
| 508 | + acnx = await psycopg.AsyncConnection.connect("test") |
| 509 | + async with acnx as cnx: |
| 510 | + async with cnx.cursor() as cursor: |
| 511 | + await cursor.execute("SELECT * FROM test", delay=delay) |
| 512 | + |
| 513 | + await test_async_connection() |
| 514 | + spans_list = self.memory_exporter.get_finished_spans() |
| 515 | + self.assertEqual(len(spans_list), 1) |
| 516 | + span = spans_list[0] |
| 517 | + |
| 518 | + # duration is nanoseconds |
| 519 | + duration = span.end_time - span.start_time |
| 520 | + self.assertGreater(duration, delay * 1e9) |
| 521 | + |
| 522 | + PsycopgInstrumentor().uninstrument() |
0 commit comments