@@ -2217,6 +2217,141 @@ def to_s
2217
2217
end
2218
2218
end
2219
2219
2220
+ describe "#before_complete" do
2221
+ it "stores the given hook when passed as a block" do
2222
+ expect ( Appsignal ::Transaction . before_complete ) . to be_empty
2223
+ Appsignal ::Transaction . before_complete do |transaction , error |
2224
+ transaction . set_action ( error . message )
2225
+ end
2226
+
2227
+ expect ( Appsignal ::Transaction . before_complete ) . to_not be_empty
2228
+
2229
+ transaction = new_transaction
2230
+ error = ExampleStandardError . new ( "hook_error" )
2231
+
2232
+ expect ( transaction ) . to_not have_action ( "hook_error" )
2233
+ Appsignal ::Transaction . before_complete . first . call ( transaction , error )
2234
+ expect ( transaction ) . to have_action ( "hook_error" )
2235
+ end
2236
+
2237
+ it "stores the given hook when using <<" do
2238
+ expect ( Appsignal ::Transaction . before_complete ) . to be_empty
2239
+ proc = proc do |transaction , error |
2240
+ transaction . set_action ( error . message )
2241
+ end
2242
+
2243
+ Appsignal ::Transaction . before_complete << proc
2244
+
2245
+ expect ( Appsignal ::Transaction . before_complete ) . to eq ( Set . new ( [ proc ] ) )
2246
+ end
2247
+
2248
+ it "only stores a hook once when added several times" do
2249
+ expect ( Appsignal ::Transaction . before_complete ) . to be_empty
2250
+ proc = proc do |transaction |
2251
+ transaction . set_action ( "hook_action" )
2252
+ end
2253
+
2254
+ Appsignal ::Transaction . before_complete ( &proc )
2255
+ Appsignal ::Transaction . before_complete << proc
2256
+
2257
+ expect ( Appsignal ::Transaction . before_complete ) . to eq ( Set . new ( [ proc ] ) )
2258
+ end
2259
+
2260
+ context "when the transaction has an error" do
2261
+ it "calls the given hook with the error when a transaction is completed" do
2262
+ block = proc do |transaction , error |
2263
+ transaction . set_action ( error . message )
2264
+ end
2265
+
2266
+ Appsignal ::Transaction . before_complete ( &block )
2267
+
2268
+ transaction = new_transaction
2269
+ error = ExampleStandardError . new ( "hook_error" )
2270
+ transaction . set_error ( error )
2271
+
2272
+ expect ( block ) . to (
2273
+ receive ( :call )
2274
+ . with ( transaction , error )
2275
+ . and_call_original
2276
+ )
2277
+
2278
+ transaction . complete
2279
+
2280
+ expect ( transaction ) . to have_action ( "hook_error" )
2281
+ end
2282
+ end
2283
+
2284
+ context "when the transaction has several errors" do
2285
+ it "calls the given hook for each of the duplicate error transactions" do
2286
+ block = proc do |transaction , error |
2287
+ transaction . set_action ( error . message )
2288
+ end
2289
+
2290
+ Appsignal ::Transaction . before_complete ( &block )
2291
+
2292
+ transaction = new_transaction
2293
+ first_error = ExampleStandardError . new ( "hook_error_first" )
2294
+ transaction . set_error ( first_error )
2295
+
2296
+ second_error = ExampleStandardError . new ( "hook_error_second" )
2297
+ transaction . set_error ( second_error )
2298
+
2299
+ transaction . complete
2300
+
2301
+ expect ( created_transactions . length ) . to eq ( 2 )
2302
+
2303
+ expect ( created_transactions . find { |t | t == transaction } ) . to (
2304
+ have_action ( "hook_error_first" )
2305
+ )
2306
+ expect ( created_transactions . find { |t | t != transaction } ) . to (
2307
+ have_action ( "hook_error_second" )
2308
+ )
2309
+ end
2310
+ end
2311
+
2312
+ context "when the transaction does not have an error" do
2313
+ it "calls the given hook with nil when a transaction is completed" do
2314
+ block = proc do |transaction |
2315
+ transaction . set_action ( "hook_action" )
2316
+ end
2317
+
2318
+ Appsignal ::Transaction . before_complete ( &block )
2319
+
2320
+ transaction = new_transaction
2321
+
2322
+ expect ( block ) . to (
2323
+ receive ( :call )
2324
+ . with ( transaction , nil )
2325
+ . and_call_original
2326
+ )
2327
+
2328
+ transaction . complete
2329
+
2330
+ expect ( transaction ) . to have_action ( "hook_action" )
2331
+ end
2332
+ end
2333
+
2334
+ it "calls all the hooks in order" do
2335
+ Appsignal ::Transaction . before_complete do |transaction , error |
2336
+ transaction . set_namespace ( error . message )
2337
+ transaction . set_action ( "hook_action_1" )
2338
+ end
2339
+
2340
+ Appsignal ::Transaction . before_complete do |transaction , error |
2341
+ transaction . set_action ( error . message )
2342
+ end
2343
+
2344
+ transaction = new_transaction
2345
+ error = ExampleStandardError . new ( "hook_error" )
2346
+ transaction . set_error ( error )
2347
+
2348
+ transaction . complete
2349
+
2350
+ expect ( transaction ) . to have_namespace ( "hook_error" )
2351
+ expect ( transaction ) . to have_action ( "hook_error" )
2352
+ end
2353
+ end
2354
+
2220
2355
describe "#start_event" do
2221
2356
let ( :transaction ) { new_transaction }
2222
2357
0 commit comments