Browse Source

Polishing.

Remove BDD comments.
Minor formatting.

Original pull request #5083
See #5082
issue/aot-doc
Jens Schauder 2 months ago
parent
commit
d83754ecf9
No known key found for this signature in database
GPG Key ID: 2BE5D185CD2A1CE6
  1. 32
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/observability/MongoObservationCommandListenerTests.java

32
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/observability/MongoObservationCommandListenerTests.java

@ -75,6 +75,7 @@ class MongoObservationCommandListenerTests { @@ -75,6 +75,7 @@ class MongoObservationCommandListenerTests {
@AfterEach
void tearDown() {
Observation currentObservation = observationRegistry.getCurrentObservation();
if (currentObservation != null) {
currentObservation.stop();
@ -85,40 +86,32 @@ class MongoObservationCommandListenerTests { @@ -85,40 +86,32 @@ class MongoObservationCommandListenerTests {
@Test
void commandStartedShouldNotInstrumentWhenAdminDatabase() {
// when
listener.commandStarted(new CommandStartedEvent(null, 0, 0, null, "admin", "", null));
// then
assertThat(meterRegistry).hasNoMetrics();
}
@Test
void commandStartedShouldNotInstrumentWhenNoRequestContext() {
// when
listener.commandStarted(new CommandStartedEvent(null, 0, 0, null, "some name", "", null));
// then
assertThat(meterRegistry).hasNoMetrics();
}
@Test
void commandStartedShouldNotInstrumentWhenNoParentSampleInRequestContext() {
// when
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "", null));
// then
assertThat(meterRegistry).hasMeterWithName("spring.data.mongodb.command.active");
}
@Test // GH-4994
void commandStartedShouldAlwaysIncludeCollection() {
// when
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "hello", null));
// then
// although command 'hello' is collection-less, metric must have tag "db.mongodb.collection"
assertThat(meterRegistry).hasMeterWithNameAndTags(
"spring.data.mongodb.command.active",
@ -140,18 +133,15 @@ class MongoObservationCommandListenerTests { @@ -140,18 +133,15 @@ class MongoObservationCommandListenerTests {
new BsonDocument("collection", new BsonString("user"))));
listener.commandSucceeded(new CommandSucceededEvent(context, 0, 0, null, "insert", null, null, 0));
// then
assertThatTimerRegisteredWithTags();
}
@Test
void successfullyCompletedCommandShouldCreateTimerWhenParentSampleInRequestContext() {
// given
Observation parent = Observation.start("name", observationRegistry);
RequestContext traceRequestContext = getContext();
// when
listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, //
new ConnectionDescription( //
new ServerId( //
@ -160,18 +150,15 @@ class MongoObservationCommandListenerTests { @@ -160,18 +150,15 @@ class MongoObservationCommandListenerTests {
new BsonDocument("collection", new BsonString("user"))));
listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "insert", null, null, 0));
// then
assertThatTimerRegisteredWithTags();
}
@Test
void successfullyCompletedCommandWithCollectionHavingCommandNameShouldCreateTimerWhenParentSampleInRequestContext() {
// given
Observation parent = Observation.start("name", observationRegistry);
RequestContext traceRequestContext = getContext();
// when
listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, //
new ConnectionDescription( //
new ServerId( //
@ -181,18 +168,15 @@ class MongoObservationCommandListenerTests { @@ -181,18 +168,15 @@ class MongoObservationCommandListenerTests {
new BsonDocument("aggregate", new BsonString("user"))));
listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "aggregate", null, null, 0));
// then
assertThatTimerRegisteredWithTags();
}
@Test
void successfullyCompletedCommandWithoutClusterInformationShouldCreateTimerWhenParentSampleInRequestContext() {
// given
Observation parent = Observation.start("name", observationRegistry);
RequestContext traceRequestContext = getContext();
// when
listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, null, "database", "insert",
new BsonDocument("collection", new BsonString("user"))));
listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "insert", null, null, 0));
@ -207,11 +191,9 @@ class MongoObservationCommandListenerTests { @@ -207,11 +191,9 @@ class MongoObservationCommandListenerTests {
@Test
void commandWithErrorShouldCreateTimerWhenParentSampleInRequestContext() {
// given
Observation parent = Observation.start("name", observationRegistry);
RequestContext traceRequestContext = getContext();
// when
listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, //
new ConnectionDescription( //
new ServerId( //
@ -222,20 +204,17 @@ class MongoObservationCommandListenerTests { @@ -222,20 +204,17 @@ class MongoObservationCommandListenerTests {
listener.commandFailed( //
new CommandFailedEvent(traceRequestContext, 0, 0, null, "db", "insert", 0, new IllegalAccessException()));
// then
assertThatTimerRegisteredWithTags();
}
@Test // GH-4481
void completionShouldIgnoreIncompatibleObservationContext() {
// given
RequestContext traceRequestContext = getContext();
Observation observation = mock(Observation.class);
traceRequestContext.put(ObservationThreadLocalAccessor.KEY, observation);
// when
listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "insert", null, null, 0));
verify(observation).getContext();
@ -245,13 +224,11 @@ class MongoObservationCommandListenerTests { @@ -245,13 +224,11 @@ class MongoObservationCommandListenerTests {
@Test // GH-4481
void failureShouldIgnoreIncompatibleObservationContext() {
// given
RequestContext traceRequestContext = getContext();
Observation observation = mock(Observation.class);
traceRequestContext.put(ObservationThreadLocalAccessor.KEY, observation);
// when
listener.commandFailed(new CommandFailedEvent(traceRequestContext, 0, 0, null, "db", "insert", 0, null));
verify(observation).getContext();
@ -261,7 +238,6 @@ class MongoObservationCommandListenerTests { @@ -261,7 +238,6 @@ class MongoObservationCommandListenerTests {
@Test // GH-4321
void shouldUseObservationConvention() {
// given
MongoHandlerObservationConvention customObservationConvention = new MongoHandlerObservationConvention() {
@Override
public boolean supportsContext(Observation.Context context) {
@ -276,22 +252,18 @@ class MongoObservationCommandListenerTests { @@ -276,22 +252,18 @@ class MongoObservationCommandListenerTests {
this.listener = new MongoObservationCommandListener(observationRegistry, mock(ConnectionString.class),
customObservationConvention);
// when
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "", null));
// then
assertThat(meterRegistry).hasMeterWithName("custom.name.active");
}
@Test // GH-5064
void completionRestoresParentObservation() {
// given
Observation parent = Observation.start("name", observationRegistry);
observationRegistry.setCurrentObservationScope(parent.openScope());
RequestContext traceRequestContext = getContext();
// when
listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, null, "database", "insert",
new BsonDocument("collection", new BsonString("user"))));
@ -306,12 +278,10 @@ class MongoObservationCommandListenerTests { @@ -306,12 +278,10 @@ class MongoObservationCommandListenerTests {
@Test // GH-5064
void failureRestoresParentObservation() {
// given
Observation parent = Observation.start("name", observationRegistry);
observationRegistry.setCurrentObservationScope(parent.openScope());
RequestContext traceRequestContext = getContext();
// when
listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, null, "database", "insert",
new BsonDocument("collection", new BsonString("user"))));

Loading…
Cancel
Save