MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / test_pool_07

Method test_pool_07

tests/test_pool.py:155–213  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

153 self.assertIs(con, await fut)
154
155 async def test_pool_07(self):
156 cons = set()
157 connect_called = 0
158 init_called = 0
159 setup_called = 0
160 reset_called = 0
161
162 async def connect(*args, **kwargs):
163 nonlocal connect_called
164 connect_called += 1
165 return await pg_connection.connect(*args, **kwargs)
166
167 async def setup(con):
168 nonlocal setup_called
169 if con._con not in cons: # `con` is `PoolConnectionProxy`.
170 raise RuntimeError('init was not called before setup')
171 setup_called += 1
172
173 async def init(con):
174 nonlocal init_called
175 if con in cons:
176 raise RuntimeError('init was called more than once')
177 cons.add(con)
178 init_called += 1
179
180 async def reset(con):
181 nonlocal reset_called
182 reset_called += 1
183
184 async def user(pool):
185 async with pool.acquire() as con:
186 if con._con not in cons: # `con` is `PoolConnectionProxy`.
187 raise RuntimeError('init was not called')
188
189 async with self.create_pool(database='postgres',
190 init_size=2,
191 min_size=1, max_size=5,
192 connect=connect,
193 init=init,
194 setup=setup,
195 reset=reset) as pool:
196 users = asyncio.gather(*[user(pool) for _ in range(10)])
197 await users
198
199 self.assertEqual(len(cons), 5)
200 self.assertEqual(connect_called, 5)
201 self.assertEqual(init_called, 5)
202 self.assertEqual(setup_called, 10)
203 self.assertEqual(reset_called, 10)
204
205 async def bad_connect(*args, **kwargs):
206 return 1
207
208 with self.assertRaisesRegex(
209 asyncpg.InterfaceError,
210 "expected pool connect callback to return an instance of "
211 "'asyncpg\\.connection\\.Connection', got 'int'"
212 ):

Callers

nothing calls this directly

Calls 1

create_poolMethod · 0.45

Tested by

no test coverage detected