Debian Codegraph @

tests/test_pool.py

raw symbols SCIP
1 import pytest
2
3 from bonsai import LDAPClient
4 from bonsai.pool import (
5 ClosedPool,
6 ConnectionPool,
7 EmptyPool,
8 PoolError,
9 ThreadedConnectionPool,
10 )
11
12 import math
13 import threading
14 import time
15
16
17 def test_init():
18 """ Test pool initialisation. """
19 cli = LDAPClient("ldap://dummy.nfo")
20 with pytest.raises(ValueError):
21 _ = ConnectionPool(cli, minconn=-3)
22 with pytest.raises(ValueError):
23 _ = ConnectionPool(cli, minconn=5, maxconn=3)
24 pool = ConnectionPool(cli, minconn=2, maxconn=5)
25 assert pool.closed == True
26 assert pool.empty == False
27 assert pool.max_connection == 5
28 assert pool.shared_connection == 0
29 assert pool.idle_connection == 0
30
31
32 def test_open(client):
33 """ Test opening the pool. """
34 pool = ConnectionPool(client, minconn=5)
35 pool.open()
36 assert pool.closed != True
37 assert pool.idle_connection == 5
38
39
40 def test_close(client):
41 """ Test closing the connection. """
42 pool = ConnectionPool(client, minconn=1)
43 pool.open()
44 conn = pool.get()
45 pool.close()
46 assert pool.closed == True
47 assert pool.idle_connection == 0
48 assert pool.shared_connection == 0
49 assert conn.closed == True
50
51
52 def test_get(client):
53 """ Test getting a connection from the pool. """
54 pool = ConnectionPool(client, minconn=1, maxconn=2)
55 with pytest.raises(ClosedPool):
56 _ = pool.get()
57 pool.open()
58 assert pool.max_connection == 2
59 assert pool.idle_connection == 1
60 assert pool.shared_connection == 0
61 conn1 = pool.get()
62 assert conn1 is not None
63 assert conn1.closed == False
64 assert pool.idle_connection == 0
65 assert pool.shared_connection == 1
66 conn2 = pool.get()
67 assert pool.idle_connection == 0
68 assert pool.shared_connection == 2
69 with pytest.raises(EmptyPool):
70 _ = pool.get()
71 assert pool.empty == True
72
73
74 def test_put(client):
75 """ Test putting connection back into the pool. """
76 pool = ConnectionPool(client, minconn=1, maxconn=1)
77 with pytest.raises(ClosedPool):
78 pool.put(None)
79 pool.open()
80 conn = pool.get()
81 assert pool.idle_connection == 0
82 pool.put(conn)
83 assert pool.idle_connection == 1
84 other_conn = client.connect()
85 with pytest.raises(PoolError):
86 pool.put(other_conn)
87
88
89 def test_put_closed(client):
90 """ Test closed connections are not put back into the pool. """
91 pool = ConnectionPool(client, minconn=1, maxconn=1)
92 pool.open()
93 conn = pool.get()
94 assert pool.idle_connection == 0
95 assert pool.shared_connection == 1
96 conn.close()
97 pool.put(conn)
98 assert pool.idle_connection == 0
99 assert pool.shared_connection == 0
100
101 # The pool is below its minimum connection count, but this shouldn't cause
102 # problems and get should create a new connection.
103 conn = pool.get()
104 assert pool.idle_connection == 0
105 assert pool.shared_connection == 1
106 pool.put(conn)
107 pool.close()
108
109
110 def test_max_connection():
111 """ Test max_connection property. """
112 cli = LDAPClient("ldap://dummy.nfo")
113 pool = ConnectionPool(cli, minconn=5, maxconn=5)
114 assert pool.max_connection == 5
115 with pytest.raises(ValueError):
116 pool.max_connection = 4
117 pool.max_connection = 10
118 assert pool.max_connection == 10
119
120
121 def test_spawn(client):
122 """ Test context manager. """
123 pool = ConnectionPool(client, minconn=1, maxconn=1)
124 assert pool.idle_connection == 0
125 with pool.spawn() as conn:
126 _ = conn.whoami()
127 assert pool.idle_connection == 1
128 assert pool.shared_connection == 0
129
130
131 def keep(pool, sleep):
132 try:
133 with pool.spawn() as conn:
134 time.sleep(sleep)
135 assert not conn.closed
136 except ClosedPool:
137 pass
138
139
140 def test_threaded_pool_block(client):
141 """ Test threaded pool blocks when it's empty. """
142 sleep = 5
143 pool = ThreadedConnectionPool(client, minconn=1, maxconn=1)
144 pool.open()
145 t0 = threading.Thread(target=keep, args=(pool, sleep))
146 conn = pool.get()
147 t0.start()
148 pool.put(conn)
149 time.sleep(2)
150 start = time.time()
151 conn = pool.get()
152 # Some assertation that it didn't happen immediately.
153 assert math.ceil(time.time() - start) + 1.2 >= sleep / 2.0
154 assert not conn.closed
155 pool.put(conn)
156
157
158 def test_threaded_pool_raise(client):
159 """ Test threaded pool blocks when it's empty. """
160 sleep = 5
161 pool = ThreadedConnectionPool(client, minconn=1, maxconn=1, block=False)
162 t0 = threading.Thread(target=keep, args=(pool, sleep))
163 t0.start()
164 time.sleep(2)
165 with pytest.raises(EmptyPool):
166 _ = pool.get()
167 time.sleep(sleep + 1.2)
168 conn = pool.get()
169 assert conn.closed == False
170
171
172 def test_threaded_pool_close(client):
173 """ Test closing threaded pool. """
174 sleep = 5
175 pool = ThreadedConnectionPool(client, minconn=1, maxconn=1, block=False)
176 t0 = threading.Thread(target=keep, args=(pool, sleep))
177 t0.start()
178 time.sleep(2)
179 pool.close()
180 assert pool.closed
181 t0.join()