Browse files

Removes mocha, replaces with MiniTest::Stub

  • Loading branch information...
1 parent e2551cf commit d6b84ca89671e13bda4a0ea1cb2b59d3c4235f14 @jonhyman jonhyman committed Mar 27, 2013
Showing with 136 additions and 93 deletions.
  1. +0 −1 Gemfile
  2. +79 −46 test/legacy/resque_hook_test.rb
  3. +0 −1 test/legacy/test_helper.rb
  4. +57 −45 test/legacy/worker_test.rb
View
1 Gemfile
@@ -15,7 +15,6 @@ end
group :test do
gem "rack-test", "~> 0.5"
gem "json"
- gem "mocha", :require => false
gem "minitest"
gem "sinatra"
gem "capybara"
View
125 test/legacy/resque_hook_test.rb
@@ -14,7 +14,6 @@
Resque::Worker.__send__(:public, :will_fork?)
@worker = Resque::Worker.new(:jobs)
- @worker.stubs(:will_fork?).returns(false)
$called = false
class CallNotifyJob
@@ -42,17 +41,21 @@ def self.perform
2.times { Resque::Job.create(:jobs, CallNotifyJob) }
assert_equal(0, counter)
- @worker.work(0)
- assert_equal(1, counter)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ assert_equal(1, counter)
+ end
end
it 'calls before_first_fork with worker' do
trapped_worker = nil
Resque.before_first_fork { |worker| trapped_worker = worker }
- @worker.work(0)
- assert_equal(@worker, trapped_worker)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ assert_equal(@worker, trapped_worker)
+ end
end
it 'calls before_fork before each job' do
@@ -62,8 +65,10 @@ def self.perform
2.times { Resque::Job.create(:jobs, CallNotifyJob) }
assert_equal(0, counter)
- @worker.work(0)
- assert_equal(@worker.will_fork? ? 2 : 0, counter)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ assert_equal(@worker.will_fork? ? 2 : 0, counter)
+ end
end
it 'calls before_perform before each job' do
@@ -73,8 +78,10 @@ def self.perform
2.times { Resque::Job.create(:jobs, CallNotifyJob) }
assert_equal(0, counter)
- @worker.work(0)
- assert_equal(2, counter)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ assert_equal(2, counter)
+ end
end
it 'calls after_fork after each job if forking' do
@@ -84,8 +91,10 @@ def self.perform
2.times { Resque::Job.create(:jobs, CallNotifyJob) }
assert_equal(0, counter)
- @worker.work(0)
- assert_equal(@worker.will_fork? ? 2 : 0, counter)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ assert_equal(@worker.will_fork? ? 2 : 0, counter)
+ end
end
it 'calls after_perform after each job' do
@@ -95,29 +104,37 @@ def self.perform
2.times { Resque::Job.create(:jobs, CallNotifyJob) }
assert_equal(0, counter)
- @worker.work(0)
- assert_equal(2, counter)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ assert_equal(2, counter)
+ end
end
it 'calls before_first_fork before forking' do
Resque.before_first_fork { assert(!$called) }
Resque::Job.create(:jobs, CallNotifyJob)
- @worker.work(0)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ end
end
it 'calls before_fork before forking' do
Resque.before_fork { assert(!$called) }
Resque::Job.create(:jobs, CallNotifyJob)
- @worker.work(0)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ end
end
it 'calls after_fork after forking' do
Resque.after_fork { assert($called) }
Resque::Job.create(:jobs, CallNotifyJob)
- @worker.work(0)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ end
end
it 'registeres multiple before_first_forks' do
@@ -129,8 +146,10 @@ def self.perform
Resque::Job.create(:jobs, CallNotifyJob)
assert(!first && !second)
- @worker.work(0)
- assert(first && second)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ assert(first && second)
+ end
end
it 'registers multiple before_forks' do
@@ -142,12 +161,14 @@ def self.perform
Resque::Job.create(:jobs, CallNotifyJob)
assert(!first && !second)
- @worker.work(0)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
- if @worker.will_fork?
- assert(first && second)
- else
- assert(!first && !second)
+ if @worker.will_fork?
+ assert(first && second)
+ else
+ assert(!first && !second)
+ end
end
end
@@ -160,12 +181,14 @@ def self.perform
Resque::Job.create(:jobs, CallNotifyJob)
assert(!first && !second)
- @worker.work(0)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
- if @worker.will_fork?
- assert(first && second)
- else
- assert(!first && !second)
+ if @worker.will_fork?
+ assert(first && second)
+ else
+ assert(!first && !second)
+ end
end
end
@@ -176,15 +199,17 @@ def self.perform
Resque.before_pause { first = true }
Resque.before_pause { second = true }
- @worker.pause_processing
+ @worker.stub(:will_fork?, false) do
+ @worker.pause_processing
- assert(!first && !second)
+ assert(!first && !second)
- t = Thread.start { sleep(0.1); Process.kill('CONT', @worker.pid) }
- @worker.work(0)
- t.join
+ t = Thread.start { sleep(0.1); Process.kill('CONT', @worker.pid) }
+ @worker.work(0)
+ t.join
- assert(first && second)
+ assert(first && second)
+ end
end
it 'registers multiple after_pause hooks' do
@@ -193,16 +218,18 @@ def self.perform
Resque.after_pause { first = true }
Resque.after_pause { second = true }
+ @worker.stub(:will_fork?, false) do
- @worker.pause_processing
+ @worker.pause_processing
- assert(!first && !second)
+ assert(!first && !second)
- t = Thread.start { sleep(0.1); Process.kill('CONT', @worker.pid) }
- @worker.work(0)
- t.join
+ t = Thread.start { sleep(0.1); Process.kill('CONT', @worker.pid) }
+ @worker.work(0)
+ t.join
- assert(first && second)
+ assert(first && second)
+ end
end
it 'registers multiple before_perform' do
@@ -214,8 +241,10 @@ def self.perform
Resque::Job.create(:jobs, CallNotifyJob)
assert(!first && !second)
- @worker.work(0)
- assert(first && second)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ assert(first && second)
+ end
end
it 'registers multiple after_perform' do
@@ -227,8 +256,10 @@ def self.perform
Resque::Job.create(:jobs, CallNotifyJob)
assert(!first && !second)
- @worker.work(0)
- assert(first && second)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ assert(first && second)
+ end
end
it 'flattens hooks on assignment' do
@@ -239,7 +270,9 @@ def self.perform
Resque::Job.create(:jobs, CallNotifyJob)
assert(!first && !second)
- @worker.work(0)
- assert(first && second)
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ assert(first && second)
+ end
end
end
View
1 test/legacy/test_helper.rb
@@ -4,7 +4,6 @@
require 'redis/namespace'
require 'minitest/autorun'
require 'resque/core_ext/kernel'
-require 'mocha/setup'
$dir = File.dirname(File.expand_path(__FILE__))
$LOAD_PATH.unshift $dir + '/../lib'
View
102 test/legacy/worker_test.rb
@@ -12,7 +12,6 @@
Resque.after_fork = nil
@worker = Resque::Worker.new(:jobs)
- @worker.stubs(:will_fork?).returns(false)
Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
Resque::Worker.__send__(:public, :will_fork?)
Resque::Worker.__send__(:public, :reserve)
@@ -33,10 +32,12 @@
it "unavailable job definition reports exception and message" do
Resque::Job.create(:jobs, 'NoJobDefinition')
- @worker.work(0)
- assert_equal 1, Resque::Failure.count, 'failure not reported'
- assert_equal('NameError', Resque::Failure.all['exception'])
- assert_match('uninitialized constant', Resque::Failure.all['error'])
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0)
+ assert_equal 1, Resque::Failure.count, 'failure not reported'
+ assert_equal('NameError', Resque::Failure.all['exception'])
+ assert_match('uninitialized constant', Resque::Failure.all['error'])
+ end
end
it "validates jobs before enquing them." do
@@ -256,34 +257,37 @@ def self.perform
Resque::Job.create(:critical, GoodJob)
Resque::Job.create(:blahblah, GoodJob)
+
worker = Resque::Worker.new("*")
- worker.stubs(:will_fork?).returns(false)
- processed_queues = []
+ worker.stub(:will_fork?, false) do
+ processed_queues = []
- worker.work(0) do |job|
- processed_queues << job.queue
- end
+ worker.work(0) do |job|
+ processed_queues << job.queue
+ end
- assert_equal %w( jobs high critical blahblah ).sort, processed_queues
+ assert_equal %w( jobs high critical blahblah ).sort, processed_queues
+ end
end
it "can work with dynamically added queues when using wildcard" do
worker = Resque::Worker.new("*")
- worker.stubs(:will_fork?).returns(false)
+ worker.stub(:will_fork?, false) do
- assert_equal ["jobs"], Resque.queues
+ assert_equal ["jobs"], Resque.queues
- Resque::Job.create(:high, GoodJob)
- Resque::Job.create(:critical, GoodJob)
- Resque::Job.create(:blahblah, GoodJob)
+ Resque::Job.create(:high, GoodJob)
+ Resque::Job.create(:critical, GoodJob)
+ Resque::Job.create(:blahblah, GoodJob)
- processed_queues = []
+ processed_queues = []
- worker.work(0) do |job|
- processed_queues << job.queue
- end
+ worker.work(0) do |job|
+ processed_queues << job.queue
+ end
- assert_equal %w( jobs high critical blahblah ).sort, processed_queues
+ assert_equal %w( jobs high critical blahblah ).sort, processed_queues
+ end
end
it "has a unique id" do
@@ -405,21 +409,24 @@ def self.perform
it "knows when it started" do
time = Time.now
@worker.work(0) do
- assert Time.parse(@worker.started) - time < 0.1
+ registry = Resque::WorkerRegistry.new(@worker)
+ assert Time.parse(registry.started) - time < 0.1
end
end
it "knows whether it exists or not" do
@worker.work(0) do
- assert Resque::Worker.exists?(@worker)
- assert !Resque::Worker.exists?('blah-blah')
+ assert Resque::WorkerRegistry.exists?(@worker)
+ assert !Resque::WorkerRegistry.exists?('blah-blah')
end
end
it "sets $0 while working" do
- @worker.work(0) do
- ver = Resque::Version
- assert_equal "resque-#{ver}: Processing jobs since #{Time.now.to_i}", $0
+ @worker.stub(:will_fork?, false) do
+ @worker.work(0) do
+ ver = Resque::Version
+ assert_equal "resque-#{ver}: Processing jobs since #{Time.now.to_i}", $0
+ end
end
end
@@ -429,13 +436,13 @@ def self.perform
assert_equal @worker.to_s, found.to_s
assert found.working?
registry = Resque::WorkerRegistry.new(@worker)
- assert_equal registry.job, found.job
+ assert_equal registry.job, Resque::WorkerRegistry.new(found).job
end
end
it "doesn't find fakes" do
@worker.work(0) do
- found = Resque::Worker.find('blah-blah')
+ found = Resque::WorkerRegistry.find('blah-blah')
assert_equal nil, found
end
end
@@ -574,10 +581,11 @@ def self.perform
proc = Proc.new { File.open(file.path, "w+") { |f| f.write("foo")} }
# Somewhat of a pain to test because the child will fork, so need to communicate across processes and not use
# Redis (since we're stubbing out reconnect...)
- Resque.redis.client.stubs(:reconnect).returns(proc.call)
- @worker.work(0)
- val = File.read(file.path)
- assert_equal val, "foo"
+ Resque.redis.client.stub(:reconnect, proc) do
+ @worker.work(0)
+ val = File.read(file.path)
+ assert_equal val, "foo"
+ end
end
it "tries to reconnect three times before giving up" do
@@ -607,15 +615,18 @@ def sleep(duration = nil)
end
end
- @worker.stubs(:will_fork?).returns(true)
- # The 4th try gives up and throws an exception
- begin
- @worker.work(0)
- rescue
- end
+ # Make sure we haven't been flagged as reconnected (from previous tests)
+ @worker.instance_variable_set(:@reconnected, false)
+ @worker.stub(:will_fork?, true) do
+ # The 4th try gives up and throws an exception
+ begin
+ @worker.work(0)
+ rescue
+ end
- val = File.read("reconnect_test.txt").to_i
- assert_equal 4, val
+ val = File.read("reconnect_test.txt").to_i
+ assert_equal 4, val
+ end
ensure
File.delete("reconnect_test.txt") if File.exists?("reconnect_test.txt")
@@ -768,10 +779,11 @@ def self.on_failure_store_exception(exc, *args)
end
it "will notify failure hooks when a job is killed by a signal" do
- @worker.stubs(:will_fork?).returns(true)
- Resque.enqueue(SuicidalJob)
- @worker.work(0)
- assert_equal Resque::DirtyExit, SuicidalJob.send(:class_variable_get, :@@failure_exception).class
+ @worker.stub(:will_fork?, true) do
+ Resque.enqueue(SuicidalJob)
+ @worker.work(0)
+ assert_equal Resque::DirtyExit, SuicidalJob.send(:class_variable_get, :@@failure_exception).class
+ end
end
end

0 comments on commit d6b84ca

Please sign in to comment.