File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed
src/main/java/org/testcontainers/containers Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -6,3 +6,9 @@ dependencies {
66 testImplementation(" org.mongodb:mongodb-driver-sync:4.8.0" )
77 testImplementation ' org.assertj:assertj-core:3.23.1'
88}
9+
10+ tasks. japicmp {
11+ methodExcludes = [
12+ " org.testcontainers.containers.MongoDBContainer#containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse)"
13+ ]
14+ }
Original file line number Diff line number Diff line change @@ -82,8 +82,12 @@ public String getReplicaSetUrl(final String databaseName) {
8282 }
8383
8484 @ Override
85- protected void containerIsStarted (InspectContainerResponse containerInfo ) {
86- initReplicaSet ();
85+ protected void containerIsStarted (InspectContainerResponse containerInfo , boolean reused ) {
86+ if (reused && isReplicationSetAlreadyInitialized ()) {
87+ log .debug ("Replica set already initialized." );
88+ } else {
89+ initReplicaSet ();
90+ }
8791 }
8892
8993 private String [] buildMongoEvalCommand (final String command ) {
@@ -151,4 +155,13 @@ public static class ReplicaSetInitializationException extends RuntimeException {
151155 super (errorMessage );
152156 }
153157 }
158+
159+ @ SneakyThrows
160+ private boolean isReplicationSetAlreadyInitialized () {
161+ // since we are creating a replica set with one node, this node must be primary (state = 1)
162+ final ExecResult execCheckRsInit = execInContainer (
163+ buildMongoEvalCommand ("if(db.adminCommand({replSetGetStatus: 1})['myState'] != 1) quit(900)" )
164+ );
165+ return execCheckRsInit .getExitCode () == CONTAINER_EXIT_CODE_OK ;
166+ }
154167}
You can’t perform that action at this time.
0 commit comments