Skip to content
This repository
Browse code

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

…n 4.0.3
  • Loading branch information...
commit b6a9e1c30a3816e55624de25cdebe39d291ea43c 1 parent 0f7fa22
Yury Yantsevich authored March 25, 2013
6  Gemfile.lock
... ...
@@ -1,7 +1,7 @@
1 1
 PATH
2 2
   remote: .
3 3
   specs:
4  
-    iron_mq (4.0.1)
  4
+    iron_mq (4.0.3)
5 5
       iron_core (>= 0.5.1)
6 6
 
7 7
 GEM
@@ -20,9 +20,9 @@ GEM
20 20
     net-http-persistent (2.8)
21 21
     quicky (0.4.0)
22 22
     rake (10.0.3)
23  
-    rest (2.2.0)
24  
-      net-http-persistent
  23
+    rest (2.5.0)
25 24
       rest-client (>= 0.3.0)
  25
+      typhoeus (>= 0.5.4)
26 26
     rest-client (1.6.7)
27 27
       mime-types (>= 1.16)
28 28
     test-unit (2.5.4)
1  lib/iron_mq.rb
... ...
@@ -1,4 +1,5 @@
1 1
 require File.expand_path('iron_mq/response', File.dirname(__FILE__))
  2
+require File.expand_path('iron_mq/subscribers', File.dirname(__FILE__))
2 3
 require File.expand_path('iron_mq/queues', File.dirname(__FILE__))
3 4
 require File.expand_path('iron_mq/messages', File.dirname(__FILE__))
4 5
 require File.expand_path('iron_mq/client', File.dirname(__FILE__))
20  lib/iron_mq/messages.rb
@@ -22,20 +22,18 @@ def release(options = {})
22 22
     def subscribers(options = {})
23 23
       response = call_api_and_parse_response(:get, "/subscribers", {}, false)
24 24
 
25  
-      response['subscribers'].each_with_object([]) do |subscriber, ret|
26  
-        ret << Subscriber.new(subscriber, self, options)
27  
-      end
  25
+      response['subscribers'].map { |s| Subscriber.new(s, self, options) }
28 26
     end
29 27
 
30 28
     def delete
31  
-      begin
32  
-        call_api_and_parse_response(:delete)
33  
-      rescue Rest::HttpError => ex
34  
-        if ex.code == 404
35  
-          Rest.logger.info("Delete got 404, safe to ignore.")
36  
-        else
37  
-          raise ex
38  
-        end
  29
+      call_api_and_parse_response(:delete)
  30
+    rescue Rest::HttpError => ex
  31
+      if ex.code == 404
  32
+        Rest.logger.info("Delete got 404, safe to ignore.")
  33
+        # return ResponseBase as normal
  34
+        ResponseBase.new({"msg" => "Deleted"})
  35
+      else
  36
+        raise ex
39 37
       end
40 38
     end
41 39
 
51  lib/iron_mq/queues.rb
@@ -12,14 +12,15 @@ def initialize(client, queue_name)
12 12
     end
13 13
 
14 14
     def info
15  
-      info = raw
16  
-      begin
17  
-        # Do not instantiate response
18  
-        info = call_api_and_parse_response(:get, '', {}, false)
19  
-      rescue Rest::HttpError
  15
+      call_api_and_parse_response(:get)
  16
+    rescue Rest::HttpError => ex
  17
+      if ex.code == 404
  18
+        Rest.logger.info("GET Queue#info return 404, treat as new queue, safe to ignore.")
  19
+        # return ResponseBase as normal
  20
+        ResponseBase.new(raw)
  21
+      else
  22
+        raise ex
20 23
       end
21  
-
22  
-      ResponseBase.new(info)
23 24
     end
24 25
 
25 26
     def size
@@ -56,6 +57,14 @@ def clear
56 57
     # Backward compatibility, better name is `delete`
57 58
     def delete_queue
58 59
       call_api_and_parse_response(:delete)
  60
+    rescue Rest::HttpError => ex
  61
+      if ex.code == 404
  62
+        Rest.logger.info("Delete got 404, safe to ignore.")
  63
+        # return ResponseBase as normal
  64
+        ResponseBase.new({"msg" => "Deleted"})
  65
+      else
  66
+        raise ex
  67
+      end
59 68
     end
60 69
 
61 70
     # Backward compatibility
@@ -96,14 +105,10 @@ def post_messages(payload, options = {})
96 105
                batch = true
97 106
                # FIXME: This maybe better to process Array of Objects the same way as for single message.
98 107
                #
99  
-               #          payload.each_with_object([]) do |msg, res|
100  
-               #            res << options.merge(:body => msg)
101  
-               #          end
  108
+               #          payload.map { |msg| options.merge(:body => msg) }
102 109
                #
103 110
                #        For now user must pass objects like `[{:body => msg1}, {:body => msg2}]`
104  
-               payload.each_with_object([]) do |msg, res|
105  
-                 res << msg.merge(options)
106  
-               end
  111
+               payload.map { |msg| msg.merge(options) }
107 112
              else
108 113
                [ options.merge(:body => payload) ]
109 114
              end
@@ -113,16 +118,15 @@ def post_messages(payload, options = {})
113 118
 
114 119
       if instantiate
115 120
         n = batch ? 2 : 1
116  
-        msg_ids = res["ids"].map { |id| {'id' => id} }
  121
+        msg_ids = res["ids"].map { |id| {"id" => id} }
117 122
 
118 123
         process_messages(msg_ids, {:n => n})
119 124
       else
120 125
         if batch
121 126
           # FIXME: Return Array of ResponsBase instead, it seems more clear than raw response
122 127
           #
123  
-          #          res["ids"].each_with_object([]) do |id, responses|
124  
-          #            responses << ResponseBase.new({"id" => id, "msg" => res["msg"]})
125  
-          #          end
  128
+          #          res["ids"].map { |id| ResponseBase.new({"id" => id, "msg" => res["msg"]}) }
  129
+          #
126 130
           ResponseBase.new(res) # Backward capable
127 131
         else
128 132
           ResponseBase.new({"id" => res["ids"][0], "msg" => res["msg"]})
@@ -190,8 +194,7 @@ def method_missing(meth, *args)
190 194
       #   queue.size
191 195
       # etc.
192 196
       if args.length == 0
193  
-        res = info.send(meth)
194  
-        res ? res : super
  197
+        self.info[meth.to_s]
195 198
       else
196 199
         super
197 200
       end
@@ -207,13 +210,9 @@ def process_messages(messages, options)
207 210
       multiple = wait_for_multiple?(options)
208 211
 
209 212
       if messages.is_a?(Array) && messages.size > 0
210  
-        if multiple
211  
-          messages.each_with_object([]) do |m, msgs|
212  
-            msgs << Message.new(self, m)
213  
-          end
214  
-        else
215  
-          Message.new(self, messages[0])
216  
-        end
  213
+        msgs = messages.map { |m| Message.new(self, m) }
  214
+
  215
+        multiple ? msgs : msgs[0]
217 216
       else
218 217
         multiple ? [] : nil
219 218
       end
4  lib/iron_mq/response.rb
@@ -22,7 +22,9 @@ def raw
22 22
     private
23 23
 
24 24
     def stringify_keys(hash)
25  
-      hash.keys.each_with_object({}) { |k, res| res[k.to_s] = hash[k] }
  25
+      hash.keys.each { |k| hash[k.to_s] = hash.delete(k) }
  26
+
  27
+      hash
26 28
     end
27 29
   end
28 30
 
8  lib/iron_mq/subscribers.rb
@@ -13,6 +13,14 @@ def initialize(data, message, options = {})
13 13
     # `options` was kept for backward compatibility
14 14
     def delete(options = {})
15 15
       @message.call_api_and_parse_response(:delete, path)
  16
+    rescue Rest::HttpError => ex
  17
+      if ex.code == 404
  18
+        Rest.logger.info("Delete got 404, safe to ignore.")
  19
+        # return ResponseBase as normal
  20
+        ResponseBase.new({"msg" => "Deleted"})
  21
+      else
  22
+        raise ex
  23
+      end
16 24
     end
17 25
 
18 26
     alias_method :acknowledge, :delete
2  lib/iron_mq/version.rb
... ...
@@ -1,4 +1,4 @@
1 1
 module IronMQ
2  
-  VERSION = "4.0.2"
  2
+  VERSION = "4.0.3"
3 3
 end
4 4
 
6  test/test_base.rb
@@ -32,9 +32,9 @@ def setup
32 32
     @host = "#{config['host'] || "mq-aws-us-east-1.iron.io"}"
33 33
 
34 34
     @client = IronMQ::Client.new(@config['iron'])
35  
-
36  
-    Rest.logger.level = Logger::DEBUG # this doesn't work for some reason?
37  
-    IronCore::Logger.logger.level = Logger::DEBUG
  35
+    puts "IronMQ::VERSION = #{IronMQ::VERSION}"
  36
+    #Rest.logger.level = Logger::DEBUG # this doesn't work for some reason?
  37
+    #IronCore::Logger.logger.level = Logger::DEBUG
38 38
 
39 39
     @queue_name = 'ironmq-ruby-tests' # default queue for tests
40 40
   end
30  test/test_iron_mq.rb
@@ -159,9 +159,8 @@ def test_timeout
159 159
 
160 160
     tries = MAX_TRIES
161 161
     while tries > 0
162  
-      sleep 0.5
  162
+      sleep 2
163 163
       tries -= 1
164  
-      sleep 1
165 164
 
166 165
       new_msg = queue.get
167 166
       # p new_msg
@@ -180,7 +179,7 @@ def test_timeout
180 179
     msg = queue.get
181 180
     # p msg
182 181
     assert msg
183  
-    assert_equal msg.raw['timeout'], 30
  182
+    assert_equal 30, msg.timeout
184 183
 
185 184
     msg_nil = queue.get
186 185
     # p msg_nil
@@ -188,9 +187,8 @@ def test_timeout
188 187
 
189 188
     tries = MAX_TRIES
190 189
     while tries > 0
191  
-      sleep 0.5
  190
+      sleep 2
192 191
       tries -= 1
193  
-      sleep 1
194 192
 
195 193
       new_msg = queue.get
196 194
       next if new_msg.nil?
@@ -205,8 +203,9 @@ def test_timeout
205 203
     # timeout on get
206 204
     res = queue.post("hello world timeout3!")
207 205
     msg = queue.get(:timeout => 30)
  206
+    puts "MESSAGE IS #{msg.inspect}"
208 207
     assert msg
209  
-    assert_equal msg.raw['timeout'], 30
  208
+    assert_equal msg.timeout, 30
210 209
 
211 210
     msg_nil = queue.get
212 211
     # p msg_nil
@@ -214,14 +213,13 @@ def test_timeout
214 213
 
215 214
     tries = MAX_TRIES
216 215
     while tries > 0
217  
-      sleep 0.5
  216
+      sleep 2
218 217
       tries -= 1
219  
-      sleep 1
220 218
 
221 219
       new_msg = queue.get
222 220
       next if new_msg.nil?
223 221
 
224  
-      assert_equal new_msg.id, msg.id
  222
+      assert_equal msg.id, new_msg.id
225 223
 
226 224
       new_msg.delete
227 225
       break
@@ -245,7 +243,11 @@ def test_queues
245 243
     #  q = @client.queues.get(:name => "some_queue_that_does_not_exist")
246 244
     #end
247 245
     queue = @client.queues.get(:name => "some_queue_that_does_not_exist")
248  
-    assert queue.new? == true
  246
+    assert queue.new?
  247
+
  248
+    # create at least one queue
  249
+    queue.post('create queue message')
  250
+    assert_equal queue.new?, false, "queue must exist on the service after post message to"
249 251
 
250 252
     res = @client.queues.list
251 253
     # puts "res.size: #{res.size}"
@@ -261,6 +263,10 @@ def test_queues
261 263
     # res.each do |q| { p q.name }
262 264
 
263 265
     assert_equal 0, res.size
  266
+
  267
+    # delete queue on test complete
  268
+    resp = queue.delete_queue
  269
+    assert_equal 200, resp.code, "API must response with HTTP 200 status, but returned HTTP #{resp.code}"
264 270
   end
265 271
 
266 272
   def test_delay
@@ -544,7 +550,7 @@ def test_clear
544 550
     val = "hi mr clean"
545 551
     queue.post(val)
546 552
 
547  
-    sleep 0.5 # make sure the counter has time to update
  553
+    sleep 2 # make sure the counter has time to update
548 554
     assert_equal 1, queue.size
549 555
 
550 556
     queue.clear
@@ -581,7 +587,7 @@ def test_poll
581 587
     while tries > 0
582 588
       tries -= 1
583 589
       break if 0 == queue.size
584  
-      sleep 0.5
  590
+      sleep 1
585 591
     end
586 592
     assert_not_equal tries, 0
587 593
 

0 notes on commit b6a9e1c

Please sign in to comment.
Something went wrong with that request. Please try again.