diff --git a/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/ObservationValidator.java b/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/ObservationValidator.java index 174504a94..1c701bfee 100644 --- a/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/ObservationValidator.java +++ b/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/ObservationValidator.java @@ -84,19 +84,22 @@ public void onEvent(Event event, Context context) { @Override public void onScopeOpened(Context context) { addHistoryElement(context, EventName.SCOPE_OPEN); - checkIfObservationWasStartedButNotStopped("Invalid scope opening", context); + // In some cases (Reactor) scope open can happen after the observation is stopped + checkIfObservationWasStarted("Invalid scope opening", context); } @Override public void onScopeClosed(Context context) { addHistoryElement(context, EventName.SCOPE_CLOSE); - checkIfObservationWasStartedButNotStopped("Invalid scope closing", context); + // In some cases (Reactor) scope close can happen after the observation is stopped + checkIfObservationWasStarted("Invalid scope closing", context); } @Override public void onScopeReset(Context context) { addHistoryElement(context, EventName.SCOPE_RESET); - checkIfObservationWasStartedButNotStopped("Invalid scope resetting", context); + // In some cases (Reactor) scope reset can happen after the observation is stopped + checkIfObservationWasStarted("Invalid scope resetting", context); } @Override @@ -122,13 +125,20 @@ private void addHistoryElement(Context context, EventName eventName) { } @Nullable - private Status checkIfObservationWasStartedButNotStopped(String prefix, Context context) { + private Status checkIfObservationWasStarted(String prefix, Context context) { Status status = context.get(Status.class); if (status == null) { consumer.accept(new ValidationResult( prefix + ": Observation '" + context.getName() + "' has not been started yet", context)); } - else if (status.isStopped()) { + + return status; + } + + @Nullable + private Status checkIfObservationWasStartedButNotStopped(String prefix, Context context) { + Status status = checkIfObservationWasStarted(prefix, context); + if (status != null && status.isStopped()) { consumer.accept(new ValidationResult( prefix + ": Observation '" + context.getName() + "' has already been stopped", context)); } diff --git a/micrometer-observation-test/src/test/java/io/micrometer/observation/tck/ObservationValidatorTests.java b/micrometer-observation-test/src/test/java/io/micrometer/observation/tck/ObservationValidatorTests.java index 28f5b271d..eeb5c5b06 100644 --- a/micrometer-observation-test/src/test/java/io/micrometer/observation/tck/ObservationValidatorTests.java +++ b/micrometer-observation-test/src/test/java/io/micrometer/observation/tck/ObservationValidatorTests.java @@ -154,56 +154,27 @@ void eventAfterStopShouldBeInvalid() { @Test @SuppressWarnings("resource") - void scopeOpenAfterStopShouldBeInvalid() { - assertThatThrownBy(() -> { - Observation observation = Observation.start("test", registry); - observation.stop(); - observation.openScope(); - }).isExactlyInstanceOf(InvalidObservationException.class) - .hasNoCause() - .hasMessage("Invalid scope opening: Observation 'test' has already been stopped") - .satisfies(exception -> assertThat(exception.toString()).matches( - "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope opening: Observation 'test' has already been stopped\n" - + "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeOpenAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" - + "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeOpenAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" - + "SCOPE_OPEN: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeOpenAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); + void scopeOpenAfterStopShouldBeValid() { + Observation observation = Observation.start("test", registry); + observation.stop(); + observation.openScope(); } @Test @SuppressWarnings("resource") - void scopeResetAfterStopShouldBeInvalid() { - assertThatThrownBy(() -> { - Observation observation = Observation.start("test", registry); - Scope scope = observation.openScope(); - observation.stop(); - scope.reset(); - }).isExactlyInstanceOf(InvalidObservationException.class) - .hasNoCause() - .hasMessage("Invalid scope resetting: Observation 'test' has already been stopped") - .satisfies(exception -> assertThat(exception.toString()).matches( - "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope resetting: Observation 'test' has already been stopped\n" - + "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeResetAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" - + "SCOPE_OPEN: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeResetAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)\n" - + "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeResetAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" - + "SCOPE_RESET: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeResetAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); + void scopeResetAfterStopShouldBeValid() { + Observation observation = Observation.start("test", registry); + Scope scope = observation.openScope(); + observation.stop(); + scope.reset(); } @Test - void scopeCloseAfterStopShouldBeInvalid() { - assertThatThrownBy(() -> { - Observation observation = Observation.start("test", registry); - Scope scope = observation.openScope(); - observation.stop(); - scope.close(); - }).isExactlyInstanceOf(InvalidObservationException.class) - .hasNoCause() - .hasMessage("Invalid scope closing: Observation 'test' has already been stopped") - .satisfies(exception -> assertThat(exception.toString()).matches( - "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope closing: Observation 'test' has already been stopped\n" - + "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeCloseAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" - + "SCOPE_OPEN: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeCloseAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)\n" - + "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeCloseAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" - + "SCOPE_CLOSE: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeCloseAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); + void scopeCloseAfterStopShouldBeValid() { + Observation observation = Observation.start("test", registry); + Scope scope = observation.openScope(); + observation.stop(); + scope.close(); } @Test