| 454 | @pytest.mark.slow |
| 455 | @pytest.mark.timing |
| 456 | async def test_shrink(dsn, monkeypatch): |
| 457 | from psycopg_pool.pool_async import ShrinkPool |
| 458 | |
| 459 | results: list[tuple[int, int]] = [] |
| 460 | |
| 461 | async def run_hacked(self, pool): |
| 462 | n0 = pool._nconns |
| 463 | await orig_run(self, pool) |
| 464 | n1 = pool._nconns |
| 465 | results.append((n0, n1)) |
| 466 | |
| 467 | orig_run = ShrinkPool._run |
| 468 | monkeypatch.setattr(ShrinkPool, "_run", run_hacked) |
| 469 | |
| 470 | async def worker(n): |
| 471 | async with p.connection() as conn: |
| 472 | await conn.execute("select pg_sleep(0.1)") |
| 473 | |
| 474 | async with pool.AsyncConnectionPool(dsn, min_size=2, max_size=4, max_idle=0.2) as p: |
| 475 | await p.wait(5.0) |
| 476 | assert p.max_idle == 0.2 |
| 477 | |
| 478 | ts = [spawn(worker, args=(i,)) for i in range(4)] |
| 479 | await gather(*ts) |
| 480 | |
| 481 | await asleep(1) |
| 482 | |
| 483 | assert results == [(4, 4), (4, 3), (3, 2), (2, 2), (2, 2)] |
| 484 | |
| 485 | |
| 486 | @pytest.mark.slow |