forked from coleifer/peewee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests_pool.py
174 lines (138 loc) · 5.16 KB
/
tests_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import heapq
import psycopg2 # Trigger import error if not installed.
import threading
import time
from unittest import TestCase
from peewee import *
from playhouse.pool import *
class FakeDatabase(SqliteDatabase):
def __init__(self, *args, **kwargs):
self.counter = 0
self.closed_counter = 0
super(FakeDatabase, self).__init__(*args, **kwargs)
def _connect(self, *args, **kwargs):
"""
Return increasing integers instead of actual database connections.
"""
self.counter += 1
return self.counter
def _close(self, conn):
self.closed_counter += 1
class TestDB(PooledDatabase, FakeDatabase):
def __init__(self, *args, **kwargs):
super(TestDB, self).__init__(*args, **kwargs)
self.conn_key = lambda conn: conn
pooled_db = PooledPostgresqlDatabase('peewee_test')
normal_db = PostgresqlDatabase('peewee_test')
class Number(Model):
value = IntegerField()
class Meta:
database = pooled_db
class TestPooledDatabase(TestCase):
def setUp(self):
self.db = TestDB('testing')
def test_connection_pool(self):
# Ensure that a connection is created and accessible.
self.assertEqual(self.db.get_conn(), 1)
self.assertEqual(self.db.get_conn(), 1)
# Ensure that closing and reopening will return the same connection.
self.db.close()
self.db.connect()
self.assertEqual(self.db.get_conn(), 1)
def test_concurrent_connections(self):
db = TestDB('testing', threadlocals=True)
signal = threading.Event()
def open_conn():
db.connect()
signal.wait()
db.close()
# Simulate 5 concurrent connections.
threads = [threading.Thread(target=open_conn) for i in range(5)]
for thread in threads:
thread.start()
# Wait for all connections to be opened.
while db.counter < 5:
time.sleep(.01)
# Signal threads to close connections and join threads.
signal.set()
[t.join() for t in threads]
self.assertEqual(db.counter, 5)
self.assertEqual(db._in_use, {})
def test_max_conns(self):
for i in range(self.db.max_connections):
self.db.connect()
self.assertEqual(self.db.get_conn(), i + 1)
self.assertRaises(ValueError, self.db.connect)
def test_stale_timeout(self):
# Create a test database with a very short stale timeout.
db = TestDB('testing', stale_timeout=.01)
self.assertEqual(db.get_conn(), 1)
# Return the connection to the pool.
db.close()
# Sleep long enough for the connection to be considered stale.
time.sleep(.01)
# A new connection will be returned.
self.assertEqual(db.get_conn(), 2)
def test_manual_close(self):
conn = self.db.get_conn()
self.assertEqual(conn, 1)
self.db.manual_close()
conn = self.db.get_conn()
self.assertEqual(conn, 2)
self.db.close()
conn = self.db.get_conn()
self.assertEqual(conn, 2)
def test_stale_timeout_cascade(self):
now = time.time()
db = TestDB('testing', stale_timeout=10)
conns = [
(now - 20, 1),
(now - 15, 2),
(now - 5, 3),
(now, 4),
]
for ts_conn in conns:
heapq.heappush(db._connections, ts_conn)
self.assertEqual(db.get_conn(), 3)
self.assertEqual(db._in_use, {3: now - 5})
self.assertEqual(db._connections, [(now, 4)])
def test_connect_cascade(self):
now = time.time()
db = TestDB('testing', stale_timeout=10)
conns = [
(now - 15, 1), # Skipped due to being stale.
(now - 5, 2), # In the 'closed' set.
(now - 3, 3),
(now, 4), # In the 'closed' set.
]
db._closed.add(2)
db._closed.add(4)
db.counter = 4 # The next connection we create will have id=5.
for ts_conn in conns:
heapq.heappush(db._connections, ts_conn)
# Conn 3 is not stale or closed, so we will get it.
self.assertEqual(db.get_conn(), 3)
self.assertEqual(db._in_use, {3: now - 3})
self.assertEqual(db._connections, [(now, 4)])
# Since conn 4 is closed, we will open a new conn.
db.connect()
self.assertEqual(db.get_conn(), 5)
self.assertEqual(sorted(db._in_use.keys()), [3, 5])
self.assertEqual(db._connections, [])
class TestConnectionPool(TestCase):
def setUp(self):
# Use an un-pooled database to drop/create the table.
if Number._meta.db_table in normal_db.get_tables():
normal_db.drop_table(Number)
normal_db.create_table(Number)
def test_reuse_connection(self):
for i in range(5):
Number.create(value=i)
conn_id = id(pooled_db.get_conn())
pooled_db.close()
for i in range(5, 10):
Number.create(value=i)
self.assertEqual(id(pooled_db.get_conn()), conn_id)
self.assertEqual(
[x.value for x in Number.select().order_by(Number.id)],
list(range(10)))