forked from bninja/pika-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
207 lines (164 loc) · 5.52 KB
/
test.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
from __future__ import unicode_literals
import json
import select
import threading
import time
import uuid
import pika
import pytest
import pika_pool
@pytest.fixture(scope='session')
def params():
return pika.URLParameters('amqp://guest:guest@localhost:5672/')
@pytest.fixture(scope='session', autouse=True)
def schema(request, params):
cxn = pika.BlockingConnection(params)
channel = cxn.channel()
channel.queue_declare(queue='pika_pool_test')
consumed = {
}
@pytest.fixture(scope='session', autouse=True)
def consume(params):
def _callback(ch, method, properties, body):
msg = Message.from_json(body)
consumed[msg.id] = msg
def _forever():
channel.start_consuming()
cxn = pika.BlockingConnection(params)
channel = cxn.channel()
channel.queue_declare(queue='pika_pool_test')
channel.basic_consume(_callback, queue='pika_pool_test', no_ack=True)
thd = threading.Thread(target=_forever)
thd.daemon = True
thd.start()
@pytest.fixture
def null_pool(params):
return pika_pool.NullPool(
create=lambda: pika.BlockingConnection(params),
)
class Message(dict):
@classmethod
def generate(cls, **kwargs):
id = kwargs.pop('id', uuid.uuid4().hex)
return cls(id=id, **kwargs)
@property
def id(self):
return self['id']
def to_json(self):
return json.dumps(self)
@classmethod
def from_json(cls, raw):
return cls(json.loads(raw.decode('utf-8')))
class TestNullPool(object):
def test_pub(self, null_pool):
msg = Message.generate()
with null_pool.acquire() as cxn:
cxn.channel.basic_publish(
exchange='',
routing_key='pika_pool_test',
body=msg.to_json()
)
time.sleep(0.1)
assert msg.id in consumed
@pytest.fixture
def queued_pool(params):
return pika_pool.QueuedPool(
create=lambda: pika.BlockingConnection(params),
recycle=10,
stale=10,
max_size=10,
max_overflow=10,
timeout=10,
)
@pytest.fixture
def empty_queued_pool(request, queued_pool):
queued = [queued_pool.acquire() for _ in range(queued_pool.max_size)]
request.addfinalizer(lambda: [cxn.release() for cxn in queued])
overflow = [queued_pool.acquire() for _ in range(queued_pool.max_overflow)]
request.addfinalizer(lambda: [cxn.release() for cxn in overflow])
return queued_pool
def test_use_it():
params = pika.URLParameters(
'amqp://guest:guest@localhost:5672/?'
'socket_timeout=10&'
'connection_attempts=2'
)
pool = pika_pool.QueuedPool(
create=lambda: pika.BlockingConnection(parameters=params),
max_size=10,
max_overflow=10,
timeout=10,
recycle=3600,
stale=45,
)
with pool.acquire() as cxn:
cxn.channel.basic_publish(
body=json.dumps({
'type': 'banana',
'description': 'they are yellow'
}),
exchange='',
routing_key='fruits',
properties=pika.BasicProperties(
content_type='application/json',
content_encoding='utf-8',
delivery_mode=2,
)
)
assert 'cxn=localhost:5672//' in str(cxn.fairy)
class TestQueuedPool(object):
def test_invalidate_connection(slef, queued_pool):
msg = Message.generate()
with pytest.raises(select.error):
with queued_pool.acquire() as cxn:
fairy = cxn.fairy
raise select.error(9, 'Bad file descriptor')
assert fairy.cxn.is_closed
def test_pub(self, queued_pool):
msg = Message.generate()
with queued_pool.acquire() as cxn:
cxn.channel.basic_publish(
exchange='',
routing_key='pika_pool_test',
body=msg.to_json()
)
time.sleep(0.1)
assert msg.id in consumed
def test_expire(self, queued_pool):
with queued_pool.acquire() as cxn:
expired = id(cxn.fairy.cxn)
expires_at = cxn.fairy.created_at + queued_pool.recycle
with queued_pool.acquire() as cxn:
assert expired == id(cxn.fairy.cxn)
cxn.fairy.created_at -= queued_pool.recycle
with queued_pool.acquire() as cxn:
assert expired != id(cxn.fairy.cxn)
def test_stale(self, queued_pool):
with queued_pool.acquire() as cxn:
stale = id(cxn.fairy.cxn)
fairy = cxn.fairy
with queued_pool.acquire() as cxn:
assert stale == id(cxn.fairy.cxn)
fairy.released_at -= queued_pool.stale
with queued_pool.acquire() as cxn:
assert stale != id(cxn.fairy.cxn)
def test_overflow(self, queued_pool):
queued = [queued_pool.acquire() for _ in range(queued_pool.max_size)]
with queued_pool.acquire() as cxn:
fairy = cxn.fairy
for cxn in queued:
cxn.release()
assert fairy.cxn.is_closed
def test_timeout(self, empty_queued_pool):
empty_queued_pool.timeout = 2
st = time.time()
with pytest.raises(pika_pool.Timeout):
empty_queued_pool.acquire()
elapsed = time.time() - st
assert elapsed < 2.5
def test_timeout_override(self, empty_queued_pool):
st = time.time()
with pytest.raises(pika_pool.Timeout):
empty_queued_pool.acquire(timeout=1)
elapsed = time.time() - st
assert elapsed < 1.5