Skip to content

Commit ea85b6d

Browse files
committed
test on_failure
1 parent 9195049 commit ea85b6d

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

test/job_hooks_test.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,58 @@ def self.around_perform(history)
178178
assert_equal history, [:start_around_perform, :finish_around_perform], "perform was not run"
179179
end
180180
end
181+
182+
context "Resque::Job on_failure" do
183+
include PerformJob
184+
185+
class FailureJobThatDoesNotFail
186+
def self.perform(history)
187+
history << :perform
188+
end
189+
def self.on_failure(exception, history)
190+
history << exception.message
191+
end
192+
end
193+
194+
test "it does not call on_failure if no failures occur" do
195+
result = perform_job(FailureJobThatDoesNotFail, history=[])
196+
assert_equal true, result, "perform returned true"
197+
assert_equal history, [:perform]
198+
end
199+
200+
class FailureJobThatFails
201+
def self.perform(history)
202+
history << :perform
203+
raise StandardError, "oh no"
204+
end
205+
def self.on_failure(exception, history)
206+
history << exception.message
207+
end
208+
end
209+
210+
test "it calls on_failure with the exception and then re-raises the exception" do
211+
history = []
212+
assert_raises StandardError do
213+
perform_job(FailureJobThatFails, history)
214+
end
215+
assert_equal history, [:perform, "oh no"]
216+
end
217+
218+
class FailureJobThatFailsBadly
219+
def self.perform(history)
220+
history << :perform
221+
raise SyntaxError, "oh no"
222+
end
223+
def self.on_failure(exception, history)
224+
history << exception.message
225+
end
226+
end
227+
228+
test "it calls on_failure even with bad exceptions" do
229+
history = []
230+
assert_raises SyntaxError do
231+
perform_job(FailureJobThatFailsBadly, history)
232+
end
233+
assert_equal history, [:perform, "oh no"]
234+
end
235+
end

0 commit comments

Comments
 (0)