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

Method test_pool_07

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

Source from the content-addressed store, hash-verified

135 self.assertIs(con, await fut)
136
137 async def test_pool_07(self):
138 cons = set()
139 connect_called = 0
140 init_called = 0
141 setup_called = 0
142 reset_called = 0
143
144 async def connect(*args, **kwargs):
145 nonlocal connect_called
146 connect_called += 1
147 return await pg_connection.connect(*args, **kwargs)
148
149 async def setup(con):
150 nonlocal setup_called
151 if con._con not in cons: # `con` is `PoolConnectionProxy`.
152 raise RuntimeError('init was not called before setup')
153 setup_called += 1
154
155 async def init(con):
156 nonlocal init_called
157 if con in cons:
158 raise RuntimeError('init was called more than once')
159 cons.add(con)
160 init_called += 1
161
162 async def reset(con):
163 nonlocal reset_called
164 reset_called += 1
165
166 async def user(pool):
167 async with pool.acquire() as con:
168 if con._con not in cons: # `con` is `PoolConnectionProxy`.
169 raise RuntimeError('init was not called')
170
171 async with self.create_pool(database='postgres',
172 min_size=2,
173 max_size=5,
174 connect=connect,
175 init=init,
176 setup=setup,
177 reset=reset) as pool:
178 users = asyncio.gather(*[user(pool) for _ in range(10)])
179 await users
180
181 self.assertEqual(len(cons), 5)
182 self.assertEqual(connect_called, 5)
183 self.assertEqual(init_called, 5)
184 self.assertEqual(setup_called, 10)
185 self.assertEqual(reset_called, 10)
186
187 async def bad_connect(*args, **kwargs):
188 return 1
189
190 with self.assertRaisesRegex(
191 asyncpg.InterfaceError,
192 "expected pool connect callback to return an instance of "
193 "'asyncpg\\.connection\\.Connection', got 'int'"
194 ):

Callers

nothing calls this directly

Calls 1

create_poolMethod · 0.45

Tested by

no test coverage detected