Skip to content

Commit 1acde34

Browse files
committed
adding a busy waiting for pinging if the mongoDB is ready before finishing the starting of the SUT
1 parent 91eded8 commit 1acde34

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

jdk_11_gradle/em/embedded/rest/reservations-api/src/main/java/em/embedded/reservationsapi/EmbeddedEvoMasterController.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,34 @@ public String startSut() {
9696
"--app.jwt.secret=abcdef012345678901234567890123456789abcdef012345678901234567890123456789"
9797
});
9898

99+
try {
100+
Thread.sleep(3_000);
101+
} catch (InterruptedException e) {
102+
// do nothing
103+
}
104+
105+
while (!isMongoClientReady()) {
106+
try {
107+
Thread.sleep(1_000);
108+
} catch (InterruptedException e) {
109+
// do nothing
110+
}
111+
}
112+
99113
return "http://localhost:" + getSutPort();
100114
}
101115

116+
private boolean isMongoClientReady() {
117+
try {
118+
MongoDatabase db = mongoClient.getDatabase(MONGODB_DATABASE_NAME);
119+
Document pingResult = db.runCommand(new Document("ping", 1));
120+
return pingResult.getDouble("ok") == 1.0;
121+
} catch (Exception ex) {
122+
// Connection error
123+
return false;
124+
}
125+
}
126+
102127
protected int getSutPort() {
103128
return (Integer) ((Map) ctx.getEnvironment()
104129
.getPropertySources().get("server.ports").getSource())

0 commit comments

Comments
 (0)