forked from MagicStack/asyncpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pool.py
150 lines (110 loc) · 4.9 KB
/
test_pool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Copyright (C) 2016-present the ayncpg authors and contributors
# <see AUTHORS file>
#
# This module is part of asyncpg and is released under
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
import asyncio
import platform
from asyncpg import _testbase as tb
_system = platform.uname().system
class TestPool(tb.ConnectedTestCase):
async def test_pool_01(self):
for n in {1, 3, 5, 10, 20, 100}:
with self.subTest(tasksnum=n):
pool = await self.create_pool(database='postgres',
min_size=5, max_size=10)
async def worker():
con = await pool.acquire()
self.assertEqual(await con.fetchval('SELECT 1'), 1)
await pool.release(con)
tasks = [worker() for _ in range(n)]
await asyncio.gather(*tasks, loop=self.loop)
await pool.close()
async def test_pool_02(self):
for n in {1, 3, 5, 10, 20, 100}:
with self.subTest(tasksnum=n):
async with self.create_pool(database='postgres',
min_size=5, max_size=5) as pool:
async def worker():
con = await pool.acquire(timeout=1)
self.assertEqual(await con.fetchval('SELECT 1'), 1)
await pool.release(con)
tasks = [worker() for _ in range(n)]
await asyncio.gather(*tasks, loop=self.loop)
async def test_pool_03(self):
pool = await self.create_pool(database='postgres',
min_size=1, max_size=1)
con = await pool.acquire(timeout=1)
with self.assertRaises(asyncio.TimeoutError):
await pool.acquire(timeout=0.03)
pool.terminate()
del con
async def test_pool_04(self):
pool = await self.create_pool(database='postgres',
min_size=1, max_size=1)
con = await pool.acquire(timeout=0.1)
con.terminate()
await pool.release(con)
async with pool.acquire(timeout=0.1):
con.terminate()
con = await pool.acquire(timeout=0.1)
self.assertEqual(await con.fetchval('SELECT 1'), 1)
await pool.close()
async def test_pool_05(self):
for n in {1, 3, 5, 10, 20, 100}:
with self.subTest(tasksnum=n):
pool = await self.create_pool(database='postgres',
min_size=5, max_size=10)
async def worker():
async with pool.acquire() as con:
self.assertEqual(await con.fetchval('SELECT 1'), 1)
tasks = [worker() for _ in range(n)]
await asyncio.gather(*tasks, loop=self.loop)
await pool.close()
async def test_pool_06(self):
fut = asyncio.Future(loop=self.loop)
async def setup(con):
fut.set_result(con)
async with self.create_pool(database='postgres',
min_size=5, max_size=5,
setup=setup) as pool:
con = await pool.acquire()
self.assertIs(con, await fut)
async def test_pool_auth(self):
if not self.cluster.is_managed():
self.skipTest('unmanaged cluster')
self.cluster.reset_hba()
if _system != 'Windows':
self.cluster.add_hba_entry(
type='local',
database='postgres', user='pooluser',
auth_method='md5')
self.cluster.add_hba_entry(
type='host', address='127.0.0.1/32',
database='postgres', user='pooluser',
auth_method='md5')
self.cluster.add_hba_entry(
type='host', address='::1/128',
database='postgres', user='pooluser',
auth_method='md5')
self.cluster.reload()
try:
await self.con.execute('''
CREATE ROLE pooluser WITH LOGIN PASSWORD 'poolpassword'
''')
pool = await self.create_pool(database='postgres',
user='pooluser',
password='poolpassword',
min_size=5, max_size=10)
async def worker():
con = await pool.acquire()
self.assertEqual(await con.fetchval('SELECT 1'), 1)
await pool.release(con)
tasks = [worker() for _ in range(5)]
await asyncio.gather(*tasks, loop=self.loop)
await pool.close()
finally:
await self.con.execute('DROP ROLE pooluser')
# Reset cluster's pg_hba.conf since we've meddled with it
self.cluster.trust_local_connections()
self.cluster.reload()