Skip to content

Commit

Permalink
Merge pull request #32 from ktdreyer/minitest
Browse files Browse the repository at this point in the history
tests: switch to minitest 5
  • Loading branch information
ono committed May 28, 2014
2 parents 09dd48f + b6119d9 commit bd30229
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 69 deletions.
3 changes: 3 additions & 0 deletions resque-cleaner.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Gem::Specification.new do |s|

s.add_dependency "resque", "~> 1.0"

s.add_development_dependency "minitest", "~> 5.0"
s.add_development_dependency "rack-test", "~> 0.6.0"

s.description = <<DESCRIPTION
resque-cleaner maintains the cleanliness of failed jobs on Resque.
DESCRIPTION
Expand Down
46 changes: 23 additions & 23 deletions test/resque_cleaner_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
require 'time'
context "ResqueCleaner" do
setup do
describe "ResqueCleaner" do
before do
Resque.redis.flushall

@worker = Resque::Worker.new(:jobs,:jobs2)
Expand All @@ -28,75 +28,75 @@
@cleaner.print_message = false
end

test "#select_by_regex returns only Jason jobs" do
it "#select_by_regex returns only Jason jobs" do
ret = @cleaner.select_by_regex(/Jason/)
assert_equal 13, ret.size
end

test "#select_by_regex returns an empty array if passed a non-regex" do
it "#select_by_regex returns an empty array if passed a non-regex" do
['string', nil, 13, Class.new].each do |non_regex|
ret = @cleaner.select_by_regex(nil)
assert_equal 0, ret.size
end
end

test "#select returns failure jobs" do
it "#select returns failure jobs" do
ret = @cleaner.select
assert_equal 42, ret.size
end

test "#select works with a limit" do
it "#select works with a limit" do
@cleaner.limiter.maximum = 10
ret = @cleaner.select

# only maximum number
assert_equal 10, ret.size

# latest one
# lait one
assert_equal Time.parse(ret[0]['failed_at']), Time.parse('2010-08-13')
end

test "#select with a block returns failure jobs which the block evaluates true" do
it "#select with a block returns failure jobs which the block evaluates true" do
ret = @cleaner.select {|job| job["payload"]["args"][0]=="Jason"}
assert_equal 13, ret.size
end

test "#clear deletes failure jobs" do
it "#clear deletes failure jobs" do
cleared = @cleaner.clear
assert_equal 42, cleared
assert_equal 0, @cleaner.select.size
end

test "#clear with a block deletes failure jobs which the block evaluates true" do
it "#clear with a block deletes failure jobs which the block evaluates true" do
cleared = @cleaner.clear{|job| job["payload"]["args"][0]=="Jason"}
assert_equal 13, cleared
assert_equal 42-13, @cleaner.select.size
assert_equal 0, @cleaner.select{|job| job["payload"]["args"][0]=="Jason"}.size
end

test "#requeue retries failure jobs" do
it "#requeue retries failure jobs" do
assert_equal 0, queue_size(:jobs,:jobs2)
requeued = @cleaner.requeue
assert_equal 42, requeued
assert_equal 42, @cleaner.select.size # it doesn't clear jobs
assert_equal 42, queue_size(:jobs,:jobs2)
end

test "#requeue with a block retries failure jobs which the block evaluates true" do
it "#requeue with a block retries failure jobs which the block evaluates true" do
requeued = @cleaner.requeue{|job| job["payload"]["args"][0]=="Jason"}
assert_equal 13, requeued
assert_equal 13, queue_size(:jobs,:jobs2)
end

test "#requeue with clear option requeues and deletes failure jobs" do
it "#requeue with clear option requeues and deletes failure jobs" do
assert_equal 0, queue_size(:jobs,:jobs2)
requeued = @cleaner.requeue(true)
assert_equal 42, requeued
assert_equal 42, queue_size(:jobs,:jobs2)
assert_equal 0, @cleaner.select.size
end

test "#requeue with :queue option requeues the jobs to the queue" do
it "#requeue with :queue option requeues the jobs to the queue" do
assert_equal 0, queue_size(:jobs,:jobs2,:retry)
requeued = @cleaner.requeue false, :queue => :retry
assert_equal 42, requeued
Expand All @@ -105,14 +105,14 @@
assert_equal 42, queue_size(:retry)
end

test "#clear_stale deletes failure jobs which is queued before the last x enqueued" do
it "#clear_stale deletes failure jobs which is queued before the last x enqueued" do
@cleaner.limiter.maximum = 10
@cleaner.clear_stale
assert_equal 10, @cleaner.failure.count
assert_equal Time.parse(@cleaner.failure_jobs[0]['failed_at']), Time.parse('2010-08-13')
end

test "FailedJobEx module extends job and provides some useful methods" do
it "FailedJobEx module extends job and provides some useful methods" do
# before 2009-04-01
ret = @cleaner.select {|j| j.before?('2009-04-01')}
assert_equal 6, ret.size
Expand Down Expand Up @@ -147,7 +147,7 @@
assert_equal 1, ret.size
end

test "#stats_by_date returns stats grouped by date" do
it "#stats_by_date returns stats grouped by date" do
ret = @cleaner.stats_by_date
assert_equal 6, ret['2009/03/13']
assert_equal 14, ret['2009/11/13']
Expand All @@ -159,25 +159,25 @@
assert_equal 11, ret['2010/08/13']
end

test "#stats_by_class returns stats grouped by class" do
it "#stats_by_class returns stats grouped by class" do
ret = @cleaner.stats_by_class
assert_equal 35, ret['BadJob']
assert_equal 7, ret['BadJobWithSyntaxError']
end

test "#stats_by_class works with broken log" do
it "#stats_by_class works with broken log" do
add_empty_payload_failure
ret = @cleaner.stats_by_class
assert_equal 1, ret['UNKNOWN']
end

test "#stats_by_exception returns stats grouped by exception" do
it "#stats_by_exception returns stats grouped by exception" do
ret = @cleaner.stats_by_exception
assert_equal 35, ret['RuntimeError']
assert_equal 7, ret['SyntaxError']
end

test "#lock ensures that a new failure job doesn't affect in a limit mode" do
it "#lock ensures that a new failure job doesn't affect in a limit mode" do
@cleaner.limiter.maximum = 23
@cleaner.limiter.lock do
first = @cleaner.select[0]
Expand All @@ -192,9 +192,9 @@
assert_equal "Jack", first["payload"]["args"][0]
end

test "allows you to configure limiter" do
it "allows you to configure limiter" do
c = Resque::Plugins::ResqueCleaner.new
assert_not_equal c.limiter.maximum, 10_000
refute_equal c.limiter.maximum, 10_000

module Resque::Plugins
ResqueCleaner::Limiter.default_maximum = 10_000
Expand Down
26 changes: 16 additions & 10 deletions test/resque_web_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
require File.expand_path(File.dirname(__FILE__) + '/test_helper')

# Pull in the server test_helper from resque
require 'resque/server/test_helper.rb'
require 'digest/sha1'
require 'json'
require 'rack/test'

class Minitest::Spec
include Rack::Test::Methods
def app
Resque::Server.new
end
end

def setup_some_failed_jobs
Resque.redis.flushall
Expand All @@ -20,39 +26,39 @@ def setup_some_failed_jobs
@cleaner.print_message = false
end

context "resque-web" do
setup do
describe "resque-web" do
before do
setup_some_failed_jobs
end

test "#cleaner should respond with success" do
it "#cleaner should respond with success" do
get "/cleaner"
assert last_response.body.include?('BadJob')
assert last_response.body =~ /\bException\b/
end

test "#cleaner_list should respond with success" do
it "#cleaner_list should respond with success" do
get "/cleaner_list"
assert last_response.ok?, last_response.errors
end

test '#cleaner_list shows the failed jobs' do
it '#cleaner_list shows the failed jobs' do
get "/cleaner_list"
assert last_response.body.include?('BadJob')
end

test "#cleaner_list shows the failed jobs when we use a select_by_regex" do
it "#cleaner_list shows the failed jobs when we use a select_by_regex" do
get "/cleaner_list", :regex => "BadJob*"
assert last_response.body.include?('"BadJobWithSyntaxError"')
assert last_response.body.include?('"BadJob"')
end


test '#cleaner_exec clears job' do
it '#cleaner_exec clears job' do
post "/cleaner_exec", :action => "clear", :sha1 => Digest::SHA1.hexdigest(@cleaner.select[0].to_json)
assert_equal 10, @cleaner.select.size
end
test "#cleaner_dump should respond with success" do
it "#cleaner_dump should respond with success" do
get "/cleaner_dump"
assert last_response.ok?, last_response.errors
end
Expand Down
50 changes: 14 additions & 36 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
dir = File.dirname(File.expand_path(__FILE__))
$LOAD_PATH.unshift dir + '/../lib'
$TESTING = true
require 'test/unit'
require 'rubygems'
require 'minitest'
require 'minitest/spec'
require 'minitest/autorun'
require 'resque'
require 'timecop'

Expand All @@ -16,6 +18,8 @@
require 'resque'
require 'resque_cleaner'

$TEST_PID = Process.pid

#
# make sure we can run redis
#
Expand All @@ -31,48 +35,22 @@
# start our own redis when the tests start,
# kill it when they end
#
at_exit do
next if $!

if defined?(MiniTest)
exit_code = MiniTest::Unit.new.run(ARGV)
else
exit_code = Test::Unit::AutoRunner.run
MiniTest.after_run {
if Process.pid == $TEST_PID
processes = `ps -A -o pid,command | grep [r]edis-test`.split($/)
pids = processes.map { |process| process.split(" ")[0] }
puts "Killing test redis server..."
pids.each { |pid| Process.kill("TERM", pid.to_i) }
dump = "test/dump.rdb"
File.delete(dump) if File.exist?(dump)
end

pid = `ps -A -o pid,command | grep [r]edis-test`.split(" ")[0]
puts "Killing test redis server..."
Process.kill("KILL", pid.to_i)
dump = "test/dump.rdb"
File.delete(dump) if File.exist?(dump)
exit exit_code
end
}

puts "Starting redis for testing at localhost:9736..."
`redis-server #{dir}/redis-test.conf`
Resque.redis = 'localhost:9736'


##
# test/spec/mini 3
# http://gist.github.com/25455
# chris@ozmm.org
#
def context(*args, &block)
return super unless (name = args.first) && block
require 'test/unit'
klass = Class.new(defined?(ActiveSupport::TestCase) ? ActiveSupport::TestCase : Test::Unit::TestCase) do
def self.test(name, &block)
define_method("test_#{name.gsub(/\W/,'_')}", &block) if block
end
def self.xtest(*args) end
def self.setup(&block) define_method(:setup, &block) end
def self.teardown(&block) define_method(:teardown, &block) end
end
(class << klass; self end).send(:define_method, :name) { name.gsub(/\W/,'_') }
klass.class_eval &block
end

##
# Helper to perform job classes
#
Expand Down

0 comments on commit bd30229

Please sign in to comment.