Skip to content

Commit 1cf6d83

Browse files
committed
tests for resque hooks before_perform and after_perform
1 parent 1998444 commit 1cf6d83

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

test/resque_hook_test.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
Resque.before_first_fork = nil
88
Resque.before_fork = nil
99
Resque.after_fork = nil
10+
Resque.before_perform = nil
11+
Resque.after_perform = nil
1012

1113
@worker = Resque::Worker.new(:jobs)
1214

@@ -23,6 +25,8 @@ def self.perform
2325
assert_equal [], Resque.before_first_fork
2426
assert_equal [], Resque.before_fork
2527
assert_equal [], Resque.after_fork
28+
assert_equal [], Resque.before_perform
29+
assert_equal [], Resque.after_perform
2630
end
2731

2832
it 'calls before_first_fork once' do
@@ -47,6 +51,17 @@ def self.perform
4751
assert_equal(@worker.will_fork? ? 2 : 0, counter)
4852
end
4953

54+
it 'calls before_perform before each job' do
55+
counter = 0
56+
57+
Resque.before_perform { counter += 1 }
58+
2.times { Resque::Job.create(:jobs, CallNotifyJob) }
59+
60+
assert_equal(0, counter)
61+
@worker.work(0)
62+
assert_equal(2, counter)
63+
end
64+
5065
it 'calls after_fork after each job if forking' do
5166
counter = 0
5267

@@ -58,6 +73,17 @@ def self.perform
5873
assert_equal(@worker.will_fork? ? 2 : 0, counter)
5974
end
6075

76+
it 'calls after_perform after each job' do
77+
counter = 0
78+
79+
Resque.after_perform { counter += 1 }
80+
2.times { Resque::Job.create(:jobs, CallNotifyJob) }
81+
82+
assert_equal(0, counter)
83+
@worker.work(0)
84+
assert_equal(2, counter)
85+
end
86+
6187
it 'calls before_first_fork before forking' do
6288
Resque.before_first_fork { assert(!$called) }
6389

@@ -163,4 +189,31 @@ def self.perform
163189

164190
assert(first && second)
165191
end
192+
193+
it 'registers multiple before_perform' do
194+
first = false
195+
second = false
196+
197+
Resque.before_perform { first = true }
198+
Resque.before_perform { second = true }
199+
Resque::Job.create(:jobs, CallNotifyJob)
200+
201+
assert(!first && !second)
202+
@worker.work(0)
203+
assert(first && second)
204+
end
205+
206+
it 'registers multiple after_perform' do
207+
first = false
208+
second = false
209+
210+
Resque.after_perform { first = true }
211+
Resque.after_perform { second = true }
212+
Resque::Job.create(:jobs, CallNotifyJob)
213+
214+
assert(!first && !second)
215+
@worker.work(0)
216+
assert(first && second)
217+
end
218+
166219
end

0 commit comments

Comments
 (0)