-
Notifications
You must be signed in to change notification settings - Fork 1k
Scoped checked actions #3323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scoped checked actions #3323
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -293,54 +293,172 @@ void callableThrowingErrorShouldBeObserved() { | |||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void runnableShouldBeScoped() { | ||||||||||||||||||
registry.observationConfig().observationHandler(c -> true); | ||||||||||||||||||
ObservationHandler<Observation.Context> handler = mock(ObservationHandler.class); | ||||||||||||||||||
when(handler.supportsContext(isA(Observation.Context.class))).thenReturn(true); | ||||||||||||||||||
registry.observationConfig().observationHandler(handler); | ||||||||||||||||||
Observation observation = Observation.start("myObservation", registry); | ||||||||||||||||||
Runnable runnable = () -> assertThat(registry.getCurrentObservation()).isSameAs(observation); | ||||||||||||||||||
observation.scoped(runnable); | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
assertThat(observation.getContext().getError()).isEmpty(); | ||||||||||||||||||
|
||||||||||||||||||
InOrder inOrder = inOrder(handler); | ||||||||||||||||||
inOrder.verify(handler).onScopeOpened(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler).onScopeClosed(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler, times(0)).onError(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler, times(0)).onStop(isA(Observation.Context.class)); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void errorShouldBeReportedOnFailingScopedRunnable() { | ||||||||||||||||||
registry.observationConfig().observationHandler(c -> true); | ||||||||||||||||||
Observation.Context context = new Observation.Context(); | ||||||||||||||||||
Observation observation = Observation.start("myObservation", context, registry); | ||||||||||||||||||
ObservationHandler<Observation.Context> handler = mock(ObservationHandler.class); | ||||||||||||||||||
when(handler.supportsContext(isA(Observation.Context.class))).thenReturn(true); | ||||||||||||||||||
registry.observationConfig().observationHandler(handler); | ||||||||||||||||||
Observation observation = Observation.start("myObservation", registry); | ||||||||||||||||||
RuntimeException error = new RuntimeException("simulated"); | ||||||||||||||||||
|
||||||||||||||||||
Runnable runnable = () -> { | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isSameAs(observation); | ||||||||||||||||||
throw error; | ||||||||||||||||||
}; | ||||||||||||||||||
assertThatThrownBy(() -> observation.scoped(runnable)).isSameAs(error); | ||||||||||||||||||
assertThat(context.getError()).containsSame(error); | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
assertThat(observation.getContext().getError()).containsSame(error); | ||||||||||||||||||
|
||||||||||||||||||
InOrder inOrder = inOrder(handler); | ||||||||||||||||||
inOrder.verify(handler).onScopeOpened(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler).onScopeClosed(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler).onError(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler, times(0)).onStop(isA(Observation.Context.class)); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void checkedRunnableShouldBeScoped() throws Throwable { | ||||||||||||||||||
ObservationHandler<Observation.Context> handler = mock(ObservationHandler.class); | ||||||||||||||||||
when(handler.supportsContext(isA(Observation.Context.class))).thenReturn(true); | ||||||||||||||||||
registry.observationConfig().observationHandler(handler); | ||||||||||||||||||
Observation observation = Observation.start("myObservation", registry); | ||||||||||||||||||
Observation.CheckedRunnable runnable = () -> assertThat(registry.getCurrentObservation()).isSameAs(observation); | ||||||||||||||||||
observation.scopedChecked(runnable); | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
assertThat(observation.getContext().getError()).isEmpty(); | ||||||||||||||||||
|
||||||||||||||||||
InOrder inOrder = inOrder(handler); | ||||||||||||||||||
inOrder.verify(handler).onScopeOpened(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler).onScopeClosed(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler, times(0)).onError(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler, times(0)).onStop(isA(Observation.Context.class)); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void errorShouldBeReportedOnFailingScopedCheckedRunnable() { | ||||||||||||||||||
ObservationHandler<Observation.Context> handler = mock(ObservationHandler.class); | ||||||||||||||||||
when(handler.supportsContext(isA(Observation.Context.class))).thenReturn(true); | ||||||||||||||||||
registry.observationConfig().observationHandler(handler); | ||||||||||||||||||
Observation observation = Observation.start("myObservation", registry); | ||||||||||||||||||
RuntimeException error = new RuntimeException("simulated"); | ||||||||||||||||||
|
||||||||||||||||||
Observation.CheckedRunnable runnable = () -> { | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isSameAs(observation); | ||||||||||||||||||
throw error; | ||||||||||||||||||
}; | ||||||||||||||||||
assertThatThrownBy(() -> observation.scopedChecked(runnable)).isSameAs(error); | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
assertThat(observation.getContext().getError()).containsSame(error); | ||||||||||||||||||
|
||||||||||||||||||
InOrder inOrder = inOrder(handler); | ||||||||||||||||||
inOrder.verify(handler).onScopeOpened(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler).onScopeClosed(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler).onError(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler, times(0)).onStop(isA(Observation.Context.class)); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void supplierShouldBeScoped() { | ||||||||||||||||||
registry.observationConfig().observationHandler(c -> true); | ||||||||||||||||||
ObservationHandler<Observation.Context> handler = mock(ObservationHandler.class); | ||||||||||||||||||
when(handler.supportsContext(isA(Observation.Context.class))).thenReturn(true); | ||||||||||||||||||
registry.observationConfig().observationHandler(handler); | ||||||||||||||||||
Observation observation = Observation.start("myObservation", registry); | ||||||||||||||||||
Supplier<String> supplier = () -> { | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isSameAs(observation); | ||||||||||||||||||
return "test"; | ||||||||||||||||||
}; | ||||||||||||||||||
String result = observation.scoped(supplier); | ||||||||||||||||||
assertThat(result).isEqualTo("test"); | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
assertThat(result).isEqualTo("test"); | ||||||||||||||||||
|
||||||||||||||||||
InOrder inOrder = inOrder(handler); | ||||||||||||||||||
inOrder.verify(handler).onScopeOpened(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler).onScopeClosed(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler, times(0)).onError(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler, times(0)).onStop(isA(Observation.Context.class)); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void errorShouldBeReportedOnFailingScopedSupplier() { | ||||||||||||||||||
registry.observationConfig().observationHandler(c -> true); | ||||||||||||||||||
Observation.Context context = new Observation.Context(); | ||||||||||||||||||
Observation observation = Observation.start("myObservation", context, registry); | ||||||||||||||||||
ObservationHandler<Observation.Context> handler = mock(ObservationHandler.class); | ||||||||||||||||||
when(handler.supportsContext(isA(Observation.Context.class))).thenReturn(true); | ||||||||||||||||||
registry.observationConfig().observationHandler(handler); | ||||||||||||||||||
Observation observation = Observation.start("myObservation", registry); | ||||||||||||||||||
RuntimeException error = new RuntimeException("simulated"); | ||||||||||||||||||
|
||||||||||||||||||
Supplier<String> supplier = () -> { | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isSameAs(observation); | ||||||||||||||||||
throw error; | ||||||||||||||||||
}; | ||||||||||||||||||
assertThatThrownBy(() -> observation.scoped(supplier)).isSameAs(error); | ||||||||||||||||||
assertThat(context.getError()).containsSame(error); | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
assertThat(observation.getContext().getError()).containsSame(error); | ||||||||||||||||||
|
||||||||||||||||||
InOrder inOrder = inOrder(handler); | ||||||||||||||||||
inOrder.verify(handler).onScopeOpened(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler).onScopeClosed(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler).onError(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler, times(0)).onStop(isA(Observation.Context.class)); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void checkedCallableShouldBeScoped() throws Throwable { | ||||||||||||||||||
ObservationHandler<Observation.Context> handler = mock(ObservationHandler.class); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💬 7 similar findings have been found in this PR unchecked: unchecked conversion Expand here to view all instances of this finding
Visit the Lift Web Console to find more details in your report. Reply with "@sonatype-lift help" for info about LiftBot commands. When talking to LiftBot, you need to refresh the page to see its response. Click here to get to know more about LiftBot commands. Was this a good recommendation? |
||||||||||||||||||
when(handler.supportsContext(isA(Observation.Context.class))).thenReturn(true); | ||||||||||||||||||
registry.observationConfig().observationHandler(handler); | ||||||||||||||||||
Observation observation = Observation.start("myObservation", registry); | ||||||||||||||||||
Observation.CheckedCallable<String> callable = () -> { | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isSameAs(observation); | ||||||||||||||||||
return "test"; | ||||||||||||||||||
}; | ||||||||||||||||||
String result = observation.scopedChecked(callable); | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
assertThat(result).isEqualTo("test"); | ||||||||||||||||||
|
||||||||||||||||||
InOrder inOrder = inOrder(handler); | ||||||||||||||||||
inOrder.verify(handler).onScopeOpened(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler).onScopeClosed(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler, times(0)).onError(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler, times(0)).onStop(isA(Observation.Context.class)); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void errorShouldBeReportedOnFailingScopedCheckedCallable() { | ||||||||||||||||||
ObservationHandler<Observation.Context> handler = mock(ObservationHandler.class); | ||||||||||||||||||
when(handler.supportsContext(isA(Observation.Context.class))).thenReturn(true); | ||||||||||||||||||
registry.observationConfig().observationHandler(handler); | ||||||||||||||||||
Observation observation = Observation.start("myObservation", registry); | ||||||||||||||||||
RuntimeException error = new RuntimeException("simulated"); | ||||||||||||||||||
|
||||||||||||||||||
Observation.CheckedCallable<String> callable = () -> { | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isSameAs(observation); | ||||||||||||||||||
throw error; | ||||||||||||||||||
}; | ||||||||||||||||||
assertThatThrownBy(() -> observation.scopedChecked(callable)).isSameAs(error); | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
assertThat(observation.getContext().getError()).containsSame(error); | ||||||||||||||||||
|
||||||||||||||||||
InOrder inOrder = inOrder(handler); | ||||||||||||||||||
inOrder.verify(handler).onScopeOpened(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler).onScopeClosed(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler).onError(isA(Observation.Context.class)); | ||||||||||||||||||
inOrder.verify(handler, times(0)).onStop(isA(Observation.Context.class)); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
|
@@ -359,6 +477,22 @@ void runnableShouldNotBeParentScopedIfParentIsNull() { | |||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void checkedRunnableShouldBeParentScoped() throws Throwable { | ||||||||||||||||||
registry.observationConfig().observationHandler(c -> true); | ||||||||||||||||||
Observation parent = Observation.start("myObservation", registry); | ||||||||||||||||||
Observation.CheckedRunnable runnable = () -> assertThat(registry.getCurrentObservation()).isSameAs(parent); | ||||||||||||||||||
Observation.tryScopedChecked(parent, runnable); | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void checkedRunnableShouldNotBeParentScopedIfParentIsNull() throws Throwable { | ||||||||||||||||||
Observation.CheckedRunnable runnable = () -> assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
Observation.tryScopedChecked(null, runnable); | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void supplierShouldBeParentScoped() { | ||||||||||||||||||
registry.observationConfig().observationHandler(c -> true); | ||||||||||||||||||
|
@@ -382,6 +516,29 @@ void supplierShouldNotBeParentScopedIfParentIsNull() { | |||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void checkedCallableShouldBeParentScoped() throws Throwable { | ||||||||||||||||||
registry.observationConfig().observationHandler(c -> true); | ||||||||||||||||||
Observation parent = Observation.start("myObservation", registry); | ||||||||||||||||||
Observation.CheckedCallable<String> callable = () -> { | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isSameAs(parent); | ||||||||||||||||||
return "test"; | ||||||||||||||||||
}; | ||||||||||||||||||
String result = Observation.tryScopedChecked(parent, callable); | ||||||||||||||||||
assertThat(result).isEqualTo("test"); | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void checkedCallableShouldNotBeParentScopedIfParentIsNull() throws Throwable { | ||||||||||||||||||
Observation.CheckedCallable<String> callable = () -> { | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
return "test"; | ||||||||||||||||||
}; | ||||||||||||||||||
Observation.tryScopedChecked(null, callable); | ||||||||||||||||||
assertThat(registry.getCurrentObservation()).isNull(); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Test | ||||||||||||||||||
void observationFieldsShouldBeSetOnContext() { | ||||||||||||||||||
AssertingHandler assertingHandler = new AssertingHandler(); | ||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.