Skip to content

Fix unique ID generation #775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions asyncpg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@


import asyncio
import uuid

import asyncpg
import collections
import collections.abc
Expand Down Expand Up @@ -1344,9 +1346,10 @@ def _check_open(self):
raise exceptions.InterfaceError('connection is closed')

def _get_unique_id(self, prefix):
global _uid
_uid += 1
return '__asyncpg_{}_{:x}__'.format(prefix, _uid)
return '__asyncpg_{prefix}_{uuid}__'.format(
prefix=prefix,
uuid=uuid.uuid4(),
)

def _mark_stmts_as_closed(self):
for stmt in self._stmt_cache.iter_statements():
Expand Down Expand Up @@ -2258,6 +2261,3 @@ def _check_record_class(record_class):
'record_class is expected to be a subclass of '
'asyncpg.Record, got {!r}'.format(record_class)
)


_uid = 0
26 changes: 16 additions & 10 deletions tests/test_introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import asyncio
import json
import unittest.mock
import uuid

from asyncpg import _testbase as tb
from asyncpg import connection as apg_con
Expand Down Expand Up @@ -72,10 +74,9 @@ async def test_introspection_on_large_db(self):
with self.assertRunUnder(MAX_RUNTIME):
await self.con.fetchval('SELECT $1::int[]', [1, 2])

@unittest.mock.patch.object(apg_con.Connection, '_get_unique_id')
@tb.with_connection_options(statement_cache_size=0)
async def test_introspection_no_stmt_cache_01(self):
old_uid = apg_con._uid

async def test_introspection_no_stmt_cache_01(self, mocked_get_unique_id):
self.assertEqual(self.con._stmt_cache.get_max_size(), 0)
await self.con.fetchval('SELECT $1::int[]', [1, 2])

Expand All @@ -91,13 +92,13 @@ async def test_introspection_no_stmt_cache_01(self):
DROP EXTENSION hstore
''')

self.assertEqual(apg_con._uid, old_uid)
mocked_get_unique_id.assert_not_called()

@unittest.mock.patch.object(apg_con.Connection, '_get_unique_id')
@tb.with_connection_options(max_cacheable_statement_size=1)
async def test_introspection_no_stmt_cache_02(self):
async def test_introspection_no_stmt_cache_02(self, mocked_get_unique_id):
# max_cacheable_statement_size will disable caching both for
# the user query and for the introspection query.
old_uid = apg_con._uid

await self.con.fetchval('SELECT $1::int[]', [1, 2])

Expand All @@ -113,18 +114,23 @@ async def test_introspection_no_stmt_cache_02(self):
DROP EXTENSION hstore
''')

self.assertEqual(apg_con._uid, old_uid)
mocked_get_unique_id.assert_not_called()

@unittest.mock.patch.object(apg_con.Connection, '_get_unique_id', return_value='uuid')
@tb.with_connection_options(max_cacheable_statement_size=10000)
async def test_introspection_no_stmt_cache_03(self):
async def test_introspection_no_stmt_cache_03(self, mocked_get_unique_id):
# max_cacheable_statement_size will disable caching for
# the user query but not for the introspection query.
old_uid = apg_con._uid

await self.con.fetchval(
"SELECT $1::int[], '{foo}'".format(foo='a' * 10000), [1, 2])

self.assertEqual(apg_con._uid, old_uid + 1)
mocked_get_unique_id.assert_called_once()

def test_introspection_get_unique_id(self):
result = self.con._get_unique_id('prefix')
pattern = r'__asyncpg_prefix_[0-9a-f\-]{36}__'
self.assertRegexpMatches(result, pattern)

async def test_introspection_sticks_for_ps(self):
# Test that the introspected codec pipeline for a prepared
Expand Down