Skip to content

Commit

Permalink
Removes mocha, replaces with MiniTest::Stub
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhyman committed Mar 27, 2013
1 parent e2551cf commit d6b84ca
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 93 deletions.
1 change: 0 additions & 1 deletion Gemfile
Expand Up @@ -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"
Expand Down
125 changes: 79 additions & 46 deletions test/legacy/resque_hook_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
1 change: 0 additions & 1 deletion test/legacy/test_helper.rb
Expand Up @@ -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'
Expand Down

0 comments on commit d6b84ca

Please sign in to comment.