|
8 | 8 | import asyncio
|
9 | 9 | import asyncpg
|
10 | 10 | import inspect
|
11 |
| -import platform |
12 | 11 | import os
|
| 12 | +import platform |
| 13 | +import random |
13 | 14 | import unittest
|
14 | 15 |
|
15 | 16 | from asyncpg import _testbase as tb
|
@@ -378,6 +379,84 @@ async def get_xact_id(con):
|
378 | 379 | id3 = await get_xact_id(con)
|
379 | 380 | self.assertNotEqual(id2, id3)
|
380 | 381 |
|
| 382 | + async def test_pool_connection_methods(self): |
| 383 | + async def test_fetch(pool): |
| 384 | + i = random.randint(0, 20) |
| 385 | + await asyncio.sleep(random.random() / 100) |
| 386 | + r = await pool.fetch('SELECT {}::int'.format(i)) |
| 387 | + self.assertEqual(r, [(i,)]) |
| 388 | + return 1 |
| 389 | + |
| 390 | + async def test_fetchrow(pool): |
| 391 | + i = random.randint(0, 20) |
| 392 | + await asyncio.sleep(random.random() / 100) |
| 393 | + r = await pool.fetchrow('SELECT {}::int'.format(i)) |
| 394 | + self.assertEqual(r, (i,)) |
| 395 | + return 1 |
| 396 | + |
| 397 | + async def test_fetchval(pool): |
| 398 | + i = random.randint(0, 20) |
| 399 | + await asyncio.sleep(random.random() / 100) |
| 400 | + r = await pool.fetchval('SELECT {}::int'.format(i)) |
| 401 | + self.assertEqual(r, i) |
| 402 | + return 1 |
| 403 | + |
| 404 | + async def test_execute(pool): |
| 405 | + await asyncio.sleep(random.random() / 100) |
| 406 | + r = await pool.execute('SELECT generate_series(0, 10)') |
| 407 | + self.assertEqual(r, 'SELECT {}'.format(11)) |
| 408 | + return 1 |
| 409 | + |
| 410 | + async def test_execute_with_arg(pool): |
| 411 | + i = random.randint(0, 20) |
| 412 | + await asyncio.sleep(random.random() / 100) |
| 413 | + r = await pool.execute('SELECT generate_series(0, $1)', i) |
| 414 | + self.assertEqual(r, 'SELECT {}'.format(i + 1)) |
| 415 | + return 1 |
| 416 | + |
| 417 | + async def run(N, meth): |
| 418 | + async with self.create_pool(database='postgres', |
| 419 | + min_size=5, max_size=10) as pool: |
| 420 | + |
| 421 | + coros = [meth(pool) for _ in range(N)] |
| 422 | + res = await asyncio.gather(*coros, loop=self.loop) |
| 423 | + self.assertEqual(res, [1] * N) |
| 424 | + |
| 425 | + methods = [test_fetch, test_fetchrow, test_fetchval, |
| 426 | + test_execute, test_execute_with_arg] |
| 427 | + |
| 428 | + for method in methods: |
| 429 | + with self.subTest(method=method.__name__): |
| 430 | + await run(200, method) |
| 431 | + |
| 432 | + async def test_pool_connection_execute_many(self): |
| 433 | + async def worker(pool): |
| 434 | + await asyncio.sleep(random.random() / 100) |
| 435 | + await pool.executemany(''' |
| 436 | + INSERT INTO exmany VALUES($1, $2) |
| 437 | + ''', [ |
| 438 | + ('a', 1), ('b', 2), ('c', 3), ('d', 4) |
| 439 | + ]) |
| 440 | + return 1 |
| 441 | + |
| 442 | + N = 200 |
| 443 | + |
| 444 | + async with self.create_pool(database='postgres', |
| 445 | + min_size=5, max_size=10) as pool: |
| 446 | + |
| 447 | + await pool.execute('CREATE TABLE exmany (a text, b int)') |
| 448 | + try: |
| 449 | + |
| 450 | + coros = [worker(pool) for _ in range(N)] |
| 451 | + res = await asyncio.gather(*coros, loop=self.loop) |
| 452 | + self.assertEqual(res, [1] * N) |
| 453 | + |
| 454 | + n_rows = await pool.fetchval('SELECT count(*) FROM exmany') |
| 455 | + self.assertEqual(n_rows, N * 4) |
| 456 | + |
| 457 | + finally: |
| 458 | + await pool.execute('DROP TABLE exmany') |
| 459 | + |
381 | 460 |
|
382 | 461 | @unittest.skipIf(os.environ.get('PGHOST'), 'using remote cluster for testing')
|
383 | 462 | class TestHostStandby(tb.ConnectedTestCase):
|
|
0 commit comments