Skip to content
Browse files

compensate for lack of ordered hash in ruby 1.8

  • Loading branch information...
1 parent 2bf3e97 commit 5d7d26f060ac0f0654675682d2c4bea167aeaa86 @hone hone committed Apr 14, 2012
Showing with 43 additions and 6 deletions.
  1. +7 −6 lib/resque/multi_queue.rb
  2. +36 −0 test/multi_queue_test.rb
View
13 lib/resque/multi_queue.rb
@@ -14,13 +14,14 @@ class MultiQueue
def initialize(queues, redis)
super()
- @queues = {}
- @redis = redis
+ @queues = queues # since ruby 1.8 doesn't have Ordered Hashes
+ @queue_hash = {}
+ @redis = redis
queues.each do |queue|
key = @redis.is_a?(Redis::Namespace) ? "#{@redis.namespace}:" : ""
key += queue.redis_name
- @queues[key] = queue
+ @queue_hash[key] = queue
end
end
@@ -34,7 +35,7 @@ def pop(non_block = false)
synchronize do
value = nil
- @queues.values.each do |queue|
+ @queues.each do |queue|
begin
return [queue, queue.pop(true)]
rescue ThreadError
@@ -44,11 +45,11 @@ def pop(non_block = false)
raise ThreadError
end
else
- queue_names = @queues.values.map {|queue| queue.redis_name }
+ queue_names = @queues.map {|queue| queue.redis_name }
synchronize do
value = @redis.blpop(*(queue_names + [1])) until value
queue_name, payload = value
- queue = @queues[queue_name]
+ queue = @queue_hash[queue_name]
[queue, queue.decode(payload)]
end
end
View
36 test/multi_queue_test.rb
@@ -49,4 +49,40 @@
Timeout::timeout(2) { queue.pop }
end
end
+
+ it "blocking pop processes queues in the order given" do
+ foo = Resque::Queue.new 'foo', redis, coder
+ bar = Resque::Queue.new 'bar', redis, coder
+ baz = Resque::Queue.new 'baz', redis, coder
+ queues = [foo, bar, baz]
+ queue = Resque::MultiQueue.new(queues, redis)
+ job = { 'class' => 'GoodJob', 'args' => [35, 'tar'] }
+
+ queues.each {|q| q << job }
+
+ processed_queues = queues.map do
+ q, j = queue.pop
+ q
+ end
+
+ assert_equal processed_queues, queues
+ end
+
+ it "nonblocking pop processes queues in the order given" do
+ foo = Resque::Queue.new 'foo', redis, coder
+ bar = Resque::Queue.new 'bar', redis, coder
+ baz = Resque::Queue.new 'baz', redis, coder
+ queues = [foo, bar, baz]
+ queue = Resque::MultiQueue.new(queues, redis)
+ job = { 'class' => 'GoodJob', 'args' => [35, 'tar'] }
+
+ queues.each {|q| q << job }
+
+ processed_queues = queues.map do
+ q, j = queue.pop(true)
+ q
+ end
+
+ assert_equal processed_queues, queues
+ end
end

0 comments on commit 5d7d26f

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