Skip to content

Commit 0d8cd74

Browse files
committed
Adding a regression test for asynccursor bug
Adds a test to check that the async tracer is actually awaiting cursor.execute. On my machine, with the bug present, the duration is 14000ns. With the bug patched the the duration is orders of magnitude larger.
1 parent 71f50a7 commit 0d8cd74

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

instrumentation/opentelemetry-instrumentation-psycopg/tests/test_psycopg_integration.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import asyncio
1516
import types
1617
from unittest import IsolatedAsyncioTestCase, mock
1718

@@ -50,10 +51,15 @@ def __init__(self, *args, **kwargs):
5051
pass
5152

5253
# pylint: disable=unused-argument, no-self-use
53-
async def execute(self, query, params=None, throw_exception=False):
54+
async def execute(
55+
self, query, params=None, throw_exception=False, delay=0.0
56+
):
5457
if throw_exception:
5558
raise psycopg.Error("Test Exception")
5659

60+
if delay:
61+
await asyncio.sleep(delay)
62+
5763
# pylint: disable=unused-argument, no-self-use
5864
async def executemany(self, query, params=None, throw_exception=False):
5965
if throw_exception:
@@ -492,3 +498,27 @@ async def test_not_recording_async(self):
492498
self.assertFalse(mock_span.set_status.called)
493499

494500
PsycopgInstrumentor().uninstrument()
501+
502+
async def test_tracing_is_async(self):
503+
PsycopgInstrumentor().instrument()
504+
505+
# before this async fix cursor.execute would take 14000 ns, delaying for
506+
# 100,000ns
507+
delay = 0.0001
508+
509+
async def test_async_connection():
510+
acnx = await psycopg.AsyncConnection.connect("test")
511+
async with acnx as cnx:
512+
async with cnx.cursor() as cursor:
513+
await cursor.execute("SELECT * FROM test", delay=delay)
514+
515+
await test_async_connection()
516+
spans_list = self.memory_exporter.get_finished_spans()
517+
self.assertEqual(len(spans_list), 1)
518+
span = spans_list[0]
519+
520+
# duration is nanoseconds
521+
duration = span.end_time - span.start_time
522+
self.assertGreater(duration, delay * 1e9)
523+
524+
PsycopgInstrumentor().uninstrument()

0 commit comments

Comments
 (0)