Skip to content

Commit 9195049

Browse files
committed
test around_perform
1 parent b99c482 commit 9195049

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

test/job_hooks_test.rb

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,97 @@ def self.perform(history)
8484
end
8585
def self.after_perform(history)
8686
history << :after_perform
87-
raise SyntaxError
87+
raise StandardError
8888
end
8989
end
9090

9191
test "raises an error but has already performed if after_perform fails" do
9292
history = []
93-
assert_raises SyntaxError do
93+
assert_raises StandardError do
9494
perform_job(AfterPerformJobFails, history)
9595
end
9696
assert_equal history, [:perform, :after_perform], "Only after_perform was run"
9797
end
9898
end
99+
100+
context "Resque::Job around_perform" do
101+
include PerformJob
102+
103+
class AroundPerformJob
104+
def self.perform(history)
105+
history << :perform
106+
end
107+
def self.around_perform(history)
108+
history << :start_around_perform
109+
yield
110+
history << :finish_around_perform
111+
end
112+
end
113+
114+
test "it runs around_perform then yields in order to perform" do
115+
result = perform_job(AroundPerformJob, history=[])
116+
assert_equal true, result, "perform returned true"
117+
assert_equal history, [:start_around_perform, :perform, :finish_around_perform]
118+
end
119+
120+
class AroundPerformJobFailsBeforePerforming
121+
def self.perform(history)
122+
history << :perform
123+
end
124+
def self.around_perform(history)
125+
history << :start_around_perform
126+
raise StandardError
127+
yield
128+
history << :finish_around_perform
129+
end
130+
end
131+
132+
test "raises an error and does not perform if around_perform fails before yielding" do
133+
history = []
134+
assert_raises StandardError do
135+
perform_job(AroundPerformJobFailsBeforePerforming, history)
136+
end
137+
assert_equal history, [:start_around_perform], "Only part of around_perform was run"
138+
end
139+
140+
class AroundPerformJobFailsWhilePerforming
141+
def self.perform(history)
142+
history << :perform
143+
raise StandardError
144+
end
145+
def self.around_perform(history)
146+
history << :start_around_perform
147+
begin
148+
yield
149+
ensure
150+
history << :ensure_around_perform
151+
end
152+
history << :finish_around_perform
153+
end
154+
end
155+
156+
test "raises an error but may handle exceptions if perform fails" do
157+
history = []
158+
assert_raises StandardError do
159+
perform_job(AroundPerformJobFailsWhilePerforming, history)
160+
end
161+
assert_equal history, [:start_around_perform, :perform, :ensure_around_perform], "Only part of around_perform was run"
162+
end
163+
164+
class AroundPerformJobDoesNotHaveToYield
165+
def self.perform(history)
166+
history << :perform
167+
end
168+
def self.around_perform(history)
169+
history << :start_around_perform
170+
history << :finish_around_perform
171+
end
172+
end
173+
174+
test "around_perform is not required to yield" do
175+
history = []
176+
result = perform_job(AroundPerformJobDoesNotHaveToYield, history)
177+
assert_equal false, result, "perform returns false"
178+
assert_equal history, [:start_around_perform, :finish_around_perform], "perform was not run"
179+
end
180+
end

0 commit comments

Comments
 (0)