iron_mq 2.1.3 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_iron_mq.rb CHANGED
@@ -6,18 +6,17 @@ require_relative 'test_base'
6
6
  class IronMQTests < TestBase
7
7
  def setup
8
8
  super
9
-
9
+ @skip = @host.include? 'rackspace'
10
+ LOG.info "@host: #{@host}"
10
11
  queues = @client.queues.list
11
12
  p queues
12
-
13
13
  clear_queue()
14
-
15
14
  end
16
15
 
17
16
  def test_performance_post_100_messages
18
- @client.queue_name = 'test_basics_6'
17
+ @client.queue_name = 'test_perf_100'
19
18
  # slower to rackspace since this is running on aws
20
- timeout = @client.host.include?('rackspace') ? 40 : 12
19
+ timeout = @host.include?('rackspace') ? 40 : 12
21
20
  assert_performance timeout do
22
21
  100.times do
23
22
  @client.messages.post("hello world!")
@@ -27,7 +26,7 @@ class IronMQTests < TestBase
27
26
 
28
27
 
29
28
  def test_basics
30
- queue_name = 'test_basics_6'
29
+ queue_name = 'test_basics_7'
31
30
  @client.queue_name = queue_name
32
31
  clear_queue
33
32
 
@@ -108,6 +107,25 @@ class IronMQTests < TestBase
108
107
 
109
108
  end
110
109
 
110
+
111
+ def test_queues_list
112
+ queue_name = 'test_queues_list'
113
+ @client.queue_name = queue_name
114
+ clear_queue
115
+
116
+ res = @client.messages.post("hello world!")
117
+ p res
118
+
119
+ res = @client.queues.list
120
+ res.each do |q|
121
+ puts "#{q.name} and #{queue_name}";
122
+ if q.name == queue_name
123
+ assert_equal q.size, 1
124
+ end
125
+ end
126
+ end
127
+
128
+
111
129
  # TODO: pass :timeout in post/get messages and test those
112
130
  def test_timeout
113
131
  @client.queue_name = "test_timeout_6"
@@ -120,40 +138,45 @@ class IronMQTests < TestBase
120
138
  p msg
121
139
  assert msg
122
140
 
123
- msg4 = @client.messages.get()
124
- p msg4
125
- assert_nil msg4
126
-
127
- puts 'sleeping 45 seconds...'
128
- sleep 45
129
-
130
- msg3 = @client.messages.get()
131
- p msg3
132
- assert_nil msg3
133
-
134
- puts 'sleeping another 45 seconds...'
135
- sleep 45
136
-
137
- msg2 = @client.messages.get()
138
- assert msg2
139
- assert_equal msg2.id, msg.id
140
-
141
- msg2.delete
141
+ msg_nil = @client.messages.get()
142
+ p msg_nil
143
+ assert_nil msg_nil
144
+
145
+ tries = MAX_TRIES
146
+ while tries > 0
147
+ sleep 0.5
148
+ tries -= 1
149
+ sleep 1
150
+ new_msg = @client.messages.get()
151
+ p new_msg
152
+ next if new_msg.nil?
153
+ assert_equal new_msg.id, msg.id
154
+ new_msg.delete
155
+ break
156
+ end
157
+ assert_not_equal tries, 0
142
158
 
143
159
  # now try explicit timeout
144
- res = @client.messages.post("hello world timeout2!", :timeout => 10)
160
+ res = @client.messages.post("hello world timeout2!", :timeout => 5)
145
161
  p res
146
162
  msg = @client.messages.get()
147
163
  p msg
148
164
  assert msg
149
- msg4 = @client.messages.get()
150
- p msg4
151
- assert_nil msg4
152
- puts 'sleeping 15 seconds...'
153
- sleep 15
154
- msg2 = @client.messages.get()
155
- assert msg2
156
- assert_equal msg2.id, msg.id
165
+ msg_nil = @client.messages.get()
166
+ p msg_nil
167
+ assert_nil msg_nil
168
+
169
+ tries = MAX_TRIES
170
+ while tries > 0
171
+ sleep 0.5
172
+ tries -= 1
173
+ sleep 1
174
+ new_msg = @client.messages.get()
175
+ next if new_msg.nil?
176
+ assert_equal new_msg.id, msg.id
177
+ break
178
+ end
179
+ assert_not_equal tries, 0
157
180
 
158
181
  end
159
182
 
@@ -181,12 +204,6 @@ class IronMQTests < TestBase
181
204
  end
182
205
  assert_equal 0, res.size
183
206
 
184
-
185
- queue = @client.queue("test_basics_6")
186
- assert queue.name
187
- assert queue.size
188
-
189
-
190
207
  end
191
208
 
192
209
  def test_delay
@@ -195,14 +212,24 @@ class IronMQTests < TestBase
195
212
  clear_queue
196
213
  msgTxt = "testMessage-"+Time.now.to_s
197
214
  puts msgTxt
198
- @client.messages.post(msgTxt, {:delay => 10})
215
+ @client.messages.post(msgTxt, {:delay => 5})
199
216
  msg = @client.messages.get
200
217
  p msg
201
218
  assert_nil msg
202
- sleep 10
203
- msg = @client.messages.get
204
- p msg
205
- assert msg
219
+
220
+ tries = MAX_TRIES
221
+ while tries > 0
222
+ sleep 0.5
223
+ tries -= 1
224
+ sleep 1
225
+ msg = @client.messages.get
226
+ p msg
227
+ next if msg.nil?
228
+ assert_equal msg.body, msgTxt
229
+ break
230
+ end
231
+ assert_not_equal tries, 0
232
+
206
233
  end
207
234
 
208
235
  def test_batch
@@ -270,28 +297,42 @@ class IronMQTests < TestBase
270
297
  p msg
271
298
  assert_nil msg
272
299
 
273
- sleep 11
274
-
275
- msg = @client.messages.get
276
- p msg
277
- assert msg
300
+ tries = MAX_TRIES
301
+ while tries > 0
302
+ sleep 0.5
303
+ tries -= 1
304
+ sleep 1
305
+ msg = @client.messages.get
306
+ next if msg.nil?
307
+ p msg
308
+ assert_equal msg.id, msg_id
309
+ break
310
+ end
311
+ assert_not_equal tries, 0
278
312
 
279
313
  msg.release(:delay => 5)
280
314
  msg = @client.messages.get
281
315
  p msg
282
316
  assert_nil msg
283
317
 
284
- sleep 6
285
-
286
- msg = @client.messages.get
287
- p msg
288
- assert msg
289
-
318
+ tries = MAX_TRIES
319
+ while tries > 0
320
+ sleep 0.5
321
+ tries -= 1
322
+ sleep 1
323
+ msg = @client.messages.get
324
+ next if msg.nil?
325
+ p msg
326
+ assert_equal msg.id, msg_id
327
+ break
328
+ end
329
+ assert_not_equal tries, 0
290
330
  end
291
331
 
332
+
292
333
  def test_clear
293
334
 
294
- q = @client.queue("clearer_6")
335
+ q = @client.queue("test_clear_7")
295
336
 
296
337
  clear_queue(q.name)
297
338
 
@@ -312,8 +353,11 @@ class IronMQTests < TestBase
312
353
 
313
354
 
314
355
  def test_poll
315
- queue = @client.queue("test_poll_6")
316
- queue.clear
356
+ queue_name = "test_poll_6"
357
+ @client.queue_name = queue_name
358
+ clear_queue
359
+
360
+ queue = @client.queue(queue_name)
317
361
 
318
362
  v = "hello world"
319
363
  5.times do
@@ -328,7 +372,14 @@ class IronMQTests < TestBase
328
372
 
329
373
  assert_equal 5, i
330
374
 
331
- assert_equal 0, queue.reload.size
375
+ tries = MAX_TRIES
376
+ while tries > 0
377
+ tries -= 1
378
+ break if 0 == queue.reload.size
379
+ sleep 0.5
380
+ end
381
+ assert_not_equal tries, 0
382
+
332
383
 
333
384
  end
334
385
  #
@@ -351,6 +402,28 @@ class IronMQTests < TestBase
351
402
  #
352
403
  #end
353
404
 
405
+ def test_webhooks
406
+ omit_if @skip
407
+ puts "skip webhooks: #{@skip}"
408
+ qname ="webhook_queue"
409
+ path = "#{IronMQ::Messages.path(project_id: @client.project_id, queue_name: qname)}/webhook"
410
+ url = "#{@client.base_url}#{path}"
411
+ url << "?oauth=#{@client.token}"
412
+ p url
413
+
414
+ v = "hello webhook"
415
+
416
+ @rest = Rest::Client.new
417
+ p @rest.post(url, body: v)
418
+
419
+ queue = @client.queue(qname)
420
+ msg = queue.get
421
+ p msg
422
+ assert_equal v, msg.body
423
+
424
+
425
+ end
426
+
354
427
 
355
428
  end
356
429
 
@@ -0,0 +1,333 @@
1
+ # Put config.yml file in ~/Dropbox/configs/ironmq_gem/test/config.yml
2
+ require_relative 'test_base'
3
+ require 'logger'
4
+
5
+ class TestPushQueues < TestBase
6
+
7
+ def setup
8
+ super
9
+ @skip = @host.include? 'rackspace'
10
+ return if @skip # bypass these tests if rackspace
11
+ end
12
+
13
+ def make_key(i, t, random=0)
14
+ key = "pushq-#{t}-#{i}-#{random}"
15
+ end
16
+
17
+
18
+ def test_queue_subscriptions
19
+ omit_if @skip
20
+ types = ["multicast", "unicast"]
21
+ types.each do |t|
22
+
23
+ LOG.info "Trying type #{t}"
24
+
25
+ qname = "subscription-queue-#{t}"
26
+
27
+ num_subscribers = 10
28
+ subscribers = []
29
+
30
+ x = rand(1000)
31
+ num_subscribers.times do |i|
32
+ key = make_key(i, t, x)
33
+ subscribers << {url: "http://rest-test.iron.io/code/200?store=#{key}"}
34
+ end
35
+
36
+ queue = @client.queue(qname)
37
+ res = queue.update_queue(:subscribers => subscribers,
38
+ :push_type => t)
39
+ queue = @client.queue(qname)
40
+ LOG.debug queue
41
+ LOG.debug queue.subscribers
42
+ assert_equal num_subscribers, queue.subscribers.size
43
+
44
+ # add the last one
45
+ queue.reload # temporary, can remove
46
+ queue.add_subscriber({url: "http://nowhere.com"})
47
+ queue.reload
48
+ assert_equal num_subscribers + 1, queue.subscribers.size
49
+ queue.remove_subscriber({url: "http://nowhere.com"})
50
+ queue.reload
51
+ assert_equal num_subscribers, queue.subscribers.size
52
+
53
+ # todo: assert subscriptions match
54
+
55
+ msg = "hello #{x}"
56
+ m = queue.post(msg)
57
+
58
+ LOG.info "Checking results..."
59
+ @rest = Rest::Client.new
60
+ found = 0
61
+ if t == "multicast"
62
+ num_subscribers.times do |i|
63
+ key = make_key(i, t, x)
64
+ tries = MAX_TRIES
65
+ while tries > 0
66
+ tries -= 1
67
+ sleep 0.5
68
+ begin
69
+ url = "http://rest-test.iron.io/stored/#{key}"
70
+ LOG.info "checking url #{url}"
71
+ response = @rest.get(url)
72
+ parsed = JSON.parse(response.body)
73
+ LOG.debug parsed['body']
74
+ assert_equal msg, parsed['body']
75
+ found += 1
76
+ break
77
+ rescue Rest::HttpError => ex
78
+ LOG.debug ex.code
79
+ assert_equal 404, ex.code
80
+ end
81
+ end
82
+ assert_not_equal tries, 0
83
+ end
84
+ elsif t == "unicast"
85
+ tries = MAX_TRIES
86
+ while tries > 0
87
+ tries -= 1
88
+ sleep 0.5
89
+ num_subscribers.times do |i|
90
+ key = make_key(i, t, x)
91
+ begin
92
+ url = "http://rest-test.iron.io/stored/#{key}"
93
+ LOG.info "checking url #{url}"
94
+ response = @rest.get(url)
95
+ parsed = JSON.parse(response.body)
96
+ LOG.debug parsed['body']
97
+ assert_equal msg, parsed['body']
98
+ found += 1
99
+ break
100
+ rescue Rest::HttpError => ex
101
+ LOG.debug ex.code
102
+ assert_equal 404, ex.code
103
+ end
104
+ end
105
+ break if found == 1
106
+ end
107
+ assert_not_equal tries, 0
108
+ end
109
+
110
+ tries = MAX_TRIES
111
+ while tries > 0
112
+
113
+ # Need to wait > 60s here, because in case of retries on pusherd
114
+ # side (due lost connection for example) there will be no response earlier
115
+ # (default retries_delay is 60s).
116
+ sleep 1
117
+ tries -= 1
118
+ msg = queue.messages.get(m.id)
119
+ LOG.info "checking for message: #{msg}"
120
+ next if msg.nil?
121
+ subscribers = msg.subscribers
122
+
123
+ LOG.debug subscribers
124
+ if t == "unicast"
125
+ assert_equal 1, found
126
+ assert_equal 1, subscribers.size
127
+ else # pubsub
128
+ assert_equal num_subscribers, found
129
+ assert_equal num_subscribers, subscribers.size
130
+ end
131
+
132
+ do_retry = false
133
+ subscribers.each do |s|
134
+ LOG.debug s
135
+ LOG.info "status_code=#{s['status_code']}"
136
+ LOG.info "status=#{s['status']}"
137
+ do_retry = true unless 200 == s["status_code"]
138
+ do_retry = true unless "deleted" == s["status"]
139
+ end
140
+ next if do_retry
141
+ break
142
+ end
143
+ assert_not_equal tries, 0
144
+ end
145
+ end
146
+
147
+
148
+ def test_failure
149
+ omit_if @skip
150
+ @rest = Rest::Client.new
151
+ qname = "failure-queue"
152
+
153
+ x = rand(1000)
154
+
155
+ subscribers = []
156
+ subscribers << {url: "http://rest-test.iron.io/code/503?switch_after=2&switch_to=200&namespace=push-test-failures-#{x}"}
157
+ subscribers << {url: "http://rest-test.iron.io/code/503"}
158
+
159
+ num_subscribers = 2
160
+
161
+ queue = @client.queue(qname)
162
+ res = queue.update_queue(:subscribers => subscribers,
163
+ :push_type => "multicast",
164
+ :retries => 3,
165
+ :retries_delay => 3
166
+ )
167
+ queue = @client.queue(qname)
168
+ LOG.debug queue
169
+ LOG.debug queue.subscribers
170
+ assert_equal num_subscribers, queue.subscribers.size
171
+
172
+ msg = "hello #{x}"
173
+ m = queue.post(msg)
174
+ LOG.debug m
175
+
176
+ tries = MAX_TRIES
177
+ while tries > 0
178
+ sleep 0.5
179
+ tries -= 1
180
+ LOG.info 'getting status'
181
+ subscribers = queue.messages.get(m.id).subscribers
182
+ LOG.debug subscribers
183
+ LOG.info "num_subscribers=#{num_subscribers} subscribers.size=#{subscribers.size}"
184
+
185
+ assert_equal num_subscribers, subscribers.size
186
+ do_retry = false
187
+ subscribers.each do |s|
188
+ LOG.debug s
189
+ LOG.info "status_code=#{s['status_code']}"
190
+ LOG.info "status=#{s['status']}"
191
+ do_retry = true unless 503 == s["status_code"]
192
+ do_retry = true unless ["reserved", "retrying"].include? s["status"]
193
+ end
194
+ next if do_retry
195
+ break
196
+ end
197
+ assert_not_equal tries, 0
198
+
199
+ tries = MAX_TRIES
200
+ while tries > 0
201
+ sleep 0.5
202
+ tries -= 1
203
+ subscribers = queue.messages.get(m.id).subscribers
204
+ LOG.debug subscribers
205
+ assert_equal num_subscribers, subscribers.size
206
+ do_retry = false
207
+ subscribers.each do |s|
208
+ LOG.debug s
209
+ if s["url"] == "http://rest-test.iron.io/code/503"
210
+ do_retry = true unless 503 == s["status_code"]
211
+ do_retry = true unless "error" == s["status"]
212
+ else
213
+ do_retry = true unless 200 == s["status_code"]
214
+ do_retry = true unless "deleted" == s["status"]
215
+ end
216
+ end
217
+ next if do_retry
218
+ break
219
+ end
220
+ assert_not_equal tries, 0
221
+ end
222
+
223
+
224
+ def test_202
225
+ omit_if @skip
226
+ types = ["multicast"]
227
+ types.each do |t|
228
+
229
+ LOG.info "Trying type #{t}"
230
+
231
+ qname = "subscription-queue-#{t}-202"
232
+
233
+ num_subscribers = 2
234
+ subscribers = []
235
+
236
+ x = rand(1000)
237
+ num_subscribers.times do |i|
238
+ key = make_key(i, t, x)
239
+ subscribers << {url: "http://rest-test.iron.io/code/202?store=#{key}"}
240
+ end
241
+
242
+ queue = @client.queue(qname)
243
+ res = queue.update_queue(:subscribers => subscribers,
244
+ :push_type => t)
245
+ queue = @client.queue(qname)
246
+ LOG.debug queue
247
+ LOG.debug queue.subscribers
248
+ assert_equal num_subscribers, queue.subscribers.size
249
+ # todo: assert subscriptions match
250
+
251
+ msg = "hello #{x}"
252
+ m = queue.post(msg,
253
+ {:timeout => 2})
254
+
255
+ tries = MAX_TRIES
256
+ while tries > 0
257
+ sleep 0.5
258
+ tries -= 1
259
+ subscribers = queue.messages.get(m.id).subscribers
260
+ LOG.debug subscribers
261
+ assert_equal num_subscribers, subscribers.size
262
+ do_retry = false
263
+ subscribers.each do |s|
264
+ LOG.debug s
265
+ do_retry = true unless 202 == s["status_code"]
266
+ do_retry = true unless "reserved" == s["status"]
267
+ end
268
+ next if do_retry
269
+ break
270
+ end
271
+ assert_not_equal tries, 0
272
+
273
+ LOG.info 'sleeping 2'
274
+ sleep 2
275
+
276
+ tries = MAX_TRIES
277
+ while tries > 0
278
+ sleep 0.5
279
+ subscribers = queue.messages.get(m.id).subscribers
280
+ LOG.debug subscribers
281
+ assert_equal num_subscribers, subscribers.size
282
+
283
+ do_retry = false
284
+ subscribers.each do |s|
285
+ LOG.debug s
286
+ LOG.info "status_code=#{s['status_code']}"
287
+ LOG.info "status=#{s['status']}"
288
+
289
+ do_retry = true unless 202 == s["status_code"]
290
+ do_retry = true unless "reserved" == s["status"]
291
+ end
292
+ next if do_retry
293
+
294
+ # now let's delete it to say we're done with it
295
+ subscribers.each do |s|
296
+ LOG.debug s
297
+ LOG.info "status_code=#{s['status_code']}"
298
+ LOG.info "status=#{s['status']}"
299
+ LOG.info "Acking subscriber"
300
+ res = s.delete
301
+ LOG.debug res
302
+ end
303
+ break
304
+ end
305
+ assert_not_equal 0, tries
306
+
307
+ tries = MAX_TRIES
308
+ while tries > 0
309
+ sleep 0.5
310
+ tries -= 1
311
+ subscribers = queue.messages.get(m.id).subscribers
312
+ LOG.debug subscribers
313
+ next unless num_subscribers == subscribers.size
314
+
315
+ do_retry = false
316
+ subscribers.each do |s|
317
+ LOG.debug s
318
+ LOG.info "status=#{s['status']}"
319
+ do_retry = true unless "deleted" == s["status"]
320
+ end
321
+ next if do_retry
322
+ break
323
+ end
324
+ assert_not_equal 0, tries
325
+ end
326
+ end
327
+
328
+
329
+ def test_202_failure
330
+ omit_if @skip
331
+ end
332
+
333
+ end
data/test/test_tmp.rb ADDED
@@ -0,0 +1,17 @@
1
+ gem 'test-unit'
2
+ require 'test/unit'
3
+ require 'yaml'
4
+ require_relative 'test_base'
5
+
6
+ class IronMQTests < TestBase
7
+ def setup
8
+ super
9
+ end
10
+
11
+ def test_x()
12
+ list = @client.queues
13
+ p list
14
+ end
15
+
16
+
17
+ end