iron_mq 5.0.1 → 6.0.0.pre1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,7 +13,7 @@ module IronMQ
13
13
  end
14
14
 
15
15
  def id
16
- @raw["id"]
16
+ @raw['id']
17
17
  end
18
18
 
19
19
  def [](key)
@@ -21,7 +21,7 @@ module IronMQ
21
21
  end
22
22
 
23
23
  def msg
24
- @raw["msg"]
24
+ @raw['msg']
25
25
  end
26
26
  #
27
27
  #def raw
@@ -10,6 +10,10 @@ module IronMQ
10
10
  @options = options
11
11
  end
12
12
 
13
+ def name
14
+ @raw['name']
15
+ end
16
+
13
17
  def url
14
18
  @raw['url']
15
19
  end
@@ -20,7 +24,8 @@ module IronMQ
20
24
 
21
25
  # `options` was kept for backward compatibility
22
26
  def delete(options = {})
23
- @message.call_api_and_parse_response(:delete, path)
27
+ @message.call_api_and_parse_response(:delete, path,
28
+ {subscriber_name: name})
24
29
  rescue Rest::HttpError => ex
25
30
  #if ex.code == 404
26
31
  # Rest.logger.info("Delete got 404, safe to ignore.")
@@ -36,7 +41,7 @@ module IronMQ
36
41
  private
37
42
 
38
43
  def path
39
- "/subscribers/#{id}"
44
+ "/subscribers/#{name}"
40
45
  end
41
46
  end
42
47
 
@@ -1,4 +1,4 @@
1
1
  module IronMQ
2
- VERSION = "5.0.1"
2
+ VERSION = "6.0.0.pre1"
3
3
  end
4
4
 
@@ -0,0 +1,33 @@
1
+ require 'tmpdir'
2
+ require 'rake/testtask'
3
+
4
+ Dir.chdir(File.dirname(__FILE__) + '/..')
5
+
6
+ Rake::TestTask.new do |t|
7
+ if ENV['NEW_PROJECT']
8
+ require_relative '../lib/iron_worker_ng.rb'
9
+
10
+ client = IronWorkerNG::Client.new
11
+ name = 'IWtest ' + Time.now.strftime('%b %-d %T')
12
+ resp = client.api.post('projects', name: name)
13
+ res = JSON.parse(resp.body)
14
+ raise "Failed to create new project: #{res}" unless
15
+ res['msg'].start_with? 'Project Created'
16
+
17
+ ENV['IRON_PROJECT_ID'] = res['id']
18
+ end
19
+ if ENV['IRON_PROJECT_ID']
20
+ t.options = "-- --project-id=#{ENV['IRON_PROJECT_ID']}"
21
+ end
22
+
23
+ t.libs << "lib"
24
+
25
+ files = FileList['test/**/test_*.rb']
26
+ t.test_files = files.keep_if do |f|
27
+ f =~ Regexp.new(ENV['TESTP'] || '') and
28
+ not ( r = ENV['EXCLP'] and
29
+ f =~ Regexp.new(r) )
30
+ end
31
+
32
+ t.verbose = true
33
+ end
@@ -27,8 +27,8 @@ class QuickRun < TestBase
27
27
  puts "post"
28
28
  res = queue.post(body)
29
29
  # p res
30
- assert_not_nil res
31
- assert_not_nil res.id
30
+ refute_nil res
31
+ refute_nil res.id
32
32
  post_id = res.id
33
33
  assert !(res.msg.nil? || res.msg.empty?)
34
34
  end
@@ -36,7 +36,7 @@ class QuickRun < TestBase
36
36
  quicky.time(:get) do
37
37
  puts "get"
38
38
  msg = queue.get
39
- assert_not_nil msg.id
39
+ refute_nil msg.id
40
40
  assert_equal msg.id, post_id
41
41
  assert !(msg.body.nil? || msg.body.empty?)
42
42
  assert_equal body, msg.body
@@ -46,7 +46,7 @@ class QuickRun < TestBase
46
46
  puts "delete"
47
47
  res = queue.delete(post_id)
48
48
  # p res
49
- assert_not_nil res
49
+ refute_nil res
50
50
  assert !(res.msg.nil? || res.msg.empty?)
51
51
  end
52
52
 
@@ -59,8 +59,8 @@ class QuickRun < TestBase
59
59
  quicky.time(:post) do
60
60
  res = queue.post("hello world!")
61
61
  # p res
62
- assert_not_nil res
63
- assert_not_nil res.id
62
+ refute_nil res
63
+ refute_nil res.id
64
64
  post_id = res.id
65
65
  assert !(res.msg.nil? || res.msg.empty?)
66
66
  end
@@ -17,61 +17,69 @@ class TestAlerts < TestBase
17
17
 
18
18
  # no configuration
19
19
  alert = {}
20
- assert_raise(Rest::HttpError) { queue.add_alert(alert) }
20
+ assert_raises(Rest::HttpError) { queue.add_alert(alert) }
21
21
 
22
22
  # only type is specified
23
23
  alert[:type] = 'fixed'
24
- assert_raise(Rest::HttpError) { queue.add_alert(alert) }
24
+ assert_raises(Rest::HttpError) { queue.add_alert(alert) }
25
25
 
26
26
  # type and trigger value specified
27
27
  alert[:trigger] = 30
28
- assert_raise(Rest::HttpError) { queue.add_alert(alert) }
28
+ assert_raises(Rest::HttpError) { queue.add_alert(alert) }
29
29
 
30
30
  # type, trigger, and direction
31
31
  alert[:direction] = 'asc'
32
- assert_raise(Rest::HttpError) { queue.add_alert(alert) }
32
+ assert_raises(Rest::HttpError) { queue.add_alert(alert) }
33
33
 
34
34
  # type, trigger, direction, and alert queue name - alright
35
35
  aq_name = 'bad-alerts-params-alerts'
36
36
  alert[:queue] = aq_name
37
37
  assert_nothing_raised(Rest::HttpError) { queue.add_alert(alert) }
38
38
 
39
- # type, trigger, direction, queue name, and snooze - alright
39
+ # type, trigger, direction, queue name, and snooze - alert duplication
40
40
  alert[:snooze] = 8
41
+ assert_raises(Rest::HttpError) { queue.add_alert(alert) }
42
+
43
+ # alright, no alert duplication
44
+ alert[:trigger] = 50
41
45
  assert_nothing_raised(Rest::HttpError) { queue.add_alert(alert) }
46
+ queue.clear_alerts
42
47
 
43
48
  # wrong snooze
44
49
  alert[:snooze] = -13
45
- assert_raise(Rest::HttpError) { queue.add_alert(alert) }
50
+ assert_raises(Rest::HttpError) { queue.add_alert(alert) }
46
51
 
47
52
  alert[:snooze] = '1234'
48
- assert_raise(Rest::HttpError) { queue.add_alert(alert) }
53
+ assert_raises(Rest::HttpError) { queue.add_alert(alert) }
49
54
 
50
55
  # wrong type
51
56
  alert[:snooze] = 0
52
57
  alert[:type] = 'wrong'
53
- assert_raise(Rest::HttpError) { queue.add_alert(alert) }
58
+ assert_raises(Rest::HttpError) { queue.add_alert(alert) }
54
59
 
55
60
  # wrong trigger
56
61
  alert[:type] = 'progressive'
57
62
  alert[:trigger] = 'c'
58
- assert_raise(Rest::HttpError) { queue.add_alert(alert) }
63
+ assert_raises(Rest::HttpError) { queue.add_alert(alert) }
59
64
 
60
65
  # wrong direction
61
66
  alert[:trigger] = 30
62
67
  alert[:direction] = 'both'
63
- assert_raise(Rest::HttpError) { queue.add_alert(alert) }
68
+ assert_raises(Rest::HttpError) { queue.add_alert(alert) }
64
69
 
65
- # delete queue to clear
66
- delete_queues queue
70
+ # default direction is "asc"
71
+ alert.delete(:direction)
72
+ assert_nothing_raised(Rest::HttpError) { queue.add_alert(alert) }
73
+
74
+ queue.clear_alerts
67
75
 
68
76
  # Test max alerts number
69
77
  alert[:direction] = 'asc'
70
78
  # insert 6 alerts
71
- assert_raise(Rest::HttpError) { queue.add_alerts(Array.new(6, alert)) }
79
+ assert_raises(Rest::HttpError) { queue.add_alerts(Array.new(6, alert)) }
72
80
 
73
81
  queue.add_alerts(Array.new(5, alert))
74
- assert_raise(Rest::HttpError) { queue.add_alert(alert) }
82
+ assert_raises(Rest::HttpError) { queue.add_alert(alert) }
75
83
 
76
84
  delete_queues queue
77
85
 
@@ -79,14 +87,33 @@ class TestAlerts < TestBase
79
87
  # Progressive alert posts to its queue (queue is the same as alert queue)
80
88
  alert[:queue] = q_name
81
89
  alert[:type] = 'progressive'
82
- assert_raise(Rest::HttpError) { queue.add_alert(alert) }
90
+ assert_raises(Rest::HttpError) { queue.add_alert(alert) }
83
91
 
84
92
  # Q1 progressive alert posts to Q2, Q2 progressive alert posts to Q1
85
93
  a_queue = @client.queue aq_name
94
+ delete_queues a_queue
95
+
86
96
  queue.add_alert(alert.merge({:queue => aq_name}))
87
- assert_raise(Rest::HttpError) do
97
+ assert_raises(Rest::HttpError) do
88
98
  a_queue.add_alert(alert.merge({:queue => q_name}))
89
99
  end
100
+
101
+ delete_queues queue, a_queue
102
+
103
+ # Push queues have no alerts feature
104
+ queue = @client.queue 'push_queue'
105
+ delete_queues queue
106
+
107
+ queue.add_subscriber({ :url => 'http://iron.io/receiver' })
108
+ assert_raises(Rest::HttpError) { queue.add_alert(alert) }
109
+
110
+ delete_queues queue
111
+
112
+ assert_nothing_raised(Rest::HttpError) { queue.add_alert(alert) }
113
+ # conversion queue with alerts to push queue must raise exception
114
+ assert_raises(Rest::HttpError) { queue.update({ :push_type => 'multicast' }) }
115
+
116
+ delete_queues queue
90
117
  end
91
118
 
92
119
  def test_size_alerts
@@ -141,15 +168,19 @@ class TestAlerts < TestBase
141
168
 
142
169
  # Trigger alert
143
170
  post_messages(queue, trigger + 1)
144
- to_time = Time.now + snooze - 4
171
+ to_time = Time.now + snooze - 2
145
172
  assert_queue_size 1, alert_queue
146
173
 
147
- while (lambda { Time.now }).call < to_time do
174
+ while true do
148
175
  delete_messages(queue, 2) # queeu size is `trigger - 1`
149
176
  post_messages(queue, 2) # size is `trigger + 1`
150
- assert_queue_size 1, alert_queue
177
+ if Time.now < to_time
178
+ assert_queue_size 1, alert_queue
179
+ else
180
+ break
181
+ end
151
182
  end
152
- sleep 4
183
+ sleep 2
153
184
 
154
185
  delete_messages(queue, 2) # queeu size is `trigger - 1`
155
186
  post_messages(queue, 2) # size is `trigger - 1`
@@ -163,15 +194,19 @@ class TestAlerts < TestBase
163
194
  # Trigger alert
164
195
  post_messages(queue, trigger + 1)
165
196
  delete_messages(queue, 2)
166
- to_time = Time.now + snooze - 4
197
+ to_time = Time.now + snooze - 2
167
198
  assert_queue_size 1, alert_queue
168
199
 
169
- while (lambda { Time.now }).call < to_time do
200
+ while true do
170
201
  post_messages(queue, 2) # queeu size is `trigger + 1`
171
202
  delete_messages(queue, 2) # size is `trigger - 1`
172
- assert_queue_size 1, alert_queue
203
+ if Time.now < to_time
204
+ assert_queue_size 1, alert_queue
205
+ else
206
+ break
207
+ end
173
208
  end
174
- sleep 4
209
+ sleep 2
175
210
 
176
211
  post_messages(queue, 2) # size is `trigger + 1`
177
212
  delete_messages(queue, 2) # queeu size is `trigger - 1`
@@ -235,11 +270,15 @@ class TestAlerts < TestBase
235
270
  # Check queue for alert
236
271
  assert_queue_size 1, alert_queue
237
272
 
238
- while (lambda { Time.now }).call < to_time do
273
+ while true do
239
274
  # will trigger alert if snooze does not work
240
275
  post_messages(queue, trigger + 1)
241
276
  # but must not trigger
242
- assert_queue_size 1, alert_queue
277
+ if Time.now < to_time
278
+ assert_queue_size 1, alert_queue
279
+ else
280
+ break
281
+ end
243
282
  end
244
283
  sleep 4
245
284
 
@@ -250,18 +289,24 @@ class TestAlerts < TestBase
250
289
  delete_queues(queue, alert_queue)
251
290
 
252
291
  # test descending alert with snooze
253
- queue, alert_queue = clear_queue_add_alert(type, 2, 'desc', snooze)
292
+ queue, alert_queue = clear_queue_add_alert(type, 1, 'desc', snooze)
254
293
 
255
294
  # Does not trigger alert
256
295
  post_messages(queue, 20 * trigger)
257
- to_time = Time.now + snooze - 4
296
+ to_time = Time.now + snooze - 3
258
297
 
259
- while (lambda { Time.now }).call < to_time do
298
+ while true do
260
299
  delete_messages(queue, trigger + 1)
261
- assert_queue_size 1, alert_queue
300
+ amsgs = alert_queue.peek({ :n => 10 })
301
+ puts amsgs.map { |am| am.body }.join("\n")
302
+ if Time.now < to_time
303
+ assert_queue_size 1, alert_queue
304
+ else
305
+ break
306
+ end
262
307
  break if get_queue_size(queue) <= trigger
263
308
  end
264
- sleep 4
309
+ sleep 3
265
310
 
266
311
  post_messages(queue, trigger + 1)
267
312
  delete_messages(queue, trigger)
@@ -293,7 +338,7 @@ class TestAlerts < TestBase
293
338
  def delete_queues(*queues)
294
339
  queues.each do |q|
295
340
  begin
296
- q.delete_queue
341
+ q.delete
297
342
  rescue Rest::HttpError => ex
298
343
  if ex.code == 404
299
344
  Rest.logger.info("Delete queue got 404, ignoring.")
@@ -323,6 +368,7 @@ class TestAlerts < TestBase
323
368
  puts "Try to trigger descending alert... delete #{nmsgs} messages"
324
369
  delete_messages(queue, nmsgs)
325
370
  end
371
+ sleep 1
326
372
  assert_queue_size aq_size, alert_queue, 'Alert is triggered, but must not be'
327
373
 
328
374
  if qsize < trigger
@@ -332,6 +378,7 @@ class TestAlerts < TestBase
332
378
  puts "Delete #{1 + overhead} more message(s)"
333
379
  delete_messages(queue, 1 + overhead)
334
380
  end
381
+ sleep 1
335
382
  assert_queue_size aq_size + 1, alert_queue, 'Alert is not triggered, but must be'
336
383
  end
337
384
 
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
- require 'test/unit'
2
+ gem 'minitest'
3
+ require 'minitest/autorun'
3
4
  require 'yaml'
4
5
  require 'uber_config'
5
6
 
@@ -14,7 +15,7 @@ LOG = Logger.new(STDOUT)
14
15
  LOG.level = Logger::INFO
15
16
  MAX_TRIES = 10
16
17
 
17
- class TestBase < Test::Unit::TestCase
18
+ class TestBase < Minitest::Test
18
19
 
19
20
  def setup
20
21
  puts 'setup'
@@ -27,8 +28,8 @@ class TestBase < Test::Unit::TestCase
27
28
 
28
29
  @client = IronMQ::Client.new(@config['iron'])
29
30
  puts "IronMQ::VERSION = #{IronMQ::VERSION}"
30
- #Rest.logger.level = Logger::DEBUG # this doesn't work for some reason?
31
- #IronCore::Logger.logger.level = Logger::DEBUG
31
+ # Rest.logger.level = Logger::DEBUG # this doesn't work for some reason?
32
+ # IronCore::Logger.logger.level = Logger::DEBUG
32
33
 
33
34
  @queue_name = 'ironmq-ruby-tests' # default queue for tests
34
35
  end
@@ -46,6 +47,7 @@ class TestBase < Test::Unit::TestCase
46
47
  rescue Rest::HttpError => ex
47
48
  if ex.code == 404
48
49
  # this is fine
50
+ puts '404 ON CLEAR!!'
49
51
  else
50
52
  raise ex
51
53
  end
@@ -10,7 +10,7 @@ class TestBulk < TestBase
10
10
  def test_bulk
11
11
  LOG.info "test_bulk"
12
12
 
13
- q_name = 'ironmq-gem-bulk'
13
+ q_name = "ironmq-gem-bulk#{Time.now.to_i}"
14
14
  queue = @client.queue(q_name)
15
15
 
16
16
  times = 50
@@ -7,36 +7,6 @@ class IronMQTests < TestBase
7
7
  super
8
8
  LOG.info "@host: #{@host}"
9
9
 
10
- queues = @client.queues.list
11
- # p queues
12
- end
13
-
14
- def test_performance_post_100_messages
15
- queue = @client.queue('test_perf_100')
16
- # slower to rackspace since this is running on aws
17
- timeout = @host.include?('rackspace') ? 40 : 12
18
-
19
- assert_performance(timeout) do
20
- 100.times do
21
- queue.post("hello world!")
22
- end
23
-
24
- # delete queue on test complete
25
- resp = queue.delete_queue
26
- assert_equal 200, resp.code, "API must response with HTTP 200 status, but returned HTTP #{resp.code}"
27
- end
28
- end
29
-
30
- def test_get
31
- queue_name = "some_queue_abcj9u23"
32
- queue = @client.queue(queue_name)
33
- msg = queue.get
34
- v = "hello big world"
35
- queue.post(v)
36
- msg = queue.get
37
- msg.delete
38
- msg = queue.get
39
-
40
10
  end
41
11
 
42
12
  def test_basics
@@ -44,8 +14,7 @@ class IronMQTests < TestBase
44
14
  clear_queue(queue_name)
45
15
 
46
16
  # NOTE: Kept for backward compatibility checking
47
- queue = @client.queues.get(:name => queue_name)
48
- # p queue
17
+ queue = @client.queue(queue_name)
49
18
  res = queue.post("hello world!")
50
19
  # p res
51
20
 
@@ -53,17 +22,16 @@ class IronMQTests < TestBase
53
22
  assert res.id
54
23
  assert res.msg
55
24
  sleep 0.3
56
- assert_equal 1, queue.size
25
+ assert_equal 1, queue.reload.size
57
26
 
58
- message = queue.get
27
+ message = queue.reserve
59
28
  # p res
60
29
  assert res["id"]
61
30
  assert res.id
62
31
 
63
- res = queue.delete(res["id"])
32
+ res = queue.delete(res["id"], message.reservation_id)
64
33
  # p res
65
- puts "shouldn't be any more"
66
- res = queue.get
34
+ res = queue.reserve
67
35
  # p res
68
36
  assert_nil res
69
37
 
@@ -73,15 +41,14 @@ class IronMQTests < TestBase
73
41
  res = queue.post("hello world 2!")
74
42
  # p res
75
43
 
76
- msg = queue.get
44
+ msg = queue.reserve
77
45
  # p msg
78
46
  assert msg
79
47
 
80
48
  res = msg.delete
81
49
  #p res
82
50
 
83
- puts "shouldn't be any more"
84
- res = queue.get
51
+ res = queue.reserve
85
52
  # p res
86
53
  assert_nil res
87
54
 
@@ -92,16 +59,16 @@ class IronMQTests < TestBase
92
59
  # p res
93
60
  assert res.msg
94
61
 
95
- res = queue.get
96
- # p res
62
+ res = queue.reserve
63
+ puts "queue.reserve got: #{res.body}"
64
+ p res
97
65
  assert res["id"]
98
66
  assert res.id
99
67
  assert_equal v, res.body
100
68
 
101
- res = queue.delete(res.id)
102
- # p res
103
- puts "shouldn't be any more"
104
- res = queue.get
69
+ res = queue.delete(res.id, res.reservation_id)
70
+
71
+ res = queue.reserve
105
72
  # p res
106
73
  assert_nil res
107
74
 
@@ -110,14 +77,13 @@ class IronMQTests < TestBase
110
77
  # p res
111
78
  assert res.msg
112
79
 
113
- res = queue.get
80
+ res = queue.reserve
114
81
  # p res
115
82
  assert res.body
116
83
 
117
84
  res = res.delete
118
85
  # p res
119
- puts "shouldn't be any more"
120
- res = queue.get
86
+ res = queue.reserve
121
87
  # p res
122
88
  assert_nil res
123
89
 
@@ -126,7 +92,10 @@ class IronMQTests < TestBase
126
92
  assert_equal 200, resp.code, "API must response with HTTP 200 status, but returned HTTP #{resp.code}"
127
93
  end
128
94
 
95
+
96
+
129
97
  def test_multi_delete
98
+ puts 'test_multi_delete'
130
99
  queue_name = 'test_multi_delete_41'
131
100
  clear_queue(queue_name)
132
101
 
@@ -134,7 +103,7 @@ class IronMQTests < TestBase
134
103
  ids = []
135
104
  10.times do |i|
136
105
  msg = queue.post("hello #{i}")
137
- ids << msg.id
106
+ ids << {id: msg.id}
138
107
  end
139
108
  sleep 0.5
140
109
  assert_equal 10, queue.reload.size
@@ -142,10 +111,42 @@ class IronMQTests < TestBase
142
111
  queue.delete_messages(ids)
143
112
  sleep 1
144
113
  assert_equal 0, queue.reload.size
114
+
115
+ # now try it with reserved messages
116
+ ids = []
117
+ 10.times do |i|
118
+ msg = queue.post("hello #{i}")
119
+ end
120
+ sleep 0.5
121
+ assert_equal 10, queue.reload.size
122
+ while (msg = queue.reserve) != nil do
123
+ ids << {id: msg.id, reservation_id: msg.reservation_id}
124
+ end
125
+ queue.delete_messages(ids)
126
+ sleep 1
127
+ assert_equal 0, queue.reload.size
128
+
145
129
  queue.delete_queue
146
130
 
147
131
  end
148
132
 
133
+ def test_reservation_ids
134
+ puts 'test_reservation_ids'
135
+ # get a message, let it timeout, then try to delete it. That should fail.
136
+ queue_name = 'test_res_ids'
137
+ clear_queue(queue_name)
138
+
139
+ queue = @client.queue(queue_name)
140
+ msg = queue.post("hello")
141
+ msg = queue.reserve(:timeout=>3)
142
+ sleep 3
143
+ ex = assert_raises Rest::HttpError do
144
+ msg.delete
145
+ end
146
+ assert_equal 403, ex.code
147
+
148
+ end
149
+
149
150
  def test_queues_list
150
151
  queue_name = 'test_queues_list_1'
151
152
  clear_queue(queue_name)
@@ -190,42 +191,43 @@ class IronMQTests < TestBase
190
191
 
191
192
  sleep 61 # should be 1 minute timeout by default
192
193
  new_msg = queue.get
193
- assert_not_nil new_msg
194
+ refute_nil new_msg
194
195
  assert_equal new_msg.id, msg.id
195
196
  new_msg.delete
196
197
 
197
198
  # now try explicit timeout
198
- res = queue.post("hello world timeout2!", :timeout => 30)
199
- # p res
200
- msg = queue.get
199
+ timeout = 5
200
+ res = queue.post("hello world timeout2!", :timeout => timeout)
201
+ # p resds
202
+ msg = queue.get(:timeout=>timeout)
201
203
  # p msg
202
204
  assert msg
203
- assert_equal 30, msg.timeout
205
+ # assert_equal 30, msg.timeout - removed in v3
204
206
 
205
- msg_nil = queue.get
207
+ msg_nil = queue.get(:timeout=>timeout)
206
208
  # p msg_nil
207
209
  assert_nil msg_nil
208
210
 
209
- sleep 31
210
- new_msg = queue.get
211
- assert_not_nil new_msg
211
+ sleep timeout + 1
212
+ new_msg = queue.get(:timeout=>timeout)
213
+ refute_nil new_msg
212
214
  assert_equal new_msg.id, msg.id
213
215
  new_msg.delete
214
216
 
215
217
  # timeout on get
216
218
  res = queue.post("hello world timeout3!")
217
- msg = queue.get(:timeout => 30)
218
- # puts "MESSAGE IS #{msg.inspect}"
219
+ msg = queue.get(:timeout => timeout)
220
+ # puts "MESSAGE IS #{msg.inspect}"
219
221
  assert msg
220
- assert_equal msg.timeout, 30
222
+ # assert_equal msg.timeout, 30 - removed in v3
221
223
 
222
- msg_nil = queue.get
224
+ msg_nil = queue.get(:timeout=>timeout)
223
225
  # p msg_nil
224
226
  assert_nil msg_nil
225
227
 
226
- sleep 31
228
+ sleep timeout+1
227
229
  new_msg = queue.get
228
- assert_not_nil new_msg
230
+ refute_nil new_msg
229
231
  assert_equal new_msg.id, msg.id
230
232
  new_msg.delete
231
233
 
@@ -234,39 +236,21 @@ class IronMQTests < TestBase
234
236
  assert_equal 200, resp.code, "API must response with HTTP 200 status, but returned HTTP #{resp.code}"
235
237
  end
236
238
 
239
+ # todo: This test should be changed to make queues, it assumes 30+ queues are already created.
237
240
  def test_queues
238
- puts 'test_queues'
241
+ puts "test_queues-#{Time.now.to_i}"
239
242
 
240
243
  qname = "some_queue_that_does_not_exist_1"
241
244
  queue = @client.queue(qname)
242
- # delete it before the test
243
- queue.delete_queue
244
-
245
- assert_raise Rest::HttpError do
246
- # should raise a 404
247
- m = queue.size
248
- end
249
-
250
245
  # create at least one queue
251
246
  queue.post('create queue message')
252
247
  # queue should exist now
253
248
  m = queue.get
254
- assert_not_nil m
249
+ refute_nil m
255
250
 
256
- res = @client.queues.list
251
+ res = @client.queues.list(page: 1, per_page: 30)
257
252
  # puts "res.size: #{res.size}"
258
- res.each do |q|
259
- # puts "queue_name: " + q.name
260
- # puts "queue size: " + q.size.to_s
261
- assert q.size >= 0
262
- end
263
- assert res.size > 0
264
-
265
- res = @client.queues.list(:page => 50)
266
- # puts "res.size 2: #{res.size}"
267
- # res.each do |q| { p q.name }
268
-
269
- assert_equal 0, res.size
253
+ assert_equal 30, res.size
270
254
 
271
255
  # delete queue on test complete
272
256
  resp = queue.delete_queue
@@ -289,7 +273,7 @@ class IronMQTests < TestBase
289
273
 
290
274
  sleep 6
291
275
  new_msg = queue.get
292
- assert_not_nil new_msg
276
+ refute_nil new_msg
293
277
  assert_equal msg_id, new_msg.id
294
278
  new_msg.delete
295
279
 
@@ -327,7 +311,7 @@ class IronMQTests < TestBase
327
311
  assert msgs.is_a?(Array)
328
312
  msgs.each do |m|
329
313
  puts m.id
330
- assert_not_equal msg.id, m.id
314
+ refute_equal msg.id, m.id
331
315
  end
332
316
  assert msgs.size == 9, "size should be 9, but it's #{msgs.size}"
333
317
  assert msgs[0]["id"]
@@ -363,11 +347,11 @@ class IronMQTests < TestBase
363
347
  queue.post("third message")
364
348
 
365
349
  msg = queue.peek
366
- assert_not_nil msg
350
+ refute_nil msg
367
351
  assert_equal "first message", msg.body, "message body must be 'first message', but it's '#{msg.body}'"
368
352
 
369
353
  msg = queue.peek
370
- assert_not_nil msg
354
+ refute_nil msg
371
355
  assert_equal "first message", msg.body, "message body must be 'first message', but it's '#{msg.body}'"
372
356
 
373
357
  msgs = queue.peek(:n => 2)
@@ -375,7 +359,7 @@ class IronMQTests < TestBase
375
359
  assert_equal 2, msgs.size, "must received 2 messages, but received #{msgs.size}"
376
360
 
377
361
  msg = queue.peek
378
- assert_not_nil msg
362
+ refute_nil msg
379
363
  assert_equal "first message", msg.body, "message body must be 'first message', but it's '#{msg.body}'"
380
364
 
381
365
  msgs = queue.peek(:n => 7)
@@ -383,14 +367,14 @@ class IronMQTests < TestBase
383
367
  assert_equal 3, msgs.size, "must received 3 messages, but received #{msgs.size}"
384
368
 
385
369
  msg = queue.get
386
- assert_not_nil msg
370
+ refute_nil msg
387
371
  assert_equal "first message", msg.body, "message body must be 'first message', but it's '#{msg.body}'"
388
372
 
389
373
  resp = msg.delete
390
374
  assert_equal 200, resp.code, "API must response with HTTP 200 status, but returned HTTP #{resp.code}"
391
375
 
392
376
  msg = queue.peek
393
- assert_not_nil msg
377
+ refute_nil msg
394
378
  assert_equal "second message", msg.body, "message body must be 'second message', but it's '#{msg.body}'"
395
379
 
396
380
  # delete queue on test complete
@@ -405,60 +389,65 @@ class IronMQTests < TestBase
405
389
  clear_queue(queue_name)
406
390
 
407
391
  queue = @client.queue(queue_name)
408
- queue.post("first message", :timeout => 30)
409
- queue.post("second message", :timeout => 30)
410
- queue.post("third message", :timeout => 30)
392
+ queue.post("first message")
393
+ queue.post("second message")
394
+ queue.post("third message")
411
395
 
412
396
 
413
397
  # get message
414
- msg = queue.get
415
- assert_not_nil msg
398
+ msg = queue.get(:timeout => 10)
399
+ refute_nil msg
416
400
  assert_equal "first message", msg.body, "message body must be 'first message', but it's '#{msg.body}'"
417
401
 
418
- sleep 15 # timeout is not passed
402
+ sleep 5 # timeout is not passed
419
403
 
420
404
  msgs = queue.peek(:n => 3) # all messages from queue
421
405
  assert_equal Array, msgs.class, "waiting for Array, but got #{msgs.class}"
422
406
  assert_equal 2, msgs.size, "API must return only 2 messages"
423
407
  msgs.each do |m|
424
- assert_not_equal msg.id, m.id, "returned a message which must be reserved"
408
+ refute_equal msg.id, m.id, "returned a message which must be reserved"
425
409
  end
426
410
 
427
- sleep 20 # ensure timeout is passed
411
+ sleep 5.5 # ensure timeout is passed
428
412
 
429
413
  # message must return to the queue
430
414
  msgs = queue.peek(:n => 3)
431
415
  assert_equal Array, msgs.class, "waiting for Array, but got #{msgs.class}"
432
416
  assert_equal 3, msgs.size, "API must return 3 messages"
433
417
 
434
- msg = queue.get
435
- assert_not_nil msg
418
+ msg = queue.get(:timeout=>10)
419
+ refute_nil msg
436
420
  assert_equal "second message", msg.body, "message body must be 'second message', but it's '#{msg.body}'"
437
421
 
438
- sleep 15 # timeout is not passed
422
+ sleep 5 # timeout is not passed
439
423
 
440
424
  msgs = queue.peek(:n => 3) # must return another message
441
425
  assert_equal Array, msgs.class, "waiting for Array, but got #{msgs.class}"
442
426
  assert_equal 2, msgs.size, "API must return only 2 messages"
443
- msgs.each { |m| assert_not_equal msg.id, m.id, "returned message which must be reserved" }
427
+ msgs.each { |m| refute_equal msg.id, m.id, "returned message which must be reserved" }
444
428
 
445
- resp = msg.touch # more 30 seconds timeout
429
+ resp = msg.touch # (:timeout=>10) # increase timeout again, should be another 10 seconds
446
430
  assert_equal 200, resp.code, "API must response with HTTP 200 status, but returned HTTP #{resp.code}"
447
431
 
448
- sleep 20 # new timeout is not passed, but previous is (15 + 20 vs 30 + 30 seconds)
432
+ sleep 5 # new timeout is not passed, but previous is (15 + 20 vs 30 + 30 seconds)
449
433
 
450
434
  msgs = queue.peek(:n => 3) # must return the same as for msg2
451
435
  assert_equal Array, msgs.class, "waiting for Array, but got #{msgs.class}"
452
436
  assert_equal 2, msgs.size, "API must return only 2 messages"
453
- msgs.each { |m| assert_not_equal msg.id, m.id, "returned message which must be reserved" }
437
+ msgs.each { |m| refute_equal msg.id, m.id, "returned message which must be reserved" }
454
438
 
455
- sleep 15 # ensure timeout passed
439
+ sleep 5 # ensure timeout passed
440
+ queue.clear
441
+ queue.post("first message")
442
+ queue.post("second message")
443
+ queue.post("third message")
456
444
 
457
445
  # message must be returned to the end of the queue
458
446
  msgs = queue.peek(:n => 3)
447
+ msg = queue.reserve
459
448
  assert_equal Array, msgs.class, "waiting for Array, but got #{msgs.class}"
460
449
  assert_equal 3, msgs.size, "API must return 3 messages"
461
- assert_equal msg.id, msgs[2].id, "released message must be at the end of the queue"
450
+ assert_equal msg.id, msgs[0].id, "released message must be at the beginning of the queue"
462
451
 
463
452
  # delete queue on test complete
464
453
  resp = queue.delete_queue
@@ -468,57 +457,52 @@ class IronMQTests < TestBase
468
457
  def test_release
469
458
  puts 'test_release'
470
459
 
471
- queue_name = "test_release_6"
460
+ queue_name = "test_release_#{Time.now.to_i}"
472
461
  clear_queue(queue_name)
473
462
 
474
- msg_txt = "testMessage-"+Time.now.to_s
463
+ msg_txt = "testMessage-#{Time.now.to_i}"
475
464
  # puts msgTxt
476
465
 
477
466
  queue = @client.queue(queue_name)
478
467
 
479
468
  msg_id = queue.post(msg_txt, {:timeout => 60*5}).id
480
469
  # puts "msg_id: #{msg_id}"
481
- message = queue.get
470
+ message = queue.reserve
482
471
  # p msg
483
472
  assert_equal msg_id, message.id
484
473
  # Ok, so should have received same message, now let's release it quicker than the original timeout
485
474
 
486
475
  # but first, ensure the next get is nil
487
- msg = queue.get
476
+ msg = queue.reserve
488
477
  # p msg
489
478
  assert_nil msg
490
479
 
491
480
  # now release it instantly
492
- message.release
493
- msg = queue.get
494
- # p msg
495
- assert msg
496
- assert_equal msg_id, msg.id
481
+ re = message.release
482
+ msg = queue.reserve
497
483
 
498
- # ok, so should be reserved again
499
- msgr = queue.get
500
- # p msgr
501
- assert_nil msgr
484
+ assert msg.raw
485
+ assert_equal msg_id, msg.id
502
486
 
503
487
  # let's release it in 10 seconds
504
488
  msg.release(:delay => 10)
505
- msgr = queue.get
489
+ msgr = queue.reserve
506
490
  # p msg
507
491
  assert_nil msgr
508
492
 
509
493
  sleep 11
510
- msg = queue.get
511
- assert_not_nil msg
494
+ msg = queue.reserve
495
+ refute_nil msg
512
496
  assert_equal msg_id, msg.id
513
497
 
514
498
  msg.release(:delay => 5)
515
- msg = queue.get
499
+ msg = queue.reserve
516
500
  # p msg
517
501
  assert_nil msg
518
502
 
519
503
  sleep 6
520
- msg = queue.get
521
- assert_not_nil msg
504
+ msg = queue.reserve
505
+ refute_nil msg
522
506
  assert_equal msg_id, msg.id
523
507
 
524
508
  # delete queue on test complete
@@ -526,7 +510,6 @@ class IronMQTests < TestBase
526
510
  assert_equal 200, resp.code, "API must response with HTTP 200 status, but returned HTTP #{resp.code}"
527
511
  end
528
512
 
529
-
530
513
  def test_clear
531
514
  puts "test_clear"
532
515
 
@@ -579,7 +562,6 @@ class IronMQTests < TestBase
579
562
  def test_queue_delete
580
563
  queue = @client.queue("test_delete")
581
564
  queue.post("hi")
582
- old_id = queue.id
583
565
  queue.delete_queue
584
566
 
585
567
  LOG.info "sleeping for a bit to let queue delete..."
@@ -587,7 +569,6 @@ class IronMQTests < TestBase
587
569
 
588
570
  queue.post("hi2")
589
571
  # p queue
590
- assert_not_equal old_id, queue.id, "old queue ID (#{old_id}) must not be equal to new ID (#{queue.id})"
591
572
  assert_equal 1, queue.size, "queue size must be 1, but got #{queue.size}"
592
573
 
593
574
  msg = queue.get
@@ -600,7 +581,7 @@ class IronMQTests < TestBase
600
581
 
601
582
  def test_webhooks
602
583
  qname ="webhook_queue"
603
- url = "#{@client.base_url}/#{qname}/messages/webhook?oauth=#{@client.token}"
584
+ url = "#{@client.base_url}/#{qname}/webhook?oauth=#{@client.token}"
604
585
  # p url
605
586
 
606
587
  v = "hello webhook"
@@ -666,13 +647,13 @@ class IronMQTests < TestBase
666
647
  end
667
648
 
668
649
  def test_queue_set_info
669
- qname = "test_queue_set_info"
650
+ qname = "test_queue_set_info_#{Time.now.to_i}"
670
651
  clear_queue(qname)
671
652
  q = @client.queue(qname)
672
- q.update_queue(:push_type => 'unicast')
673
- assert_equal 'unicast', q.push_type
674
- q.update_queue(:retries => 10)
675
- assert_equal 'unicast', q.reload.push_type
653
+ q.update(message_timeout: 45)
654
+ assert_equal 45, q.reload.info['message_timeout']
655
+ q.update(message_expiration: 3000)
656
+ assert_equal 3000, q.reload.info['message_expiration']
676
657
  end
677
658
 
678
659
  def test_dequeue_delete
@@ -681,7 +662,7 @@ class IronMQTests < TestBase
681
662
  queue = @client.queue(queue_name)
682
663
  v = "hello thou shalt only see me once"
683
664
  queue.post(v)
684
- msg = queue.get(delete: 1, timeout: 30)
665
+ msg = queue.get(delete: true, timeout: 30)
685
666
  assert_equal msg.body, "hello thou shalt only see me once"
686
667
  sleep 1
687
668
  # get the queue again
@@ -691,5 +672,66 @@ class IronMQTests < TestBase
691
672
  msg = queue.get
692
673
  assert_equal nil, msg
693
674
  end
675
+
676
+ def test_long_polling
677
+ queue_name = "test_long_polling#{Time.now.to_i}"
678
+ queue = @client.queue(queue_name)
679
+ queue.update
680
+ msg = queue.get
681
+ assert_nil msg
682
+ v = "hello long"
683
+ # ok, nothing in the queue, let's do a long poll
684
+ starti = Time.now.to_i
685
+ thr = Thread.new {
686
+ sleep 5
687
+ puts "Posting now"
688
+ begin
689
+ queue.post(v)
690
+ rescue Exception => ex
691
+ p ex
692
+ end
693
+
694
+ }
695
+ puts "Now going to wait for it..."
696
+ msg = queue.get(wait: 20)
697
+ # p msg
698
+ endi = Time.now.to_i
699
+ duration = endi - starti
700
+ p duration
701
+ assert duration > 4 && duration <= 7
702
+ refute_nil msg
703
+ assert_equal v, msg.body
704
+ msg.delete
705
+ end
706
+
707
+ def test_delete_reserved_messages
708
+ queue_name = 'test_delete_reserved_messages'
709
+ queue = @client.queue(queue_name)
710
+ clear_queue(queue_name)
711
+ queue.post("more")
712
+ queue.post("and more")
713
+ queue.post("and more")
714
+ assert_equal 3, queue.size
715
+ messages = queue.reserve(n: 3)
716
+ queue.delete_reserved_messages(messages)
717
+ assert_equal 0, queue.reload.size
718
+ end
719
+
720
+ def test_delete_reserved_message
721
+ queue_name = 'test_delete_message'
722
+ queue = @client.queue(queue_name)
723
+ clear_queue(queue_name)
724
+ queue.post("test message")
725
+ assert_equal 1, queue.reload.size
726
+ message = queue.reserve
727
+ queue.delete(message.id, message.reservation_id)
728
+ assert_equal 0, queue.reload.size
729
+
730
+ queue.post("another message")
731
+ assert_equal 1, queue.reload.size
732
+ message = queue.reserve
733
+ message.delete
734
+ assert_equal 0, queue.reload.size
735
+ end
694
736
  end
695
737