Skip to content

Commit

Permalink
Add tests for history-tracking of ObservationValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-ivanov committed Aug 8, 2024
1 parent 982246c commit 294a5cd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ private StackTraceElement[] findRelevantStackTraceElements(StackTraceElement[] s
private int findFirstRelevantStackTraceElementIndex(StackTraceElement[] stackTrace) {
int index = -1;
for (int i = 0; i < stackTrace.length; i++) {
String className = stackTrace[i].getClassName();
if (className.equals(Observation.class.getName())
|| className.equals("io.micrometer.observation.SimpleObservation")) {
if (isObservationRelated(stackTrace[i])) {
// the first relevant StackTraceElement is after the last Observation
index = i + 1;
}
Expand All @@ -91,6 +89,13 @@ private int findFirstRelevantStackTraceElementIndex(StackTraceElement[] stackTra
return (index >= stackTrace.length) ? -1 : index;
}

private boolean isObservationRelated(StackTraceElement stackTraceElement) {
String className = stackTraceElement.getClassName();
return className.equals(Observation.class.getName())
|| className.equals("io.micrometer.observation.SimpleObservation")
|| className.startsWith("io.micrometer.observation.SimpleObservation$");
}

public EventName getEventName() {
return eventName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.micrometer.observation.ObservationRegistry;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
Expand All @@ -37,31 +38,44 @@ void doubleStartShouldBeInvalid() {
assertThatThrownBy(() -> Observation.start("test", registry).start())
.isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid start: Observation has already been started");
.hasMessage("Invalid start: Observation has already been started")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid start: Observation has already been started\n"
+ "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n"
+ "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$"));
}

@Test
void stopBeforeStartShouldBeInvalid() {
assertThatThrownBy(() -> Observation.createNotStarted("test", registry).stop())
.isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid stop: Observation has not been started yet");
.hasMessage("Invalid stop: Observation has not been started yet")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid stop: Observation has not been started yet\n"
+ "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$stopBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$"));
}

@Test
void errorBeforeStartShouldBeInvalid() {
assertThatThrownBy(() -> Observation.createNotStarted("test", registry).error(new RuntimeException()))
.isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid error signal: Observation has not been started yet");
.hasMessage("Invalid error signal: Observation has not been started yet")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid error signal: Observation has not been started yet\n"
+ "ERROR: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$errorBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$"));
}

@Test
void eventBeforeStartShouldBeInvalid() {
assertThatThrownBy(() -> Observation.createNotStarted("test", registry).event(Event.of("test")))
.isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid event signal: Observation has not been started yet");
.hasMessage("Invalid event signal: Observation has not been started yet")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid event signal: Observation has not been started yet\n"
+ "EVENT: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$eventBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$"));
}

@Test
Expand All @@ -71,15 +85,22 @@ void scopeBeforeStartShouldBeInvalid() {
assertThatThrownBy(() -> Observation.createNotStarted("test", registry).openScope())
.isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid scope opening: Observation has not been started yet");
.hasMessage("Invalid scope opening: Observation has not been started yet")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope opening: Observation has not been started yet\n"
+ "SCOPE_OPEN: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$"));
}

@Test
void observeAfterStartShouldBeInvalid() {
assertThatThrownBy(() -> Observation.start("test", registry).observe(() -> ""))
.isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid start: Observation has already been started");
.hasMessage("Invalid start: Observation has already been started")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid start: Observation has already been started\n"
+ "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$observeAfterStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n"
+ "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$observeAfterStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$"));
}

@Test
Expand All @@ -90,7 +111,12 @@ void doubleStopShouldBeInvalid() {
observation.stop();
}).isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid stop: Observation has already been stopped");
.hasMessage("Invalid stop: Observation has already been stopped")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid stop: Observation has already been stopped\n"
+ "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n"
+ "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n"
+ "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$"));
}

@Test
Expand All @@ -101,7 +127,12 @@ void errorAfterStopShouldBeInvalid() {
observation.error(new RuntimeException());
}).isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid error signal: Observation has already been stopped");
.hasMessage("Invalid error signal: Observation has already been stopped")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid error signal: Observation has already been stopped\n"
+ "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$errorAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n"
+ "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$errorAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n"
+ "ERROR: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$errorAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$"));
}

@Test
Expand All @@ -112,7 +143,12 @@ void eventAfterStopShouldBeInvalid() {
observation.event(Event.of("test"));
}).isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid event signal: Observation has already been stopped");
.hasMessage("Invalid event signal: Observation has already been stopped")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid event signal: Observation has already been stopped\n"
+ "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$eventAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n"
+ "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$eventAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n"
+ "EVENT: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$eventAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$"));
}

@Test
Expand All @@ -124,7 +160,12 @@ void scopeOpenAfterStopShouldBeInvalid() {
observation.openScope();
}).isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid scope opening: Observation has already been stopped");
.hasMessage("Invalid scope opening: Observation has already been stopped")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope opening: Observation 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+\\)$"));
}

@Test
Expand All @@ -137,7 +178,13 @@ void scopeResetAfterStopShouldBeInvalid() {
scope.reset();
}).isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid scope resetting: Observation has already been stopped");
.hasMessage("Invalid scope resetting: Observation has already been stopped")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope resetting: Observation 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+\\)$"));
}

@Test
Expand All @@ -149,7 +196,13 @@ void scopeCloseAfterStopShouldBeInvalid() {
scope.close();
}).isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid scope closing: Observation has already been stopped");
.hasMessage("Invalid scope closing: Observation has already been stopped")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope closing: Observation 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+\\)$"));
}

@Test
Expand Down

0 comments on commit 294a5cd

Please sign in to comment.