-
Notifications
You must be signed in to change notification settings - Fork 23
/
test.py
201 lines (167 loc) · 6.71 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
from iron_mq import *
import unittest
import random
import time
class TestIronMQ(unittest.TestCase):
def setUp(self):
self.mq = IronMQ()
self.random_number = str(int(random.random() * 10 ** 10))
def test_postMessage(self):
q = self.mq.queue("test_queue")
old_size = q.size()
q.post("test message")
self.assertEqual(old_size, q.size() - 1)
def test_touchMessage(self):
msg = "Test message %s" % time.time()
q = self.mq.queue("test_queue")
q.clear()
id = q.post(msg)
message = q.reserve()
response = q.touch(message["messages"][0]["id"], message["messages"][0]["reservation_id"])
self.assertEqual("Touched", response["msg"])
def test_touchMessageTwice(self):
msg = "Test message %s" % time.time()
q = self.mq.queue("test_queue")
q.clear()
id = q.post(msg)
message = q.reserve()
response = q.touch(message["messages"][0]["id"], message["messages"][0]["reservation_id"])
# use new reservation_id
response = q.touch(message["messages"][0]["id"], response["reservation_id"])
self.assertEqual("Touched", response["msg"])
def test_releaseMessage(self):
msg = "Test message %s" % time.time()
q = self.mq.queue("test_queue")
q.clear()
id = q.post(msg)
message = q.reserve()
response = q.release(message_id=message["messages"][0]["id"], reservation_id=message["messages"][0]["reservation_id"])
self.assertEqual("Released", response["msg"])
def test_addSubscribers(self):
queue_name = "test_queue%s" % time.time()
subscribers = [{'name': 'first', 'url': 'http://first.endpoint.xx/process' }]
self.mq.create_queue(queue_name, {'push': {'subscribers': subscribers}} )
q = self.mq.queue(queue_name)
response = q.add_subscribers(*[{'name': 'second', 'url': 'http://first.endpoint.xx/process'}])
self.assertEqual(response["msg"], "Updated")
info = q.info()
self.assertEqual(2, len(info['push']['subscribers']))
def test_removeSubscribers(self):
queue_name = "test_queue%s" % time.time()
subscribers = [{'name': 'first',
'url': 'http://first.endpoint.xx/process'},
{'name': 'second',
'url': 'http://second.endpoint.xx/process'}]
self.mq.create_queue(queue_name, {'push': {'subscribers': subscribers}} )
q = self.mq.queue(queue_name)
response = q.remove_subscribers(*['first'])
self.assertEqual(response["msg"], "Updated")
info = q.info()
self.assertEqual(1, len(info['push']['subscribers']))
def test_getMessage(self):
msg = "%s" % time.time()
q = self.mq.queue("test_queue")
q.clear()
q.post(msg)
message = q.get()
message = "%s" % message["messages"][0]["body"]
self.assertEqual(msg, message)
def test_reserveMessages(self):
msg = "%s" % time.time()
q = self.mq.queue("test_queue")
q.clear()
ids = q.post(msg + str(0), msg + str(1), msg + str(2))
messages = q.reserve(3)
for idx, val in enumerate(messages["messages"]):
self.assertEqual(msg + str(idx), val["body"])
self.assertEqual(ids["ids"][idx], val["id"])
self.assertTrue(len(val["reservation_id"]) > 0)
self.assertEqual(val["reserved_count"], 1)
def test_reserveMessage(self):
msg = "Test message %s" % time.time()
q = self.mq.queue("test_queue")
q.clear()
id = q.post(msg)
message = q.reserve()
self.assertEqual(msg, message["messages"][0]["body"])
self.assertEqual(id["ids"][0], message["messages"][0]["id"])
def test_getMessageTimeout(self):
msg = "%s" % time.time()
q = self.mq.queue("test_queue")
q.clear()
q.post(msg)
message = q.get(timeout=180)
message = "%s" % message["messages"][0]["body"]
self.assertEqual(msg, message)
time.sleep(120)
m2 = q.get()
self.assertEqual(0, len(m2["messages"]))
time.sleep(120)
m3 = q.get()
self.assertEqual(msg, message)
def test_deleteMessage(self):
q = self.mq.queue("test_queue")
size = q.size()
msg = q.post("test")
self.assertEqual(size, q.size() - 1)
q.delete(msg["ids"][0])
self.assertEqual(size, q.size())
def test_deleteReservedMessage(self):
msg = "%s" % time.time()
q = self.mq.queue("test_queue")
q.clear()
id = q.post(msg)
message = q.reserve(1, 60)
self.assertEqual(1, q.size())
reservation_id = message["messages"][0]["reservation_id"]
q.delete(id["ids"][0], reservation_id)
self.assertEqual(0, q.size())
def test_clearQueue(self):
q = self.mq.queue("test_queue")
q.post("%s" % time.time())
self.assertTrue(q.size() > 0)
q.clear()
self.assertEqual(q.size(), 0)
def test_deprecated(self):
self.assertEqual(self.mq.queues(), self.mq.getQueues())
name = 'test_queue'
q = self.mq.queue(name)
q.clear()
self.assertEqual('Cleared', self.mq.clearQueue(name)['msg'])
self.assertEqual(0, q.size())
info = self.mq.getQueueDetails(name)
self.assertEqual(q.info(), info)
msg_id = self.mq.postMessage(name, ['hello mq'])['ids'][0]
resp = self.mq.getMessage(name)
self.assertEqual(msg_id, resp['messages'][-1]['id'])
def test_postAndDeleteMultipleMessages(self):
q = self.mq.queue("test_queue")
q.clear()
old_size = q.size()
ids = q.post("more", "and more")["ids"]
self.assertEqual(old_size, q.size() - 2)
q.delete_multiple(ids=ids)
self.assertEqual(old_size, q.size())
def test_deleteMultipleReservedMessages(self):
q = self.mq.queue("test_queue")
q.clear()
old_size = q.size()
q.post("more", "and more")
messages = q.reserve(2, 60)
self.assertEqual(old_size, q.size() - 2)
q.delete_multiple(messages=messages)
self.assertEqual(old_size, q.size())
def test_getMessageById(self):
body = "%s" % time.time()
q = self.mq.queue("test_queue")
response_post = q.post(body)
message = q.get_message_by_id(response_post["ids"][0])
self.assertEqual(body, message["body"])
def test_peekMessages(self):
q = self.mq.queue("test_queue")
q.clear()
q.post("more", "and more")
response = q.peek(2)
self.assertEqual(2, len(response["messages"]))
if __name__ == '__main__':
unittest.main()