From ff03e486526d93c1c56f743078f4d6854d943f7c Mon Sep 17 00:00:00 2001 From: Adam Liddell Date: Mon, 1 Apr 2019 11:52:35 +0000 Subject: [PATCH 1/4] Make test_pool_on_connect check for multiple calls --- tests/test_pool.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/test_pool.py b/tests/test_pool.py index 06b10f92..7bee848c 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -549,19 +549,25 @@ async def test_close_running_cursor(create_pool): async def test_pool_on_connect(create_pool): - called = False + cb_called_times = 0 async def cb(connection): - nonlocal called + nonlocal cb_called_times async with connection.cursor() as cur: await cur.execute('SELECT 1') data = await cur.fetchall() assert [(1,)] == data - called = True + cb_called_times += 1 - pool = await create_pool(on_connect=cb) + pool = await create_pool( + maxsize=1, + on_connect=cb + ) + + with (await pool.cursor()) as cur: + await cur.execute('SELECT 1') with (await pool.cursor()) as cur: await cur.execute('SELECT 1') - assert called + assert cb_called_times == 1 From e7caea8d6afa0dd309c34ed23a2308082ff9ddfe Mon Sep 17 00:00:00 2001 From: Adam Liddell Date: Mon, 1 Apr 2019 11:56:20 +0000 Subject: [PATCH 2/4] Move on_connect call to after new connection made --- aiopg/pool.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/aiopg/pool.py b/aiopg/pool.py index fc91b1c6..71282b83 100644 --- a/aiopg/pool.py +++ b/aiopg/pool.py @@ -173,8 +173,6 @@ async def _acquire(self): assert not conn.closed, conn assert conn not in self._used, (conn, self._used) self._used.add(conn) - if self._on_connect is not None: - await self._on_connect(conn) return conn else: await self._cond.wait() @@ -203,6 +201,8 @@ async def _fill_free_pool(self, override_min): enable_uuid=self._enable_uuid, echo=self._echo, **self._conn_kwargs) + if self._on_connect is not None: + await self._on_connect(conn) # raise exception if pool is closing self._free.append(conn) self._cond.notify() @@ -221,6 +221,8 @@ async def _fill_free_pool(self, override_min): enable_uuid=self._enable_uuid, echo=self._echo, **self._conn_kwargs) + if self._on_connect is not None: + await self._on_connect(conn) # raise exception if pool is closing self._free.append(conn) self._cond.notify() From de1056eb9675496759ebde7ae1cd18f2edd36cfb Mon Sep 17 00:00:00 2001 From: Adam Liddell Date: Mon, 1 Apr 2019 12:02:29 +0000 Subject: [PATCH 3/4] Update docs to reflect behaviour of on_connect --- docs/core.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core.rst b/docs/core.rst index af7f7012..be59f89a 100644 --- a/docs/core.rst +++ b/docs/core.rst @@ -756,7 +756,7 @@ The basic usage is:: :param bool echo: executed log SQL queryes (disabled by default). - :param on_connect: a *callback coroutine* executed at once for every + :param on_connect: a *callback coroutine* executed once for every created connection. May be used for setting up connection level state like client encoding etc. From a849072281a7bff3a29f70119dd271a1f3a090e5 Mon Sep 17 00:00:00 2001 From: Adam Liddell Date: Mon, 1 Apr 2019 12:23:55 +0000 Subject: [PATCH 4/4] Test pool on_connect with different paths in _fill_free_pool --- tests/test_pool.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_pool.py b/tests/test_pool.py index 7bee848c..59ee10c5 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -548,7 +548,8 @@ async def test_close_running_cursor(create_pool): await cur.execute('SELECT pg_sleep(10)') -async def test_pool_on_connect(create_pool): +@pytest.mark.parametrize('pool_minsize', [0, 1]) +async def test_pool_on_connect(create_pool, pool_minsize): cb_called_times = 0 async def cb(connection): @@ -560,6 +561,7 @@ async def cb(connection): cb_called_times += 1 pool = await create_pool( + minsize=pool_minsize, maxsize=1, on_connect=cb )