-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathtest_pooled_pg.py
321 lines (293 loc) · 9.15 KB
/
test_pooled_pg.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""Test the PooledPg module.
Note:
We don't test performance here, so the test does not predicate
whether PooledPg actually will help in improving performance or not.
We also assume that the underlying SteadyPg connections are tested.
Copyright and credit info:
* This test was contributed by Christoph Zwerschke
"""
from queue import Empty, Queue
from threading import Thread
import pg # noqa: F401
import pytest
from dbutils.pooled_pg import (
InvalidConnectionError,
PooledPg,
TooManyConnectionsError,
)
from dbutils.steady_pg import SteadyPgConnection
def test_version():
from dbutils import __version__, pooled_pg
assert pooled_pg.__version__ == __version__
assert PooledPg.version == __version__
def test_create_connection():
pool = PooledPg(
1, 1, 0, False, None, None, False,
'PooledPgTestDB', user='PooledPgTestUser')
assert hasattr(pool, '_cache')
assert pool._cache.qsize() == 1
assert hasattr(pool, '_maxusage')
assert pool._maxusage is None
assert hasattr(pool, '_setsession')
assert pool._setsession is None
assert hasattr(pool, '_reset')
assert not pool._reset
db_con = pool._cache.get(0)
pool._cache.put(db_con, 0)
assert isinstance(db_con, SteadyPgConnection)
db = pool.connection()
assert pool._cache.qsize() == 0
assert hasattr(db, '_con')
assert db._con == db_con
assert hasattr(db, 'query')
assert hasattr(db, 'num_queries')
assert db.num_queries == 0
assert hasattr(db, '_maxusage')
assert db._maxusage == 0
assert hasattr(db, '_setsession_sql')
assert db._setsession_sql is None
assert hasattr(db, 'dbname')
assert db.dbname == 'PooledPgTestDB'
assert hasattr(db, 'user')
assert db.user == 'PooledPgTestUser'
db.query('select test')
assert db.num_queries == 1
pool = PooledPg(1)
db = pool.connection()
assert hasattr(db, 'dbname')
assert db.dbname is None
assert hasattr(db, 'user')
assert db.user is None
assert hasattr(db, 'num_queries')
assert db.num_queries == 0
pool = PooledPg(0, 0, 0, False, 3, ('set datestyle',))
assert pool._maxusage == 3
assert pool._setsession == ('set datestyle',)
db = pool.connection()
assert db._maxusage == 3
assert db._setsession_sql == ('set datestyle',)
def test_close_connection():
pool = PooledPg(
0, 1, 0, False, None, None, False,
'PooledPgTestDB', user='PooledPgTestUser')
db = pool.connection()
assert hasattr(db, '_con')
db_con = db._con
assert isinstance(db_con, SteadyPgConnection)
assert hasattr(pool, '_cache')
assert pool._cache.qsize() == 0
assert db.num_queries == 0
db.query('select test')
assert db.num_queries == 1
db.close()
with pytest.raises(InvalidConnectionError):
assert db.num_queries
db = pool.connection()
assert hasattr(db, 'dbname')
assert db.dbname == 'PooledPgTestDB'
assert hasattr(db, 'user')
assert db.user == 'PooledPgTestUser'
assert db.num_queries == 1
db.query('select test')
assert db.num_queries == 2
db = pool.connection()
assert pool._cache.qsize() == 1
assert pool._cache.get(0) == db_con
assert db
del db
def test_min_max_cached():
pool = PooledPg(3)
assert hasattr(pool, '_cache')
assert pool._cache.qsize() == 3
cache = [pool.connection() for _i in range(3)]
assert pool._cache.qsize() == 0
for _i in range(3):
cache.pop().close()
assert pool._cache.qsize() == 3
for _i in range(6):
cache.append(pool.connection())
assert pool._cache.qsize() == 0
for _i in range(6):
cache.pop().close()
assert pool._cache.qsize() == 6
pool = PooledPg(3, 4)
assert hasattr(pool, '_cache')
assert pool._cache.qsize() == 3
cache = [pool.connection() for _i in range(3)]
assert pool._cache.qsize() == 0
for _i in range(3):
cache.pop().close()
assert pool._cache.qsize() == 3
for _i in range(6):
cache.append(pool.connection())
assert pool._cache.qsize() == 0
for _i in range(6):
cache.pop().close()
assert pool._cache.qsize() == 4
pool = PooledPg(3, 2)
assert hasattr(pool, '_cache')
assert pool._cache.qsize() == 3
cache = [pool.connection() for _i in range(4)]
assert pool._cache.qsize() == 0
for _i in range(4):
cache.pop().close()
assert pool._cache.qsize() == 3
pool = PooledPg(2, 5)
assert hasattr(pool, '_cache')
assert pool._cache.qsize() == 2
cache = [pool.connection() for _i in range(10)]
assert pool._cache.qsize() == 0
for _i in range(10):
cache.pop().close()
assert pool._cache.qsize() == 5
def test_max_connections():
pool = PooledPg(1, 2, 3)
assert pool._cache.qsize() == 1
cache = [pool.connection() for _i in range(3)]
assert pool._cache.qsize() == 0
with pytest.raises(TooManyConnectionsError):
pool.connection()
pool = PooledPg(0, 1, 1, False)
assert pool._blocking == 0
assert pool._cache.qsize() == 0
db = pool.connection()
assert pool._cache.qsize() == 0
with pytest.raises(TooManyConnectionsError):
pool.connection()
assert db
del db
assert cache
del cache
pool = PooledPg(1, 2, 1)
assert pool._cache.qsize() == 1
cache = [pool.connection()]
assert pool._cache.qsize() == 0
cache.append(pool.connection())
assert pool._cache.qsize() == 0
with pytest.raises(TooManyConnectionsError):
pool.connection()
pool = PooledPg(3, 2, 1, False)
assert pool._cache.qsize() == 3
cache = [pool.connection() for _i in range(3)]
assert len(cache) == 3
assert pool._cache.qsize() == 0
with pytest.raises(TooManyConnectionsError):
pool.connection()
pool = PooledPg(1, 1, 1, True)
assert pool._blocking == 1
assert pool._cache.qsize() == 1
db = pool.connection()
assert pool._cache.qsize() == 0
def connection():
pool.connection().query('set thread')
thread = Thread(target=connection)
thread.start()
thread.join(0.1)
assert thread.is_alive()
assert pool._cache.qsize() == 0
session = db._con.session
assert session == []
del db
thread.join(0.1)
assert not thread.is_alive()
assert pool._cache.qsize() == 1
db = pool.connection()
assert pool._cache.qsize() == 0
assert session == ['thread']
assert db
del db
def test_one_thread_two_connections():
pool = PooledPg(2)
db1 = pool.connection()
for _i in range(5):
db1.query('select test')
db2 = pool.connection()
assert db1 != db2
assert db1._con != db2._con
for _i in range(7):
db2.query('select test')
assert db1.num_queries == 5
assert db2.num_queries == 7
del db1
db1 = pool.connection()
assert db1 != db2
assert db1._con != db2._con
assert hasattr(db1, 'query')
for _i in range(3):
db1.query('select test')
assert db1.num_queries == 8
db2.query('select test')
assert db2.num_queries == 8
def test_three_threads_two_connections():
pool = PooledPg(2, 2, 2, True)
queue = Queue(3)
def connection():
queue.put(pool.connection(), timeout=1)
for _i in range(3):
Thread(target=connection).start()
db1 = queue.get(timeout=1)
db2 = queue.get(timeout=1)
db1_con = db1._con
db2_con = db2._con
assert db1 != db2
assert db1_con != db2_con
with pytest.raises(Empty):
queue.get(timeout=0.1)
del db1
db1 = queue.get(timeout=1)
assert db1 != db2
assert db1._con != db2._con
assert db1._con == db1_con
def test_reset_transaction():
pool = PooledPg(1)
db = pool.connection()
db.begin()
con = db._con
assert con._transaction
db.query('select test')
assert con.num_queries == 1
db.close()
assert pool.connection()._con is con
assert not con._transaction
assert con.session == ['begin', 'rollback']
assert con.num_queries == 1
pool = PooledPg(1, reset=1)
db = pool.connection()
db.begin()
con = db._con
assert con._transaction
assert con.session == ['rollback', 'begin']
db.query('select test')
assert con.num_queries == 1
db.close()
assert pool.connection()._con is con
assert not con._transaction
assert con.session == ['rollback', 'begin', 'rollback', 'rollback']
assert con.num_queries == 1
pool = PooledPg(1, reset=2)
db = pool.connection()
db.begin()
con = db._con
assert con._transaction
assert con.session == ['begin']
db.query('select test')
assert con.num_queries == 1
db.close()
assert pool.connection()._con is con
assert not con._transaction
assert con.session == []
assert con.num_queries == 0
def test_context_manager():
pool = PooledPg(1, 1, 1)
with pool.connection() as db:
db_con = db._con._con
db.query('select test')
assert db_con.num_queries == 1
with pytest.raises(TooManyConnectionsError):
pool.connection()
with pool.connection() as db:
db_con = db._con._con
db.query('select test')
assert db_con.num_queries == 2
with pytest.raises(TooManyConnectionsError):
pool.connection()