Skip to content

Commit 2fcd49d

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 2fcd49d

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

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

Lines changed: 29 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,13 @@ 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(self, query, params=None, throw_exception=False, delay=0.0):
5455
if throw_exception:
5556
raise psycopg.Error("Test Exception")
5657

58+
if delay:
59+
await asyncio.sleep(delay)
60+
5761
# pylint: disable=unused-argument, no-self-use
5862
async def executemany(self, query, params=None, throw_exception=False):
5963
if throw_exception:
@@ -492,3 +496,27 @@ async def test_not_recording_async(self):
492496
self.assertFalse(mock_span.set_status.called)
493497

494498
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

Comments
 (0)