Skip to content
Browse files

add peek and list_range methods

  • Loading branch information...
1 parent 1ee3a47 commit 4d17234d3e9f46eaf03e3c8f0fe4a216ba445e22 Mateusz Juraszek committed May 10, 2011
Showing with 74 additions and 0 deletions.
  1. +21 −0 lib/resque_unit/resque.rb
  2. +53 −0 test/resque_test.rb
View
21 lib/resque_unit/resque.rb
@@ -31,6 +31,27 @@ def queue(queue_name)
queues[queue_name]
end
+ # Returns an array of jobs' payloads currently queued.
+ #
+ # start and count should be integer and can be used for pagination.
+ # start is the item to begin, count is how many items to return.
+ #
+ # To get the 3rd page of a 30 item, paginatied list one would use:
+ # Resque.peek('my_list', 59, 30)
+ def peek(queue_name, start = 0, count = 1)
+ list_range(queue_name, start, count)
+ end
+
+ # Gets a range of jobs' payloads from queue.
+ # Returns single element if count equal 1
+ def list_range(key, start = 0, count = 1)
+ if count == 1
+ queues[key][start]
+ else
+ queues[key][start...start + count] || []
+ end
+ end
+
# Yes, all Resque hooks!
def enable_hooks!
@hooks_enabled = true
View
53 test/resque_test.rb
@@ -0,0 +1,53 @@
+require 'test_helper'
+
+class ResqueTest < Test::Unit::TestCase
+
+ def setup
+ Resque.reset!
+ end
+
+ context "with one queued job" do
+ setup do
+ Resque.enqueue(MediumPriorityJob, "some args")
+ @job_payload = {:args=>["some args"], :klass=>MediumPriorityJob}
+ end
+
+ should "return job payload if peek method called with count equal 1" do
+ assert_equal Resque.peek(MediumPriorityJob.queue, 0, 1), @job_payload
+ end
+
+ should "return array of jobs' payloads if peek method called with count different than 1" do
+ assert_equal Resque.peek(MediumPriorityJob.queue, 0, 5), [@job_payload]
+ end
+ end
+
+ context "with few queued jobs" do
+ setup do
+ Resque.enqueue(MediumPriorityJob, "1")
+ Resque.enqueue(MediumPriorityJob, "2")
+ Resque.enqueue(MediumPriorityJob, "3")
+ end
+
+ should "return jobs' payloads 2 and 3 if start is set to 1 and count equal 2" do
+ assert_equal Resque.peek(MediumPriorityJob.queue, 1, 2).map{|h| h[:args]}, [["2"], ["3"]]
+ end
+
+ should "return empty array if start is higher than queue size" do
+ assert_equal Resque.peek(MediumPriorityJob.queue, 4, 2), []
+ end
+
+ should "return empty array if count is equal 0" do
+ assert_equal Resque.peek(MediumPriorityJob.queue, 0, 0), []
+ end
+ end
+
+ context "without queued jobs" do
+ should "return nil if count is 1" do
+ assert_equal Resque.peek(MediumPriorityJob.queue, 0, 1), nil
+ end
+
+ should "return empty array if count is not 1" do
+ assert_equal Resque.peek(MediumPriorityJob.queue, 0, 999), []
+ end
+ end
+end

0 comments on commit 4d17234

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