Skip to content

Fix timing-related test failure in SessionsProseTest #1325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.mongodb.event.CommandStartedEvent;
import org.bson.BsonDocument;
import org.bson.Document;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.File;
Expand Down Expand Up @@ -55,9 +57,25 @@
public abstract class AbstractSessionsProseTest {

private static final int MONGOCRYPTD_PORT = 47017;
private static Process mongocryptdProcess;

protected abstract MongoClient getMongoClient(MongoClientSettings settings);

@BeforeAll
public static void beforeAll() throws IOException {
if (serverVersionAtLeast(4, 2)) {
mongocryptdProcess = startMongocryptdProcess();
}
}

@AfterAll
public static void afterAll() {
if (mongocryptdProcess != null) {
mongocryptdProcess.destroy();
mongocryptdProcess = null;
}
}

// Test 13 from #13-existing-sessions-are-not-checked-into-a-cleared-pool-after-forking
@Test
public void shouldCreateServerSessionOnlyAfterConnectionCheckout() throws InterruptedException {
Expand Down Expand Up @@ -119,78 +137,69 @@ public void commandStarted(final CommandStartedEvent event) {
@Test
public void shouldIgnoreImplicitSessionIfConnectionDoesNotSupportSessions() throws IOException {
assumeTrue(serverVersionAtLeast(4, 2));
Process mongocryptdProcess = startMongocryptdProcess("1");
try {
// initialize to true in case the command listener is never actually called, in which case the assertFalse will fire
AtomicBoolean containsLsid = new AtomicBoolean(true);
try (MongoClient client = getMongoClient(
getMongocryptdMongoClientSettingsBuilder()
.addCommandListener(new CommandListener() {
@Override
public void commandStarted(final CommandStartedEvent event) {
containsLsid.set(event.getCommand().containsKey("lsid"));
}
})
.build())) {

Document helloResponse = client.getDatabase("admin").runCommand(new Document("hello", 1));
assertFalse((helloResponse.containsKey("logicalSessionTimeoutMinutes")));

MongoCollection<Document> collection = client.getDatabase(getDefaultDatabaseName()).getCollection(getClass().getName());
try {
collection.find().first();
} catch (MongoCommandException e) {
// ignore command errors from mongocryptd
}
assertFalse(containsLsid.get());

// reset
containsLsid.set(true);
// initialize to true in case the command listener is never actually called, in which case the assertFalse will fire
AtomicBoolean containsLsid = new AtomicBoolean(true);
try (MongoClient client = getMongoClient(
getMongocryptdMongoClientSettingsBuilder()
.addCommandListener(new CommandListener() {
@Override
public void commandStarted(final CommandStartedEvent event) {
containsLsid.set(event.getCommand().containsKey("lsid"));
}
})
.build())) {

Document helloResponse = client.getDatabase("admin").runCommand(new Document("hello", 1));
assertFalse((helloResponse.containsKey("logicalSessionTimeoutMinutes")));

try {
collection.insertOne(new Document());
} catch (MongoCommandException e) {
// ignore command errors from mongocryptd
}
assertFalse(containsLsid.get());
MongoCollection<Document> collection = client.getDatabase(getDefaultDatabaseName()).getCollection(getClass().getName());
try {
collection.find().first();
} catch (MongoCommandException e) {
// ignore command errors from mongocryptd
}
} finally {
mongocryptdProcess.destroy();
assertFalse(containsLsid.get());

// reset
containsLsid.set(true);

try {
collection.insertOne(new Document());
} catch (MongoCommandException e) {
// ignore command errors from mongocryptd
}
assertFalse(containsLsid.get());
}
}

// Test 19 from #19-explicit-session-raises-an-error-if-connection-does-not-support-sessions
@Test
public void shouldThrowOnExplicitSessionIfConnectionDoesNotSupportSessions() throws IOException {
assumeTrue(serverVersionAtLeast(4, 2));
Process mongocryptdProcess = startMongocryptdProcess("2");
try {
try (MongoClient client = getMongoClient(getMongocryptdMongoClientSettingsBuilder().build())) {
MongoCollection<Document> collection = client.getDatabase(getDefaultDatabaseName()).getCollection(getClass().getName());

Document helloResponse = client.getDatabase("admin").runCommand(new Document("hello", 1));
assertFalse((helloResponse.containsKey("logicalSessionTimeoutMinutes")));

try (ClientSession session = client.startSession()) {
String expectedClientExceptionMessage =
"Attempting to use a ClientSession while connected to a server that doesn't support sessions";
try {
collection.find(session).first();
fail("Expected MongoClientException");
} catch (MongoClientException e) {
assertEquals(expectedClientExceptionMessage, e.getMessage());
}

try {
collection.insertOne(session, new Document());
fail("Expected MongoClientException");
} catch (MongoClientException e) {
assertEquals(expectedClientExceptionMessage, e.getMessage());
}
try (MongoClient client = getMongoClient(getMongocryptdMongoClientSettingsBuilder().build())) {
MongoCollection<Document> collection = client.getDatabase(getDefaultDatabaseName()).getCollection(getClass().getName());

Document helloResponse = client.getDatabase("admin").runCommand(new Document("hello", 1));
assertFalse((helloResponse.containsKey("logicalSessionTimeoutMinutes")));

try (ClientSession session = client.startSession()) {
String expectedClientExceptionMessage =
"Attempting to use a ClientSession while connected to a server that doesn't support sessions";
try {
collection.find(session).first();
fail("Expected MongoClientException");
} catch (MongoClientException e) {
assertEquals(expectedClientExceptionMessage, e.getMessage());
}

try {
collection.insertOne(session, new Document());
fail("Expected MongoClientException");
} catch (MongoClientException e) {
assertEquals(expectedClientExceptionMessage, e.getMessage());
}
}
} finally {
mongocryptdProcess.destroy();
}
}

Expand All @@ -200,10 +209,11 @@ private static MongoClientSettings.Builder getMongocryptdMongoClientSettingsBuil
builder.hosts(singletonList(new ServerAddress("localhost", MONGOCRYPTD_PORT))));
}

private static Process startMongocryptdProcess(final String pidSuffix) throws IOException {
private static Process startMongocryptdProcess() throws IOException {
String port = Integer.toString(MONGOCRYPTD_PORT);
ProcessBuilder processBuilder = new ProcessBuilder(asList("mongocryptd",
"--port", Integer.toString(MONGOCRYPTD_PORT),
"--pidfilepath", "mongocryptd-" + pidSuffix + ".pid"));
"--port", port,
"--pidfilepath", "mongocryptd-" + port + ".pid"));
processBuilder.redirectErrorStream(true);
processBuilder.redirectOutput(new File("/tmp/mongocryptd.log"));
return processBuilder.start();
Expand Down