Skip to content

Commit

Permalink
Block assertion assert_nothing_queued.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Oct 31, 2010
1 parent e172467 commit 01486c2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
8 changes: 8 additions & 0 deletions lib/resque_unit/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ def assert_not_queued(klass = nil, args = nil, message = nil, &block)
end
end

# Asserts no jobs were queued within the block passed.
def assert_nothing_queued(message = nil, &block)
snapshot = Resque.size
yield
present = Resque.size
assert_equal snapshot, present, message || "No jobs should have been queued"
end

private

def in_queue?(queue, klass, args = nil)
Expand Down
22 changes: 15 additions & 7 deletions lib/resque_unit/resque.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ def self.reset!(queue_name = nil)
end
end

# Returns a hash of all the queue names and jobs that have been queued. The
# format is <tt>{queue_name => [job, ..]}</tt>.
def self.queues
@queue || reset!
end

# Returns an array of all the jobs that have been queued. Each
# element is of the form +{:klass => klass, :args => args}+ where
# +klass+ is the job's class and +args+ is an array of the arguments
# passed to the job.
def self.queue(queue_name)
self.reset! unless @queue
@queue[queue_name]
queues[queue_name]
end

# Executes all jobs in all queues in an undefined order.
Expand Down Expand Up @@ -51,7 +56,7 @@ def self.run_for!(queue_name)
# 3. Repeat 3
def self.full_run!
until empty_queues?
@queue.each do |k, v|
queues.each do |k, v|
while job = v.shift
job[:klass].perform(*job[:args])
end
Expand All @@ -60,9 +65,12 @@ def self.full_run!
end

# Returns the size of the given queue
def self.size(queue_name)
self.reset! unless @queue
@queue[queue_name].length
def self.size(queue_name = nil)
if queue_name
queues[queue_name].length
else
queues.values.flatten.length
end
end

# :nodoc:
Expand All @@ -80,7 +88,7 @@ def self.queue_for(klass)

# :nodoc:
def self.empty_queues?
@queue.all? do |k, v|
queues.all? do |k, v|
v.empty?
end
end
Expand Down
28 changes: 28 additions & 0 deletions test/resque_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ def setup
end
end
end

should "pass the assert_nothing_queued assertion when nothing queued in block" do
Resque.enqueue(LowPriorityJob)
assert_nothing_queued do
# Nothing.
end
end

should "fail the assert_nothing_queued assertion when queued in block" do
assert_raise Test::Unit::AssertionFailedError do
assert_nothing_queued do
Resque.enqueue(LowPriorityJob)
end
end
end
end

context "An empty queue" do
Expand Down Expand Up @@ -253,6 +268,19 @@ def setup
end
end
end

context "of assert_nothing_queued" do
should "include diff" do
begin
Resque.reset!
assert_nothing_queued do
Resque.enqueue(LowPriorityJob)
end
rescue Test::Unit::AssertionFailedError => error
assert_equal "No jobs should have been queued.\n<0> expected but was\n<1>.", error.message
end
end
end
end

context "A job that does not specify a queue" do
Expand Down

0 comments on commit 01486c2

Please sign in to comment.