Skip to content

Commit f5f50b8

Browse files
authored
JAVA-6174 Fix sdam eventType to assert serverDescriptionChangedEvent (#1978)
1 parent 11a7b73 commit f5f50b8

4 files changed

Lines changed: 43 additions & 20 deletions

File tree

driver-sync/src/test/functional/com/mongodb/client/unified/ContextElement.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.mongodb.event.CommandFailedEvent;
2727
import com.mongodb.event.CommandStartedEvent;
2828
import com.mongodb.event.CommandSucceededEvent;
29+
import com.mongodb.event.ServerDescriptionChangedEvent;
2930
import com.mongodb.internal.logging.LogMessage;
3031
import com.mongodb.lang.Nullable;
3132
import org.bson.BsonArray;
@@ -509,6 +510,10 @@ private static BsonDocument connectionPoolEventToDocument(final Object event) {
509510
}
510511

511512
private static BsonDocument serverMonitorEventToDocument(final Object event) {
513+
// ServerDescriptionChangedEvent is not a heartbeat event and has no 'awaited' field.
514+
if (event instanceof ServerDescriptionChangedEvent) {
515+
return new BsonDocument(EventMatcher.getEventType(event.getClass()), new BsonDocument());
516+
}
512517
return new BsonDocument(EventMatcher.getEventType(event.getClass()),
513518
new BsonDocument("awaited", BsonBoolean.valueOf(EventMatcher.getAwaitedFromServerMonitorEvent(event))));
514519
}

driver-sync/src/test/functional/com/mongodb/client/unified/EventMatcher.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,13 @@ private static <T> boolean serverMonitorEventMatches(
513513
if (expectedEventContents.size() > 1) {
514514
throw new UnsupportedOperationException("Matching for the following event is not implemented " + expectedEventContents.toJson());
515515
}
516+
if (event instanceof ServerDescriptionChangedEvent) {
517+
boolean matches = serverDescriptionChangedEventMatches(expectedEventContents, (ServerDescriptionChangedEvent) event);
518+
if (context != null) {
519+
assertTrue(context.getMessage("Expected serverDescriptionChangedEvent contents to match"), matches);
520+
}
521+
return matches;
522+
}
516523
if (expectedEventContents.containsKey("awaited")) {
517524
boolean expectedAwaited = expectedEventContents.getBoolean("awaited").getValue();
518525
boolean actualAwaited = getAwaitedFromServerMonitorEvent(event);
@@ -545,7 +552,7 @@ static String getEventType(final Class<?> eventClass) {
545552
return eventClassName.replace("ConnectionPool", "pool");
546553
} else if (eventClassName.startsWith("Connection")) {
547554
return eventClassName.replace("Connection", "connection");
548-
} else if (eventClassName.startsWith("ServerHeartbeat")) {
555+
} else if (eventClassName.startsWith("Server")) {
549556
StringBuilder eventTypeBuilder = new StringBuilder(eventClassName);
550557
eventTypeBuilder.setCharAt(0, Character.toLowerCase(eventTypeBuilder.charAt(0)));
551558
return eventTypeBuilder.toString();

driver-sync/src/test/functional/com/mongodb/client/unified/UnifiedTest.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.mongodb.internal.connection.TestClusterListener;
4141
import com.mongodb.internal.connection.TestCommandListener;
4242
import com.mongodb.internal.connection.TestConnectionPoolListener;
43+
import com.mongodb.internal.connection.TestServerListener;
4344
import com.mongodb.internal.logging.LogMessage;
4445
import com.mongodb.lang.NonNull;
4546
import com.mongodb.lang.Nullable;
@@ -71,7 +72,6 @@
7172
import java.util.Collections;
7273
import java.util.HashSet;
7374
import java.util.List;
74-
import java.util.Map;
7575
import java.util.Set;
7676
import java.util.concurrent.ExecutionException;
7777
import java.util.concurrent.ExecutorService;
@@ -119,6 +119,7 @@ public abstract class UnifiedTest {
119119
private static final String TOPOLOGY_CLOSED_EVENT = "topologyClosedEvent";
120120
private static final List<String> TOPOLOGY_EVENT_NAMES = asList("topologyOpeningEvent", "topologyDescriptionChangedEvent",
121121
TOPOLOGY_CLOSED_EVENT);
122+
private static final String SERVER_DESCRIPTION_CHANGED_EVENT = "serverDescriptionChangedEvent";
122123

123124
public static final int RETRY_ATTEMPTS = 3;
124125
public static final int FORCE_FLAKY_ATTEMPTS = 10;
@@ -421,33 +422,46 @@ private void compareEvents(final UnifiedTestContext context, final BsonDocument
421422
context.getEventMatcher().assertConnectionPoolEventsEquality(client, ignoreExtraEvents, expectedEvents,
422423
listener.getEvents());
423424
} else if (eventType.equals("sdam")) {
425+
List<BsonDocument> expectedTopologyEvents = new ArrayList<>();
426+
List<BsonDocument> expectedServerDescriptionChangedEvents = new ArrayList<>();
427+
List<BsonDocument> expectedHeartbeatEvents = new ArrayList<>();
428+
429+
for (BsonValue event : expectedEvents) {
430+
BsonDocument doc = event.asDocument();
431+
if (TOPOLOGY_EVENT_NAMES.stream().anyMatch(doc::containsKey)) {
432+
expectedTopologyEvents.add(doc);
433+
} else if (doc.containsKey(SERVER_DESCRIPTION_CHANGED_EVENT)) {
434+
expectedServerDescriptionChangedEvents.add(doc);
435+
} else {
436+
expectedHeartbeatEvents.add(doc);
437+
}
438+
}
424439

425-
// SDAM tests also include topology events, so we need to separate them to be able to assert them separately.
426-
// Partition the expected events into two lists with the key being if it's a topology based event or not.
427-
Map<Boolean, List<BsonDocument>> partitionedEventsMap = expectedEvents.stream()
428-
.map(BsonValue::asDocument)
429-
.collect(Collectors.partitioningBy(doc -> TOPOLOGY_EVENT_NAMES.stream().anyMatch(doc::containsKey)));
430-
431-
BsonArray expectedTopologyEvents = new BsonArray(partitionedEventsMap.get(true));
432440
if (!expectedTopologyEvents.isEmpty()) {
433441
TestClusterListener clusterListener = entities.getClusterListener(client);
434-
// Unfortunately, some tests expect the cluster to be closed, but do not define it as a waitForEvent in the spec -
435-
// causing a race condition in the test.
436-
if (expectedTopologyEvents.stream().anyMatch(doc -> doc.asDocument().containsKey(TOPOLOGY_CLOSED_EVENT))) {
442+
// Race guard: some tests expect topologyClosedEvent without a prior waitForEvent.
443+
if (expectedTopologyEvents.stream().anyMatch(doc -> doc.containsKey(TOPOLOGY_CLOSED_EVENT))) {
437444
context.getEventMatcher().waitForClusterClosedEvent(client, clusterListener);
438445
}
439-
440446
List<Object> topologyEvents = new ArrayList<>();
441447
topologyEvents.add(clusterListener.getClusterOpeningEvent());
442448
topologyEvents.addAll(clusterListener.getClusterDescriptionChangedEvents());
443449
topologyEvents.add(clusterListener.getClusterClosingEvent());
444-
context.getEventMatcher().assertTopologyEventsEquality(client, ignoreExtraEvents, expectedTopologyEvents, topologyEvents);
450+
context.getEventMatcher().assertTopologyEventsEquality(client, ignoreExtraEvents,
451+
new BsonArray(expectedTopologyEvents), topologyEvents);
452+
}
453+
454+
if (!expectedServerDescriptionChangedEvents.isEmpty()) {
455+
TestServerListener serverListener = entities.getServerListener(client);
456+
context.getEventMatcher().assertServerMonitorEventsEquality(client, ignoreExtraEvents,
457+
new BsonArray(expectedServerDescriptionChangedEvents),
458+
serverListener.getServerDescriptionChangedEvents());
445459
}
446460

447-
BsonArray expectedSdamEvents = new BsonArray(partitionedEventsMap.get(false));
448-
if (!expectedSdamEvents.isEmpty()) {
461+
if (!expectedHeartbeatEvents.isEmpty()) {
449462
TestServerMonitorListener serverMonitorListener = entities.getServerMonitorListener(client);
450-
context.getEventMatcher().assertServerMonitorEventsEquality(client, ignoreExtraEvents, expectedSdamEvents, serverMonitorListener.getEvents());
463+
context.getEventMatcher().assertServerMonitorEventsEquality(client, ignoreExtraEvents,
464+
new BsonArray(expectedHeartbeatEvents), serverMonitorListener.getEvents());
451465
}
452466
} else {
453467
throw new UnsupportedOperationException("Unexpected event type: " + eventType);

driver-sync/src/test/functional/com/mongodb/client/unified/UnifiedTestModifications.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,6 @@ public static void applyCustomizations(final TestDef def) {
429429
.file("server-discovery-and-monitoring", "pool-clear-on-error-checkout");
430430
def.skipJira("https://jira.mongodb.org/browse/JAVA-5664")
431431
.file("server-discovery-and-monitoring", "pool-cleared-on-min-pool-size-population-error");
432-
// TODO-BACKPRESSURE Nabil - This issue is unrelated to backpressure but consider fixing it before merging to main
433-
def.skipJira("https://jira.mongodb.org/browse/JAVA-6174")
434-
.file("server-discovery-and-monitoring", "backpressure-server-description-unchanged-on-min-pool-size-population-error");
435432

436433
// session tests
437434
def.skipJira("https://jira.mongodb.org/browse/JAVA-5968")

0 commit comments

Comments
 (0)