Skip to content

Commit ac477f2

Browse files
stIncMalejyemin
authored andcommitted
Make ServerSelectionLoggingTest pass when run against a load balanced cluster, or when using Unix domain socket
JAVA-4754
1 parent 28b27df commit ac477f2

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,12 @@ private static boolean serverDescriptionChangedEventMatches(final BsonDocument e
313313
return true;
314314
}
315315
String newType = expectedEventContents.getDocument("newDescription").getString("type").getValue();
316-
//noinspection SwitchStatementWithTooFewBranches
317316
switch (newType) {
318317
case "Unknown":
319318
return event.getNewDescription().getType() == ServerType.UNKNOWN;
319+
case "LoadBalancer": {
320+
return event.getNewDescription().getType() == ServerType.LOAD_BALANCER;
321+
}
320322
default:
321323
throw new UnsupportedOperationException();
322324
}

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
package com.mongodb.client.unified;
1818

19+
import com.mongodb.Function;
1920
import com.mongodb.MongoCommandException;
2021
import com.mongodb.internal.ExceptionUtils.MongoCommandExceptionUtils;
2122
import com.mongodb.internal.logging.LogMessage;
23+
import com.mongodb.lang.Nullable;
2224
import org.bson.BsonArray;
2325
import org.bson.BsonBoolean;
2426
import org.bson.BsonDocument;
@@ -44,19 +46,20 @@ final class LogMatcher {
4446
this.context = context;
4547
}
4648

47-
void assertLogMessageEquality(final String client, final BsonArray expectedMessages, final List<LogMessage> actualMessages) {
49+
void assertLogMessageEquality(final String client, final BsonArray expectedMessages, final List<LogMessage> actualMessages,
50+
final Iterable<Tweak> tweaks) {
4851
context.push(ContextElement.ofLogMessages(client, expectedMessages, actualMessages));
4952

5053
assertEquals(context.getMessage("Number of log messages must be the same"), expectedMessages.size(), actualMessages.size());
5154

5255
for (int i = 0; i < expectedMessages.size(); i++) {
53-
BsonDocument expectedMessageAsDocument = expectedMessages.get(i).asDocument().clone();
54-
// `LogMessage.Entry.Name.OPERATION` is not supported, therefore we skip matching its value
55-
BsonValue expectedDataDocument = expectedMessageAsDocument.get("data");
56-
if (expectedDataDocument != null) {
57-
expectedDataDocument.asDocument().remove(LogMessage.Entry.Name.OPERATION.getValue());
56+
BsonDocument expectedMessage = expectedMessages.get(i).asDocument().clone();
57+
for (Tweak tweak : tweaks) {
58+
expectedMessage = tweak.apply(expectedMessage);
59+
}
60+
if (expectedMessage != null) {
61+
valueMatcher.assertValuesMatch(expectedMessage, asDocument(actualMessages.get(i)));
5862
}
59-
valueMatcher.assertValuesMatch(expectedMessageAsDocument, asDocument(actualMessages.get(i)));
6063
}
6164

6265
context.pop();
@@ -108,4 +111,27 @@ private static BsonValue asBsonValue(final Object value) {
108111
}
109112
}
110113

114+
interface Tweak extends Function<BsonDocument, BsonDocument> {
115+
/**
116+
* @param expectedMessage May be {@code null}, in which case the method simply returns {@code null}.
117+
* This method may mutate {@code expectedMessage}.
118+
* @return {@code null} iff matching {@code expectedMessage} with the actual message must be skipped.
119+
*/
120+
@Nullable
121+
BsonDocument apply(@Nullable BsonDocument expectedMessage);
122+
123+
static Tweak skip(final LogMessage.Entry.Name name) {
124+
return expectedMessage -> {
125+
if (expectedMessage == null) {
126+
return null;
127+
} else {
128+
BsonDocument expectedData = expectedMessage.getDocument("data", null);
129+
if (expectedData != null) {
130+
expectedData.remove(name.getValue());
131+
}
132+
return expectedMessage;
133+
}
134+
};
135+
}
136+
}
111137
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.mongodb.MongoClientSettings;
2121
import com.mongodb.MongoNamespace;
2222
import com.mongodb.ReadPreference;
23+
import com.mongodb.UnixServerAddress;
24+
import com.mongodb.internal.logging.LogMessage;
2325
import com.mongodb.logging.TestLoggingInterceptor;
2426
import com.mongodb.WriteConcern;
2527
import com.mongodb.client.ClientSession;
@@ -72,6 +74,7 @@
7274
import static com.mongodb.client.Fixture.getMongoClient;
7375
import static com.mongodb.client.Fixture.getMongoClientSettings;
7476
import static com.mongodb.client.unified.RunOnRequirementsMatcher.runOnRequirementsMet;
77+
import static java.util.Collections.singletonList;
7578
import static java.util.stream.Collectors.toList;
7679
import static org.junit.Assert.assertEquals;
7780
import static org.junit.Assert.assertFalse;
@@ -242,7 +245,14 @@ public void shouldPassAllOutcomes() {
242245
}
243246

244247
if (definition.containsKey("expectLogMessages")) {
245-
compareLogMessages(rootContext, definition);
248+
ArrayList<LogMatcher.Tweak> tweaks = new ArrayList<>(singletonList(
249+
// `LogMessage.Entry.Name.OPERATION` is not supported, therefore we skip matching its value
250+
LogMatcher.Tweak.skip(LogMessage.Entry.Name.OPERATION)));
251+
if (getMongoClientSettings().getClusterSettings()
252+
.getHosts().stream().anyMatch(serverAddress -> serverAddress instanceof UnixServerAddress)) {
253+
tweaks.add(LogMatcher.Tweak.skip(LogMessage.Entry.Name.SERVER_PORT));
254+
}
255+
compareLogMessages(rootContext, definition, tweaks);
246256
}
247257
}
248258

@@ -266,14 +276,15 @@ private void compareEvents(final UnifiedTestContext context, final BsonDocument
266276
}
267277
}
268278

269-
private void compareLogMessages(final UnifiedTestContext rootContext, final BsonDocument definition) {
279+
private void compareLogMessages(final UnifiedTestContext rootContext, final BsonDocument definition,
280+
final Iterable<LogMatcher.Tweak> tweaks) {
270281
for (BsonValue cur : definition.getArray("expectLogMessages")) {
271282
BsonDocument curLogMessagesForClient = cur.asDocument();
272283
String clientId = curLogMessagesForClient.getString("client").getValue();
273284
TestLoggingInterceptor loggingInterceptor =
274285
entities.getClientLoggingInterceptor(clientId);
275286
rootContext.getLogMatcher().assertLogMessageEquality(clientId, curLogMessagesForClient.getArray("messages"),
276-
loggingInterceptor.getMessages());
287+
loggingInterceptor.getMessages(), tweaks);
277288
}
278289
}
279290

0 commit comments

Comments
 (0)