-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathtest_simple_pooled_db.py
More file actions
156 lines (129 loc) · 4.76 KB
/
Copy pathtest_simple_pooled_db.py
File metadata and controls
156 lines (129 loc) · 4.76 KB
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
"""Test the SimplePooledDB module.
Note:
We don't test performance here, so the test does not predicate
whether SimplePooledDB actually will help in improving performance or not.
We also do not test any real world DB-API 2 module, we just
mock the basic connection functionality of an arbitrary module.
Copyright and credit info:
* This test was contributed by Christoph Zwerschke
"""
from queue import Empty, Queue
from threading import Thread
import pytest
from dbutils import simple_pooled_db
from . import mock_db as dbapi
def my_db_pool(threadsafety, max_connections):
"""Get simple PooledDB connection."""
dbapi_threadsafety = dbapi.threadsafety
dbapi.threadsafety = threadsafety
try:
return simple_pooled_db.PooledDB(
dbapi, max_connections,
'SimplePooledDBTestDB', 'SimplePooledDBTestUser')
finally:
dbapi.threadsafety = dbapi_threadsafety
def test_version():
"""Check that the module and class versions are in sync."""
from dbutils import __version__
assert simple_pooled_db.__version__ == __version__
assert simple_pooled_db.PooledDB.version == __version__
@pytest.mark.parametrize("threadsafety", [None, -1, 0, 4])
def test_no_threadsafety(threadsafety):
"""Check that an unsupported threadsafety level is rejected."""
with pytest.raises(simple_pooled_db.NotSupportedError):
my_db_pool(threadsafety, 1)
def test_no_threadsafety_attribute(monkeypatch):
"""Check that a module hiding its threadsafety is rejected."""
monkeypatch.delattr(dbapi, 'threadsafety')
with pytest.raises(simple_pooled_db.NotSupportedError):
simple_pooled_db.PooledDB(
dbapi, 1, 'SimplePooledDBTestDB', 'SimplePooledDBTestUser')
@pytest.mark.parametrize("threadsafety", [1, 2, 3])
def test_create_connection(threadsafety):
"""Check that the pool creates a usable connection."""
dbpool = my_db_pool(threadsafety, 1)
db = dbpool.connection()
assert hasattr(db, 'cursor')
assert hasattr(db, 'open_cursors')
assert db.open_cursors == 0
assert hasattr(db, 'database')
assert db.database == 'SimplePooledDBTestDB'
assert hasattr(db, 'user')
assert db.user == 'SimplePooledDBTestUser'
cursor = db.cursor()
assert cursor is not None
assert db.open_cursors == 1
del cursor
@pytest.mark.parametrize("threadsafety", [1, 2, 3])
def test_close_connection(threadsafety):
"""Check that a closed connection is reused by the pool."""
db_pool = my_db_pool(threadsafety, 1)
db = db_pool.connection()
assert db.open_cursors == 0
cursor1 = db.cursor()
assert cursor1 is not None
assert db.open_cursors == 1
db.close()
assert not hasattr(db, 'open_cursors')
db = db_pool.connection()
assert hasattr(db, 'database')
assert db.database == 'SimplePooledDBTestDB'
assert hasattr(db, 'user')
assert db.user == 'SimplePooledDBTestUser'
assert db.open_cursors == 1
cursor2 = db.cursor()
assert cursor2 is not None
assert db.open_cursors == 2
del cursor2
del cursor1
@pytest.mark.parametrize("threadsafety", [1, 2, 3])
def test_two_connections(threadsafety):
"""Check that two connections from the pool stay independent."""
db_pool = my_db_pool(threadsafety, 2)
db1 = db_pool.connection()
cursors1 = [db1.cursor() for _i_ in range(5)]
db2 = db_pool.connection()
assert db1 != db2
cursors2 = [db2.cursor() for _i in range(7)]
assert db1.open_cursors == 5
assert db2.open_cursors == 7
db1.close()
db1 = db_pool.connection()
assert db1 != db2
assert hasattr(db1, 'cursor')
for _i in range(3):
cursors1.append(db1.cursor())
assert db1.open_cursors == 8
cursors2.append(db2.cursor())
assert db2.open_cursors == 8
del cursors2
del cursors1
def test_threadsafety_1():
"""Check that threads get dedicated connections and have to wait."""
db_pool = my_db_pool(1, 2)
queue = Queue(3)
def connection():
queue.put(db_pool.connection())
threads = [Thread(target=connection).start() for _i in range(3)]
assert len(threads) == 3
db1 = queue.get(timeout=1)
db2 = queue.get(timeout=1)
assert db1 != db2
assert db1._con != db2._con
with pytest.raises(Empty):
queue.get(timeout=0.1)
db2.close()
db3 = queue.get(timeout=1)
assert db1 != db3
assert db1._con != db3._con
@pytest.mark.parametrize("threadsafety", [2, 3])
def test_threadsafety_2(threadsafety):
"""Check that connections are shared when they are thread-safe."""
dbpool = my_db_pool(threadsafety, 2)
db1 = dbpool.connection()
db2 = dbpool.connection()
cursors = [dbpool.connection().cursor() for _i in range(100)]
assert db1.open_cursors == 50
assert db2.open_cursors == 50
assert cursors
del cursors