Skip to content

Commit 7f8a846

Browse files
authored
Fix on_connect multiple call on acquire (#552)
* Make test_pool_on_connect check for multiple calls * Move on_connect call to after new connection made * Update docs to reflect behaviour of on_connect * Test pool on_connect with different paths in _fill_free_pool
1 parent 047dee8 commit 7f8a846

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

aiopg/pool.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ async def _acquire(self):
167167
assert not conn.closed, conn
168168
assert conn not in self._used, (conn, self._used)
169169
self._used.add(conn)
170-
if self._on_connect is not None:
171-
await self._on_connect(conn)
172170
return conn
173171
else:
174172
await self._cond.wait()
@@ -197,6 +195,8 @@ async def _fill_free_pool(self, override_min):
197195
enable_uuid=self._enable_uuid,
198196
echo=self._echo,
199197
**self._conn_kwargs)
198+
if self._on_connect is not None:
199+
await self._on_connect(conn)
200200
# raise exception if pool is closing
201201
self._free.append(conn)
202202
self._cond.notify()
@@ -215,6 +215,8 @@ async def _fill_free_pool(self, override_min):
215215
enable_uuid=self._enable_uuid,
216216
echo=self._echo,
217217
**self._conn_kwargs)
218+
if self._on_connect is not None:
219+
await self._on_connect(conn)
218220
# raise exception if pool is closing
219221
self._free.append(conn)
220222
self._cond.notify()

docs/core.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ The basic usage is::
761761

762762
:param bool echo: executed log SQL queryes (disabled by default).
763763

764-
:param on_connect: a *callback coroutine* executed at once for every
764+
:param on_connect: a *callback coroutine* executed once for every
765765
created connection. May be used for setting up connection level
766766
state like client encoding etc.
767767

tests/test_pool.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,20 +546,28 @@ async def test_close_running_cursor(create_pool):
546546
await cur.execute('SELECT pg_sleep(10)')
547547

548548

549-
async def test_pool_on_connect(create_pool):
550-
called = False
549+
@pytest.mark.parametrize('pool_minsize', [0, 1])
550+
async def test_pool_on_connect(create_pool, pool_minsize):
551+
cb_called_times = 0
551552

552553
async def cb(connection):
553-
nonlocal called
554+
nonlocal cb_called_times
554555
async with connection.cursor() as cur:
555556
await cur.execute('SELECT 1')
556557
data = await cur.fetchall()
557558
assert [(1,)] == data
558-
called = True
559+
cb_called_times += 1
559560

560-
pool = await create_pool(on_connect=cb)
561+
pool = await create_pool(
562+
minsize=pool_minsize,
563+
maxsize=1,
564+
on_connect=cb
565+
)
566+
567+
with (await pool.cursor()) as cur:
568+
await cur.execute('SELECT 1')
561569

562570
with (await pool.cursor()) as cur:
563571
await cur.execute('SELECT 1')
564572

565-
assert called
573+
assert cb_called_times == 1

0 commit comments

Comments
 (0)