module Resque

Re-define errors in from Resque, in case the ‘resque’ gem was not loaded.

The fake Resque class. This needs to be loaded after the real Resque for the assertions in ResqueUnit::Assertions to work.

Public Class Methods

queues() click to toggle source

Returns a hash of all the queue names and jobs that have been queued. The format is {queue_name => [job, ..]}.

# File lib/resque_unit/resque.rb, line 22
def self.queues
  @queue || reset!
end

Public Instance Methods

all(queue_name) click to toggle source

Return an array of all jobs’ payloads for queue Elements are decoded

# File lib/resque_unit/resque.rb, line 36
def all(queue_name)
  result = list_range(queue_name, 0, size(queue_name))
  result.is_a?(Array) ? result : [ result ]
end
disable_hooks!() click to toggle source
# File lib/resque_unit/resque.rb, line 68
def disable_hooks!
  @hooks_enabled = nil
end
enable_hooks!() click to toggle source

Yes, all Resque hooks!

# File lib/resque_unit/resque.rb, line 64
def enable_hooks!
  @hooks_enabled = true
end
full_run!() click to toggle source
  1. Execute all jobs in all queues in an undefined order,

  2. Check if new jobs were announced, and execute them.

  3. Repeat 3

# File lib/resque_unit/resque.rb, line 97
def full_run!
  run! until empty_queues?
end
list_range(key, start = 0, count = 1) click to toggle source

Gets a range of jobs’ payloads from queue. Returns single element if count equal 1 Elements are decoded

# File lib/resque_unit/resque.rb, line 55
def list_range(key, start = 0, count = 1)
  data = if count == 1
    decode(queues[key][start])
  else
    (queues[key][start...start + count] || []).map { |entry| decode(entry) }
  end
end
peek(queue_name, start = 0, count = 1) click to toggle source

Returns an array of jobs’ payloads for queue.

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)
# File lib/resque_unit/resque.rb, line 48
def peek(queue_name, start = 0, count = 1)
  list_range(queue_name, start, count)
end
queue(queue_name) click to toggle source

Returns an array of all the jobs that have been queued. Each element is of the form +{“class” => klass, “args” => args}+ where klass is the job’s class and args is an array of the arguments passed to the job.

# File lib/resque_unit/resque.rb, line 30
def queue(queue_name)
  queues[queue_name]
end
reset!(queue_name = nil) click to toggle source

Resets all the queues to the empty state. This should be called in your test’s setup method until I can figure out a way for it to automatically be called.

If queue_name is given, then resets only that queue.

# File lib/resque_unit/resque.rb, line 12
def reset!(queue_name = nil)
  if @queue && queue_name
    @queue[queue_name] = []
  else
    @queue = Hash.new { |h, k| h[k] = [] }
  end
end
run!() click to toggle source

Executes all jobs in all queues in an undefined order.

# File lib/resque_unit/resque.rb, line 73
def run!
  payloads = []
  @queue.each do |queue_name, queue|
    payloads.concat queue.slice!(0, queue.size)
  end
  exec_payloads payloads.shuffle
end
run_for!(queue_name, limit=false) click to toggle source
# File lib/resque_unit/resque.rb, line 81
def run_for!(queue_name, limit=false)
  queue = @queue[queue_name]
  exec_payloads queue.slice!(0, ( limit ? limit : queue.size) ).shuffle
end
size(queue_name = nil) click to toggle source

Returns the size of the given queue

# File lib/resque_unit/resque.rb, line 102
def size(queue_name = nil)
  if queue_name
    queues[queue_name].length
  else
    queues.values.flatten.length
  end
end

Private Instance Methods

exec_payloads(raw_payloads) click to toggle source
# File lib/resque_unit/resque.rb, line 86
def exec_payloads(raw_payloads)
  raw_payloads.each do |raw_payload|
    job_payload = decode(raw_payload)
    @hooks_enabled ? perform_with_hooks(job_payload) : perform_without_hooks(job_payload)
  end
end