forked from LandRegistry/RabbitMQ-Queue-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
316 lines (217 loc) · 10.6 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
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
#!/bin/python
import json
import unittest
import datetime
import os
import socket
from queue_manager import manager
from multiprocessing import Process
"""
Test manager on an 'ad hoc' basis or automatically (pytest).
"""
# Set up root logger
logger = manager.logger
# Basic test data.
def make_message():
dt = str(datetime.datetime.now())
return json.dumps(dt.split())
class Application(object):
""" Mimic Process calls, for logging purposes. """
def __init__(self, target=manager.run):
self.target = target
self.process = None
logger.debug("Target:'{}'.".format(self.target))
def start(self):
self.process = Process(target=self.target)
logger.debug("Starting '{}' as a separate process.".format(self.target))
self.process.start()
def join(self, timeout=None):
self.process.join(timeout=timeout)
logger.debug("'{}' completed.".format(self.target))
def terminate(self):
self.process.terminate()
logger.debug("'{}' terminated.".format(self.target))
# N.B.: these tests may reverse the default 'producer' and 'consumer' targets.
class TestRegisterPublisher(unittest.TestCase):
#: This can be the callback applied when a message is received - i.e. "consume()" case.
def handle_message(self, body, message):
# Note: 'body' may have been pickled, so refer to 'payload' instead.
logger.info('Received message: {}'.format(message.payload))
logger.info(' properties: {}'.format(message.properties))
logger.info(' delivery_info: {}'.format(message.delivery_info))
message.ack()
self.payload = message.payload
def consume(self, cfg=manager.incoming_cfg):
""" Get message via callback mechanism """
with manager.setup_consumer(cfg=cfg, callback=self.handle_message) as consumer:
logger.debug("cfg: {}".format(cfg))
# 'consume' may be a misnomer here - it just initiates the consumption process, I believe.
consumer.consume()
# Execute 'drain_events()' loop in a time-out thread, in case it gets stuck.
logger.info("'drain_events()' with timeout")
try:
consumer.connection.drain_events(timeout=5)
except Exception as e:
logger.error(e)
raise
finally:
consumer.close()
def reset(self):
# """ Clear the decks. """
logger.debug("reset")
try:
with manager.setup_connection(manager.outgoing_cfg.hostname) as outgoing_connection:
# Need a connection to delete the queues.
self.assertEqual(outgoing_connection.connected, True)
outgoing_channel = outgoing_connection.channel()
queue = manager.setup_queue(outgoing_channel, cfg=manager.outgoing_cfg)
queue.purge()
queue.delete()
with manager.setup_connection(manager.incoming_cfg.hostname) as incoming_connection:
# Need a connection to delete the queues.
self.assertEqual(incoming_connection.connected, True)
incoming_channel = incoming_connection.channel()
queue = manager.setup_queue(incoming_channel, cfg=manager.incoming_cfg)
queue.purge()
queue.delete()
except Exception as e:
logger.error(e)
raise
def setUp(self):
""" Establish connection and other resources; prepare """
logger.debug("setUp")
self.app = Application()
# Ensure that message broker is alive, etc.
self.reset()
self.message = None # Message to be sent.
self.payload = None # Corresponding 'payload' of message received.
# Execute 'manager.run()' as a separate process.
self.app.start()
test_title = self.id().split(sep='.')[-1]
logger.info(test_title)
def tearDown(self):
logger.debug("setUp")
# N.B.: app needs to be terminated before queues can be deleted!
self.app.join(timeout=5)
self.app.terminate()
if os.getenv('LOG_THRESHOLD_LEVEL') != 'DEBUG':
self.reset()
def test_incoming_queue(self):
""" Basic check of 'incoming' message via default direct exchange """
# We don't need the app to be running for this test.
self.app.terminate()
self.message = make_message()
producer = manager.setup_producer(cfg=manager.incoming_cfg)
producer.publish(body=self.message, routing_key=manager.incoming_cfg.queue, headers={'title_number': 'DN1'})
logger.info("Put message, exchange: {}, {}".format(self.message, producer.exchange))
producer.close()
self.consume()
self.assertEqual(self.message, self.payload)
def test_broken_connection(self):
""" Attempt 'publish' via closed connection, which is subsequently restored. """
self.message = make_message()
# Send a message to 'incoming' exchange - i.e. as if from source .
with manager.setup_producer(cfg=manager.incoming_cfg) as producer:
producer.publish(body=self.message, routing_key=manager.incoming_cfg.queue, headers={'title_number': 'DN1'})
# Kill connection to broker.
producer.connection.close()
# Block (wait) until app times out or terminates.
self.app.join(timeout=5)
# Consume message from outgoing exchange; this will establish another connection.
self.consume(cfg=manager.outgoing_cfg)
self.assertEqual(self.message, self.payload)
def test_stored_incoming_message(self):
""" Store message in INCOMING queue, then consume later and check it. """
self.app.terminate()
self.message = make_message()
# Send a message to 'incoming' exchange - i.e. as if from source .
with manager.setup_producer(cfg=manager.incoming_cfg) as producer:
producer.publish(body=self.message, headers={'title_number': 'DN1'})
logger.debug(self.message)
self.app.start()
# Consume message from outgoing exchange.
self.consume(cfg=manager.outgoing_cfg)
self.assertEqual(self.message, self.payload)
def test_stored_outgoing_message(self):
""" Store message in OUTGOING queue, then consume later and check it. """
self.message = make_message()
# Send a message to 'incoming' exchange - i.e. as if from source .
with manager.setup_producer(cfg=manager.incoming_cfg) as producer:
producer.publish(body=self.message, routing_key=manager.incoming_cfg.queue, headers={'title_number': 'DN1'})
logger.debug(self.message)
# Kill application; wait long enough for message to be stored.
# N.B.: 1 second may be insufficient, for a full coverage check during testing.
self.app.join(timeout=5)
self.app.terminate()
# Consume message from outgoing exchange.
self.consume(cfg=manager.outgoing_cfg)
self.assertEqual(self.message, self.payload)
def test_default_topic_keys(self):
""" Check that message with a suitable routing_key matches the default binding_key. """
# We don't need the app to be running for this test.
self.app.terminate()
self.message = make_message()
ROOT_KEY = 'feeder'
# Use default binding key for the queue that is created via setup_producer().
cfg = manager.outgoing_cfg
with manager.setup_producer(cfg=cfg) as producer:
routing_key = ROOT_KEY + '.test_default_topic_keys'
producer.publish(body=self.message, routing_key=routing_key, headers={'title_number': 'DN1'})
logger.debug(self.message)
# Consume message from outgoing exchange.
self.consume(cfg=cfg)
self.assertEqual(self.message, self.payload)
def test_valid_topic_keys(self):
""" Check that message with a suitable routing_key matches corresponding binding_key. """
# We don't need the app to be running for this test.
self.app.terminate()
self.message = make_message()
ROOT_KEY = 'feeder'
# Set binding key for the queue that is created via setup_producer().
cfg = manager.outgoing_cfg._replace(binding_key=ROOT_KEY+'.*')
with manager.setup_producer(cfg=cfg) as producer:
routing_key = ROOT_KEY + '.test_valid_topic_keys'
producer.publish(body=self.message, routing_key=routing_key, headers={'title_number': 'DN1'})
logger.debug(self.message)
# Consume message from outgoing exchange.
self.consume(cfg=cfg)
self.assertEqual(self.message, self.payload)
def test_invalid_topic_keys(self):
""" Check that message with a 'bad' routing_key does not match the queue's binding_key. """
# We don't need the app to be running for this test.
self.app.terminate()
self.message = make_message()
ROOT_KEY = 'feeder'
# Set binding key for the queue that is created via setup_producer().
cfg = manager.outgoing_cfg._replace(binding_key=ROOT_KEY+'.*')
with manager.setup_producer(cfg=cfg) as producer:
routing_key = 'FEEDER' + '.test_invalid_topic_keys'
producer.publish(body=self.message, routing_key=routing_key, headers={'title_number': 'DN1'})
logger.debug(self.message)
# Attempt to consume message from outgoing exchange; should time out.
try:
self.consume(cfg=cfg)
except socket.timeout:
pass
except Exception as e:
raise
def test_end_to_end(self, count=1):
""" Send message from dummy "System Of Record", then consume and check it. """
# Send a message to 'incoming' exchange - i.e. as if from source .
# import pdb; pdb.set_trace()
with manager.setup_producer(cfg=manager.incoming_cfg) as producer:
for n in range(count):
# Message to be sent.
self.message = make_message()
producer.publish(body=self.message, routing_key=manager.incoming_cfg.queue, headers={'title_number': 'DN1'})
logger.debug(self.message)
# Wait long enough message to be processed.
self.app.join(timeout=1)
# Consume message from outgoing exchange, via callback.
self.consume(cfg=manager.outgoing_cfg)
self.assertEqual(self.message, self.payload)
def test_multiple_end_to_end(self):
""" Check many messages. """
self.test_end_to_end(10)
if __name__ == '__main__':
unittest.main()