Skip to content
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

feat: enable reuse for mongodb #6235

Merged
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions modules/mongodb/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ dependencies {
testImplementation("org.mongodb:mongodb-driver-sync:4.8.0")
testImplementation 'org.assertj:assertj-core:3.23.1'
}

tasks.japicmp {
methodExcludes = [
"org.testcontainers.containers.MongoDBContainer#containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse)"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ public String getReplicaSetUrl(final String databaseName) {
}

@Override
protected void containerIsStarted(InspectContainerResponse containerInfo) {
initReplicaSet();
protected void containerIsStarted(InspectContainerResponse containerInfo, boolean reused) {
if (reused && isReplicationSetAlreadyInitialized()) {
log.debug("Replica set already initialized.");
} else {
initReplicaSet();
}
}

private String[] buildMongoEvalCommand(final String command) {
Expand Down Expand Up @@ -151,4 +155,13 @@ public static class ReplicaSetInitializationException extends RuntimeException {
super(errorMessage);
}
}

@SneakyThrows
private boolean isReplicationSetAlreadyInitialized() {
// since we are creating a replica set with one node, this node must be primary (state = 1)
final ExecResult execCheckRsInit = execInContainer(
buildMongoEvalCommand("if(db.adminCommand({replSetGetStatus: 1})['myState'] != 1) quit(900)")
);
return execCheckRsInit.getExitCode() == CONTAINER_EXIT_CODE_OK;
}
}