From 9cb2c1ce044768ef7c396976b8abf896fad901f7 Mon Sep 17 00:00:00 2001 From: David Baumgold Date: Fri, 2 Dec 2022 23:41:35 -0600 Subject: [PATCH] Add Pool.is_closing() method (#973) --- asyncpg/pool.py | 7 +++++++ tests/test_pool.py | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/asyncpg/pool.py b/asyncpg/pool.py index 9bd2a3e3..eaf501f4 100644 --- a/asyncpg/pool.py +++ b/asyncpg/pool.py @@ -446,6 +446,13 @@ async def _initialize(self): await asyncio.gather(*connect_tasks) + def is_closing(self): + """Return ``True`` if the pool is closing or is closed. + + .. versionadded:: 0.28.0 + """ + return self._closed or self._closing + def get_size(self): """Return the current number of connections in this pool. diff --git a/tests/test_pool.py b/tests/test_pool.py index b77783e9..5577632c 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -740,6 +740,17 @@ async def test_pool_size_and_capacity(self): self.assertEqual(pool.get_size(), 3) self.assertEqual(pool.get_idle_size(), 0) + async def test_pool_closing(self): + async with self.create_pool() as pool: + self.assertFalse(pool.is_closing()) + await pool.close() + self.assertTrue(pool.is_closing()) + + async with self.create_pool() as pool: + self.assertFalse(pool.is_closing()) + pool.terminate() + self.assertTrue(pool.is_closing()) + async def test_pool_handles_transaction_exit_in_asyncgen_1(self): pool = await self.create_pool(database='postgres', min_size=1, max_size=1)