Permalink
Browse files

#53: bugfixes for 404 workaround; support 1.8 ruby; fix tests; versio…

…n 4.0.3
  • Loading branch information...
1 parent 0f7fa22 commit b6a9e1c30a3816e55624de25cdebe39d291ea43c @featalion featalion committed Mar 25, 2013
Showing with 71 additions and 57 deletions.
  1. +3 −3 Gemfile.lock
  2. +1 −0 lib/iron_mq.rb
  3. +9 −11 lib/iron_mq/messages.rb
  4. +25 −26 lib/iron_mq/queues.rb
  5. +3 −1 lib/iron_mq/response.rb
  6. +8 −0 lib/iron_mq/subscribers.rb
  7. +1 −1 lib/iron_mq/version.rb
  8. +3 −3 test/test_base.rb
  9. +18 −12 test/test_iron_mq.rb
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- iron_mq (4.0.1)
+ iron_mq (4.0.3)
iron_core (>= 0.5.1)
GEM
@@ -20,9 +20,9 @@ GEM
net-http-persistent (2.8)
quicky (0.4.0)
rake (10.0.3)
- rest (2.2.0)
- net-http-persistent
+ rest (2.5.0)
rest-client (>= 0.3.0)
+ typhoeus (>= 0.5.4)
rest-client (1.6.7)
mime-types (>= 1.16)
test-unit (2.5.4)
View
@@ -1,4 +1,5 @@
require File.expand_path('iron_mq/response', File.dirname(__FILE__))
+require File.expand_path('iron_mq/subscribers', File.dirname(__FILE__))
require File.expand_path('iron_mq/queues', File.dirname(__FILE__))
require File.expand_path('iron_mq/messages', File.dirname(__FILE__))
require File.expand_path('iron_mq/client', File.dirname(__FILE__))
View
@@ -22,20 +22,18 @@ def release(options = {})
def subscribers(options = {})
response = call_api_and_parse_response(:get, "/subscribers", {}, false)
- response['subscribers'].each_with_object([]) do |subscriber, ret|
- ret << Subscriber.new(subscriber, self, options)
- end
+ response['subscribers'].map { |s| Subscriber.new(s, self, options) }
end
def delete
- begin
- call_api_and_parse_response(:delete)
- rescue Rest::HttpError => ex
- if ex.code == 404
- Rest.logger.info("Delete got 404, safe to ignore.")
- else
- raise ex
- end
+ call_api_and_parse_response(:delete)
+ rescue Rest::HttpError => ex
+ if ex.code == 404
+ Rest.logger.info("Delete got 404, safe to ignore.")
+ # return ResponseBase as normal
+ ResponseBase.new({"msg" => "Deleted"})
+ else
+ raise ex
end
end
View
@@ -12,14 +12,15 @@ def initialize(client, queue_name)
end
def info
- info = raw
- begin
- # Do not instantiate response
- info = call_api_and_parse_response(:get, '', {}, false)
- rescue Rest::HttpError
+ call_api_and_parse_response(:get)
+ rescue Rest::HttpError => ex
+ if ex.code == 404
+ Rest.logger.info("GET Queue#info return 404, treat as new queue, safe to ignore.")
+ # return ResponseBase as normal
+ ResponseBase.new(raw)
+ else
+ raise ex
end
-
- ResponseBase.new(info)
end
def size
@@ -56,6 +57,14 @@ def clear
# Backward compatibility, better name is `delete`
def delete_queue
call_api_and_parse_response(:delete)
+ rescue Rest::HttpError => ex
+ if ex.code == 404
+ Rest.logger.info("Delete got 404, safe to ignore.")
+ # return ResponseBase as normal
+ ResponseBase.new({"msg" => "Deleted"})
+ else
+ raise ex
+ end
end
# Backward compatibility
@@ -96,14 +105,10 @@ def post_messages(payload, options = {})
batch = true
# FIXME: This maybe better to process Array of Objects the same way as for single message.
#
- # payload.each_with_object([]) do |msg, res|
- # res << options.merge(:body => msg)
- # end
+ # payload.map { |msg| options.merge(:body => msg) }
#
# For now user must pass objects like `[{:body => msg1}, {:body => msg2}]`
- payload.each_with_object([]) do |msg, res|
- res << msg.merge(options)
- end
+ payload.map { |msg| msg.merge(options) }
else
[ options.merge(:body => payload) ]
end
@@ -113,16 +118,15 @@ def post_messages(payload, options = {})
if instantiate
n = batch ? 2 : 1
- msg_ids = res["ids"].map { |id| {'id' => id} }
+ msg_ids = res["ids"].map { |id| {"id" => id} }
process_messages(msg_ids, {:n => n})
else
if batch
# FIXME: Return Array of ResponsBase instead, it seems more clear than raw response
#
- # res["ids"].each_with_object([]) do |id, responses|
- # responses << ResponseBase.new({"id" => id, "msg" => res["msg"]})
- # end
+ # res["ids"].map { |id| ResponseBase.new({"id" => id, "msg" => res["msg"]}) }
+ #
ResponseBase.new(res) # Backward capable
else
ResponseBase.new({"id" => res["ids"][0], "msg" => res["msg"]})
@@ -190,8 +194,7 @@ def method_missing(meth, *args)
# queue.size
# etc.
if args.length == 0
- res = info.send(meth)
- res ? res : super
+ self.info[meth.to_s]
else
super
end
@@ -207,13 +210,9 @@ def process_messages(messages, options)
multiple = wait_for_multiple?(options)
if messages.is_a?(Array) && messages.size > 0
- if multiple
- messages.each_with_object([]) do |m, msgs|
- msgs << Message.new(self, m)
- end
- else
- Message.new(self, messages[0])
- end
+ msgs = messages.map { |m| Message.new(self, m) }
+
+ multiple ? msgs : msgs[0]
else
multiple ? [] : nil
end
View
@@ -22,7 +22,9 @@ def raw
private
def stringify_keys(hash)
- hash.keys.each_with_object({}) { |k, res| res[k.to_s] = hash[k] }
+ hash.keys.each { |k| hash[k.to_s] = hash.delete(k) }
+
+ hash
end
end
@@ -13,6 +13,14 @@ def initialize(data, message, options = {})
# `options` was kept for backward compatibility
def delete(options = {})
@message.call_api_and_parse_response(:delete, path)
+ rescue Rest::HttpError => ex
+ if ex.code == 404
+ Rest.logger.info("Delete got 404, safe to ignore.")
+ # return ResponseBase as normal
+ ResponseBase.new({"msg" => "Deleted"})
+ else
+ raise ex
+ end
end
alias_method :acknowledge, :delete
View
@@ -1,4 +1,4 @@
module IronMQ
- VERSION = "4.0.2"
+ VERSION = "4.0.3"
end
View
@@ -32,9 +32,9 @@ def setup
@host = "#{config['host'] || "mq-aws-us-east-1.iron.io"}"
@client = IronMQ::Client.new(@config['iron'])
-
- Rest.logger.level = Logger::DEBUG # this doesn't work for some reason?
- IronCore::Logger.logger.level = Logger::DEBUG
+ puts "IronMQ::VERSION = #{IronMQ::VERSION}"
+ #Rest.logger.level = Logger::DEBUG # this doesn't work for some reason?
+ #IronCore::Logger.logger.level = Logger::DEBUG
@queue_name = 'ironmq-ruby-tests' # default queue for tests
end
View
@@ -159,9 +159,8 @@ def test_timeout
tries = MAX_TRIES
while tries > 0
- sleep 0.5
+ sleep 2
tries -= 1
- sleep 1
new_msg = queue.get
# p new_msg
@@ -180,17 +179,16 @@ def test_timeout
msg = queue.get
# p msg
assert msg
- assert_equal msg.raw['timeout'], 30
+ assert_equal 30, msg.timeout
msg_nil = queue.get
# p msg_nil
assert_nil msg_nil
tries = MAX_TRIES
while tries > 0
- sleep 0.5
+ sleep 2
tries -= 1
- sleep 1
new_msg = queue.get
next if new_msg.nil?
@@ -205,23 +203,23 @@ def test_timeout
# timeout on get
res = queue.post("hello world timeout3!")
msg = queue.get(:timeout => 30)
+ puts "MESSAGE IS #{msg.inspect}"
assert msg
- assert_equal msg.raw['timeout'], 30
+ assert_equal msg.timeout, 30
msg_nil = queue.get
# p msg_nil
assert_nil msg_nil
tries = MAX_TRIES
while tries > 0
- sleep 0.5
+ sleep 2
tries -= 1
- sleep 1
new_msg = queue.get
next if new_msg.nil?
- assert_equal new_msg.id, msg.id
+ assert_equal msg.id, new_msg.id
new_msg.delete
break
@@ -245,7 +243,11 @@ def test_queues
# q = @client.queues.get(:name => "some_queue_that_does_not_exist")
#end
queue = @client.queues.get(:name => "some_queue_that_does_not_exist")
- assert queue.new? == true
+ assert queue.new?
+
+ # create at least one queue
+ queue.post('create queue message')
+ assert_equal queue.new?, false, "queue must exist on the service after post message to"
res = @client.queues.list
# puts "res.size: #{res.size}"
@@ -261,6 +263,10 @@ def test_queues
# res.each do |q| { p q.name }
assert_equal 0, res.size
+
+ # delete queue on test complete
+ resp = queue.delete_queue
+ assert_equal 200, resp.code, "API must response with HTTP 200 status, but returned HTTP #{resp.code}"
end
def test_delay
@@ -544,7 +550,7 @@ def test_clear
val = "hi mr clean"
queue.post(val)
- sleep 0.5 # make sure the counter has time to update
+ sleep 2 # make sure the counter has time to update
assert_equal 1, queue.size
queue.clear
@@ -581,7 +587,7 @@ def test_poll
while tries > 0
tries -= 1
break if 0 == queue.size
- sleep 0.5
+ sleep 1
end
assert_not_equal tries, 0

0 comments on commit b6a9e1c

Please sign in to comment.