@@ -84,15 +84,97 @@ def self.perform(history)
84
84
end
85
85
def self . after_perform ( history )
86
86
history << :after_perform
87
- raise SyntaxError
87
+ raise StandardError
88
88
end
89
89
end
90
90
91
91
test "raises an error but has already performed if after_perform fails" do
92
92
history = [ ]
93
- assert_raises SyntaxError do
93
+ assert_raises StandardError do
94
94
perform_job ( AfterPerformJobFails , history )
95
95
end
96
96
assert_equal history , [ :perform , :after_perform ] , "Only after_perform was run"
97
97
end
98
98
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