@@ -293,54 +293,172 @@ void callableThrowingErrorShouldBeObserved() {
293
293
294
294
@ Test
295
295
void runnableShouldBeScoped () {
296
- registry .observationConfig ().observationHandler (c -> true );
296
+ ObservationHandler <Observation .Context > handler = mock (ObservationHandler .class );
297
+ when (handler .supportsContext (isA (Observation .Context .class ))).thenReturn (true );
298
+ registry .observationConfig ().observationHandler (handler );
297
299
Observation observation = Observation .start ("myObservation" , registry );
298
300
Runnable runnable = () -> assertThat (registry .getCurrentObservation ()).isSameAs (observation );
299
301
observation .scoped (runnable );
300
302
assertThat (registry .getCurrentObservation ()).isNull ();
303
+ assertThat (observation .getContext ().getError ()).isEmpty ();
304
+
305
+ InOrder inOrder = inOrder (handler );
306
+ inOrder .verify (handler ).onScopeOpened (isA (Observation .Context .class ));
307
+ inOrder .verify (handler ).onScopeClosed (isA (Observation .Context .class ));
308
+ inOrder .verify (handler , times (0 )).onError (isA (Observation .Context .class ));
309
+ inOrder .verify (handler , times (0 )).onStop (isA (Observation .Context .class ));
301
310
}
302
311
303
312
@ Test
304
313
void errorShouldBeReportedOnFailingScopedRunnable () {
305
- registry .observationConfig ().observationHandler (c -> true );
306
- Observation .Context context = new Observation .Context ();
307
- Observation observation = Observation .start ("myObservation" , context , registry );
314
+ ObservationHandler <Observation .Context > handler = mock (ObservationHandler .class );
315
+ when (handler .supportsContext (isA (Observation .Context .class ))).thenReturn (true );
316
+ registry .observationConfig ().observationHandler (handler );
317
+ Observation observation = Observation .start ("myObservation" , registry );
308
318
RuntimeException error = new RuntimeException ("simulated" );
309
319
310
320
Runnable runnable = () -> {
321
+ assertThat (registry .getCurrentObservation ()).isSameAs (observation );
311
322
throw error ;
312
323
};
313
324
assertThatThrownBy (() -> observation .scoped (runnable )).isSameAs (error );
314
- assertThat (context .getError ()).containsSame (error );
315
325
assertThat (registry .getCurrentObservation ()).isNull ();
326
+ assertThat (observation .getContext ().getError ()).containsSame (error );
327
+
328
+ InOrder inOrder = inOrder (handler );
329
+ inOrder .verify (handler ).onScopeOpened (isA (Observation .Context .class ));
330
+ inOrder .verify (handler ).onScopeClosed (isA (Observation .Context .class ));
331
+ inOrder .verify (handler ).onError (isA (Observation .Context .class ));
332
+ inOrder .verify (handler , times (0 )).onStop (isA (Observation .Context .class ));
333
+ }
334
+
335
+ @ Test
336
+ void checkedRunnableShouldBeScoped () throws Throwable {
337
+ ObservationHandler <Observation .Context > handler = mock (ObservationHandler .class );
338
+ when (handler .supportsContext (isA (Observation .Context .class ))).thenReturn (true );
339
+ registry .observationConfig ().observationHandler (handler );
340
+ Observation observation = Observation .start ("myObservation" , registry );
341
+ Observation .CheckedRunnable runnable = () -> assertThat (registry .getCurrentObservation ()).isSameAs (observation );
342
+ observation .scopedChecked (runnable );
343
+ assertThat (registry .getCurrentObservation ()).isNull ();
344
+ assertThat (observation .getContext ().getError ()).isEmpty ();
345
+
346
+ InOrder inOrder = inOrder (handler );
347
+ inOrder .verify (handler ).onScopeOpened (isA (Observation .Context .class ));
348
+ inOrder .verify (handler ).onScopeClosed (isA (Observation .Context .class ));
349
+ inOrder .verify (handler , times (0 )).onError (isA (Observation .Context .class ));
350
+ inOrder .verify (handler , times (0 )).onStop (isA (Observation .Context .class ));
351
+ }
352
+
353
+ @ Test
354
+ void errorShouldBeReportedOnFailingScopedCheckedRunnable () {
355
+ ObservationHandler <Observation .Context > handler = mock (ObservationHandler .class );
356
+ when (handler .supportsContext (isA (Observation .Context .class ))).thenReturn (true );
357
+ registry .observationConfig ().observationHandler (handler );
358
+ Observation observation = Observation .start ("myObservation" , registry );
359
+ RuntimeException error = new RuntimeException ("simulated" );
360
+
361
+ Observation .CheckedRunnable runnable = () -> {
362
+ assertThat (registry .getCurrentObservation ()).isSameAs (observation );
363
+ throw error ;
364
+ };
365
+ assertThatThrownBy (() -> observation .scopedChecked (runnable )).isSameAs (error );
366
+ assertThat (registry .getCurrentObservation ()).isNull ();
367
+ assertThat (observation .getContext ().getError ()).containsSame (error );
368
+
369
+ InOrder inOrder = inOrder (handler );
370
+ inOrder .verify (handler ).onScopeOpened (isA (Observation .Context .class ));
371
+ inOrder .verify (handler ).onScopeClosed (isA (Observation .Context .class ));
372
+ inOrder .verify (handler ).onError (isA (Observation .Context .class ));
373
+ inOrder .verify (handler , times (0 )).onStop (isA (Observation .Context .class ));
316
374
}
317
375
318
376
@ Test
319
377
void supplierShouldBeScoped () {
320
- registry .observationConfig ().observationHandler (c -> true );
378
+ ObservationHandler <Observation .Context > handler = mock (ObservationHandler .class );
379
+ when (handler .supportsContext (isA (Observation .Context .class ))).thenReturn (true );
380
+ registry .observationConfig ().observationHandler (handler );
321
381
Observation observation = Observation .start ("myObservation" , registry );
322
382
Supplier <String > supplier = () -> {
323
383
assertThat (registry .getCurrentObservation ()).isSameAs (observation );
324
384
return "test" ;
325
385
};
326
386
String result = observation .scoped (supplier );
327
- assertThat (result ).isEqualTo ("test" );
328
387
assertThat (registry .getCurrentObservation ()).isNull ();
388
+ assertThat (result ).isEqualTo ("test" );
389
+
390
+ InOrder inOrder = inOrder (handler );
391
+ inOrder .verify (handler ).onScopeOpened (isA (Observation .Context .class ));
392
+ inOrder .verify (handler ).onScopeClosed (isA (Observation .Context .class ));
393
+ inOrder .verify (handler , times (0 )).onError (isA (Observation .Context .class ));
394
+ inOrder .verify (handler , times (0 )).onStop (isA (Observation .Context .class ));
329
395
}
330
396
331
397
@ Test
332
398
void errorShouldBeReportedOnFailingScopedSupplier () {
333
- registry .observationConfig ().observationHandler (c -> true );
334
- Observation .Context context = new Observation .Context ();
335
- Observation observation = Observation .start ("myObservation" , context , registry );
399
+ ObservationHandler <Observation .Context > handler = mock (ObservationHandler .class );
400
+ when (handler .supportsContext (isA (Observation .Context .class ))).thenReturn (true );
401
+ registry .observationConfig ().observationHandler (handler );
402
+ Observation observation = Observation .start ("myObservation" , registry );
336
403
RuntimeException error = new RuntimeException ("simulated" );
337
404
338
405
Supplier <String > supplier = () -> {
406
+ assertThat (registry .getCurrentObservation ()).isSameAs (observation );
339
407
throw error ;
340
408
};
341
409
assertThatThrownBy (() -> observation .scoped (supplier )).isSameAs (error );
342
- assertThat (context .getError ()).containsSame (error );
343
410
assertThat (registry .getCurrentObservation ()).isNull ();
411
+ assertThat (observation .getContext ().getError ()).containsSame (error );
412
+
413
+ InOrder inOrder = inOrder (handler );
414
+ inOrder .verify (handler ).onScopeOpened (isA (Observation .Context .class ));
415
+ inOrder .verify (handler ).onScopeClosed (isA (Observation .Context .class ));
416
+ inOrder .verify (handler ).onError (isA (Observation .Context .class ));
417
+ inOrder .verify (handler , times (0 )).onStop (isA (Observation .Context .class ));
418
+ }
419
+
420
+ @ Test
421
+ void checkedCallableShouldBeScoped () throws Throwable {
422
+ ObservationHandler <Observation .Context > handler = mock (ObservationHandler .class );
423
+ when (handler .supportsContext (isA (Observation .Context .class ))).thenReturn (true );
424
+ registry .observationConfig ().observationHandler (handler );
425
+ Observation observation = Observation .start ("myObservation" , registry );
426
+ Observation .CheckedCallable <String > callable = () -> {
427
+ assertThat (registry .getCurrentObservation ()).isSameAs (observation );
428
+ return "test" ;
429
+ };
430
+ String result = observation .scopedChecked (callable );
431
+ assertThat (registry .getCurrentObservation ()).isNull ();
432
+ assertThat (result ).isEqualTo ("test" );
433
+
434
+ InOrder inOrder = inOrder (handler );
435
+ inOrder .verify (handler ).onScopeOpened (isA (Observation .Context .class ));
436
+ inOrder .verify (handler ).onScopeClosed (isA (Observation .Context .class ));
437
+ inOrder .verify (handler , times (0 )).onError (isA (Observation .Context .class ));
438
+ inOrder .verify (handler , times (0 )).onStop (isA (Observation .Context .class ));
439
+ }
440
+
441
+ @ Test
442
+ void errorShouldBeReportedOnFailingScopedCheckedCallable () {
443
+ ObservationHandler <Observation .Context > handler = mock (ObservationHandler .class );
444
+ when (handler .supportsContext (isA (Observation .Context .class ))).thenReturn (true );
445
+ registry .observationConfig ().observationHandler (handler );
446
+ Observation observation = Observation .start ("myObservation" , registry );
447
+ RuntimeException error = new RuntimeException ("simulated" );
448
+
449
+ Observation .CheckedCallable <String > callable = () -> {
450
+ assertThat (registry .getCurrentObservation ()).isSameAs (observation );
451
+ throw error ;
452
+ };
453
+ assertThatThrownBy (() -> observation .scopedChecked (callable )).isSameAs (error );
454
+ assertThat (registry .getCurrentObservation ()).isNull ();
455
+ assertThat (observation .getContext ().getError ()).containsSame (error );
456
+
457
+ InOrder inOrder = inOrder (handler );
458
+ inOrder .verify (handler ).onScopeOpened (isA (Observation .Context .class ));
459
+ inOrder .verify (handler ).onScopeClosed (isA (Observation .Context .class ));
460
+ inOrder .verify (handler ).onError (isA (Observation .Context .class ));
461
+ inOrder .verify (handler , times (0 )).onStop (isA (Observation .Context .class ));
344
462
}
345
463
346
464
@ Test
@@ -359,6 +477,22 @@ void runnableShouldNotBeParentScopedIfParentIsNull() {
359
477
assertThat (registry .getCurrentObservation ()).isNull ();
360
478
}
361
479
480
+ @ Test
481
+ void checkedRunnableShouldBeParentScoped () throws Throwable {
482
+ registry .observationConfig ().observationHandler (c -> true );
483
+ Observation parent = Observation .start ("myObservation" , registry );
484
+ Observation .CheckedRunnable runnable = () -> assertThat (registry .getCurrentObservation ()).isSameAs (parent );
485
+ Observation .tryScopedChecked (parent , runnable );
486
+ assertThat (registry .getCurrentObservation ()).isNull ();
487
+ }
488
+
489
+ @ Test
490
+ void checkedRunnableShouldNotBeParentScopedIfParentIsNull () throws Throwable {
491
+ Observation .CheckedRunnable runnable = () -> assertThat (registry .getCurrentObservation ()).isNull ();
492
+ Observation .tryScopedChecked (null , runnable );
493
+ assertThat (registry .getCurrentObservation ()).isNull ();
494
+ }
495
+
362
496
@ Test
363
497
void supplierShouldBeParentScoped () {
364
498
registry .observationConfig ().observationHandler (c -> true );
@@ -382,6 +516,29 @@ void supplierShouldNotBeParentScopedIfParentIsNull() {
382
516
assertThat (registry .getCurrentObservation ()).isNull ();
383
517
}
384
518
519
+ @ Test
520
+ void checkedCallableShouldBeParentScoped () throws Throwable {
521
+ registry .observationConfig ().observationHandler (c -> true );
522
+ Observation parent = Observation .start ("myObservation" , registry );
523
+ Observation .CheckedCallable <String > callable = () -> {
524
+ assertThat (registry .getCurrentObservation ()).isSameAs (parent );
525
+ return "test" ;
526
+ };
527
+ String result = Observation .tryScopedChecked (parent , callable );
528
+ assertThat (result ).isEqualTo ("test" );
529
+ assertThat (registry .getCurrentObservation ()).isNull ();
530
+ }
531
+
532
+ @ Test
533
+ void checkedCallableShouldNotBeParentScopedIfParentIsNull () throws Throwable {
534
+ Observation .CheckedCallable <String > callable = () -> {
535
+ assertThat (registry .getCurrentObservation ()).isNull ();
536
+ return "test" ;
537
+ };
538
+ Observation .tryScopedChecked (null , callable );
539
+ assertThat (registry .getCurrentObservation ()).isNull ();
540
+ }
541
+
385
542
@ Test
386
543
void observationFieldsShouldBeSetOnContext () {
387
544
AssertingHandler assertingHandler = new AssertingHandler ();
0 commit comments