Skip to content

Commit 486b41d

Browse files
strogiyotecAlmas Abdrazak
andauthored
Fix flaky test #JAVA-6016 (#1845)
* Fix flaky test #JAVA-6016 The test shouldUseConnectTimeoutMsWhenEstablishingConnectionInBackground only fails with a variant tests-require-api-version The reason is that this variant passes -Dorg.mongodb.test.api.version=1 which makes an API version to be V1 In this version the hello command is `hello` but this test case assumes it's `isMaster` * remove unused import --------- Co-authored-by: Almas Abdrazak <abdrazak.almas@mongodb.com>
1 parent 057649f commit 486b41d

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

driver-core/src/main/com/mongodb/internal/connection/CommandHelper.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import com.mongodb.ServerApi;
2121
import com.mongodb.connection.ClusterConnectionMode;
2222
import com.mongodb.internal.TimeoutContext;
23+
import com.mongodb.internal.VisibleForTesting;
24+
25+
import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE;
26+
2327
import com.mongodb.internal.async.SingleResultCallback;
2428
import com.mongodb.internal.validator.NoOpFieldNameValidator;
2529
import com.mongodb.lang.Nullable;
@@ -38,8 +42,10 @@
3842
*/
3943
public final class CommandHelper {
4044

41-
static final String HELLO = "hello";
42-
static final String LEGACY_HELLO = "isMaster";
45+
@VisibleForTesting(otherwise = PRIVATE)
46+
public static final String HELLO = "hello";
47+
@VisibleForTesting(otherwise = PRIVATE)
48+
public static final String LEGACY_HELLO = "isMaster";
4349
static final String LEGACY_HELLO_LOWER = LEGACY_HELLO.toLowerCase(Locale.ROOT);
4450

4551
static BsonDocument executeCommand(final String database, final BsonDocument command, final ClusterConnectionMode clusterConnectionMode,

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
import com.mongodb.event.ConnectionClosedEvent;
4848
import com.mongodb.event.ConnectionCreatedEvent;
4949
import com.mongodb.event.ConnectionReadyEvent;
50+
51+
import static com.mongodb.internal.connection.CommandHelper.HELLO;
52+
import static com.mongodb.internal.connection.CommandHelper.LEGACY_HELLO;
53+
5054
import com.mongodb.internal.connection.InternalStreamConnection;
5155
import com.mongodb.internal.connection.ServerHelper;
5256
import com.mongodb.internal.connection.TestCommandListener;
@@ -1051,8 +1055,7 @@ public void shouldUseConnectTimeoutMsWhenEstablishingConnectionInBackground() {
10511055
} finally {
10521056
InternalStreamConnection.setRecordEverything(false);
10531057
}
1054-
1055-
List<CommandFailedEvent> commandFailedEvents = commandListener.getCommandFailedEvents("isMaster");
1058+
List<CommandFailedEvent> commandFailedEvents = commandListener.getCommandFailedEvents(getHandshakeCommandName());
10561059
assertFalse(commandFailedEvents.isEmpty());
10571060
assertInstanceOf(MongoOperationTimeoutException.class, commandFailedEvents.get(0).getThrowable());
10581061
}
@@ -1136,8 +1139,15 @@ private MongoClient createMongoClient(final MongoClientSettings.Builder builder)
11361139
return createMongoClient(builder.build());
11371140
}
11381141

1139-
private long msElapsedSince(final long t1) {
1142+
private long msElapsedSince(final long t1) {
11401143
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t1);
11411144
}
11421145

1146+
/**
1147+
* Get the handshake command name based on the server API version.
1148+
* @return the handshake command name
1149+
*/
1150+
private String getHandshakeCommandName() {
1151+
return ClusterFixture.getServerApi() == null ? LEGACY_HELLO : HELLO;
1152+
}
11431153
}

0 commit comments

Comments
 (0)