1
|
|
|
import asyncio |
2
|
|
|
|
3
|
|
|
import pytest |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
@pytest.mark.asyncio |
7
|
|
|
async def test_pool_init(connection_pool): |
8
|
|
|
await connection_pool.init_pool() |
9
|
|
|
assert len(connection_pool._available) == 1 |
10
|
|
|
await connection_pool.close() |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
@pytest.mark.asyncio |
14
|
|
|
async def test_acquire_release(connection_pool): |
15
|
|
|
conn = await connection_pool.acquire() |
16
|
|
|
assert not len(connection_pool._available) |
17
|
|
|
assert len(connection_pool._acquired) == 1 |
18
|
|
|
assert conn.times_acquired == 1 |
19
|
|
|
connection_pool.release(conn) |
20
|
|
|
assert len(connection_pool._available) == 1 |
21
|
|
|
assert not len(connection_pool._acquired) |
22
|
|
|
assert not conn.times_acquired |
23
|
|
|
await connection_pool.close() |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
@pytest.mark.asyncio |
27
|
|
|
async def test_acquire_multiple(connection_pool): |
28
|
|
|
conn1 = await connection_pool.acquire() |
29
|
|
|
conn2 = await connection_pool.acquire() |
30
|
|
|
assert conn1 is not conn2 |
31
|
|
|
assert len(connection_pool._acquired) == 2 |
32
|
|
|
await connection_pool.close() |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
@pytest.mark.asyncio |
36
|
|
|
async def test_share(connection_pool): |
37
|
|
|
connection_pool._max_conns = 1 |
38
|
|
|
conn1 = await connection_pool.acquire() |
39
|
|
|
conn2 = await connection_pool.acquire() |
40
|
|
|
assert conn1 is conn2 |
41
|
|
|
assert conn1.times_acquired == 2 |
42
|
|
|
await connection_pool.close() |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
@pytest.mark.asyncio |
46
|
|
|
async def test_acquire_multiple_and_share(connection_pool): |
47
|
|
|
connection_pool._max_conns = 2 |
48
|
|
|
connection_pool._max_times_acquired = 2 |
49
|
|
|
conn1 = await connection_pool.acquire() |
50
|
|
|
conn2 = await connection_pool.acquire() |
51
|
|
|
assert conn1 is not conn2 |
52
|
|
|
conn3 = await connection_pool.acquire() |
53
|
|
|
conn4 = await connection_pool.acquire() |
54
|
|
|
assert conn3 is not conn4 |
55
|
|
|
assert conn3 is conn1 |
56
|
|
|
assert conn4 is conn2 |
57
|
|
|
await connection_pool.close() |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
@pytest.mark.asyncio |
61
|
|
|
async def test_max_acquired(connection_pool): |
62
|
|
|
connection_pool._max_conns = 2 |
63
|
|
|
connection_pool._max_times_acquired = 2 |
64
|
|
|
conn1 = await connection_pool.acquire() |
65
|
|
|
conn2 = await connection_pool.acquire() |
66
|
|
|
conn3 = await connection_pool.acquire() |
67
|
|
|
conn4 = await connection_pool.acquire() |
68
|
|
|
with pytest.raises(asyncio.TimeoutError): |
69
|
|
|
await asyncio.wait_for(connection_pool.acquire(), timeout=0.1) |
70
|
|
|
await connection_pool.close() |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
@pytest.mark.asyncio |
74
|
|
|
async def test_release_notify(connection_pool): |
75
|
|
|
connection_pool._max_conns = 2 |
76
|
|
|
connection_pool._max_times_acquired = 2 |
77
|
|
|
conn1 = await connection_pool.acquire() |
78
|
|
|
conn2 = await connection_pool.acquire() |
79
|
|
|
conn3 = await connection_pool.acquire() |
80
|
|
|
conn4 = await connection_pool.acquire() |
81
|
|
|
|
82
|
|
|
async def release(conn): |
83
|
|
|
conn.release() |
84
|
|
|
|
85
|
|
|
results = await asyncio.gather( |
86
|
|
|
*[connection_pool.acquire(), release(conn4)]) |
87
|
|
|
conn4 = results[0] |
88
|
|
|
assert conn4 is conn2 |
89
|
|
|
await connection_pool.close() |
90
|
|
|
|