Skip to content

Commit dc79944

Browse files
authored
Fix MongoDB container excessive CPU usage (#1146)
1 parent 9271e97 commit dc79944

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

.prettierrc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"printWidth": 120,
33
"trailingComma": "es5",
4-
"plugins": ["prettier-plugin-organize-imports"]
5-
}
4+
"plugins": [
5+
"prettier-plugin-organize-imports"
6+
],
7+
"endOfLine": "auto"
8+
}

packages/modules/mongodb/src/mongodb-container.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,36 @@ export class MongoDBContainer extends GenericContainer {
4949

5050
private withWaitForRsHealthCheck(): this {
5151
return this.withHealthCheck({
52-
test: [
53-
"CMD-SHELL",
54-
this.buildMongoEvalCommand(
55-
`'try { rs.initiate(); } catch (e){} while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'`
56-
),
57-
],
58-
interval: 250,
52+
test: ["CMD-SHELL", this.buildMongoEvalCommand(this.getRsInitCmd())],
53+
interval: 5000,
5954
timeout: 60000,
6055
retries: 1000,
6156
});
6257
}
6358

6459
private buildMongoEvalCommand(command: string) {
65-
const useMongosh = satisfies(this.imageName.tag, ">=5.0.0");
6660
const args = [];
67-
if (useMongosh) args.push("mongosh");
61+
if (this.isV5OrLater()) args.push("mongosh");
6862
else args.push("mongo", "admin");
6963
if (this.username && this.password) args.push("-u", this.username, "-p", this.password);
7064
args.push("--quiet", "--eval", command);
7165
return args.join(" ");
7266
}
67+
68+
private getRsInitCmd() {
69+
if (this.isV5OrLater())
70+
return `'try { rs.status(); } catch (e) { rs.initiate(); } while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'`;
71+
else
72+
return `'try { rs.initiate(); } catch (e) {} while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'`;
73+
}
74+
75+
private isV5OrLater() {
76+
try {
77+
return satisfies(this.imageName.tag, ">=5.0.0");
78+
} catch {
79+
return false;
80+
}
81+
}
7382
}
7483

7584
export class StartedMongoDBContainer extends AbstractStartedContainer {

packages/testcontainers/src/utils/test-helper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { GetEventsOptions, ImageInspectInfo } from "dockerode";
22
import { createServer, Server } from "http";
33
import { createSocket } from "node:dgram";
44
import fs from "node:fs";
5+
import { EOL } from "node:os";
56
import path from "node:path";
67
import { Readable } from "stream";
78
import { Agent, request } from "undici";
@@ -14,7 +15,7 @@ import { StartedTestContainer } from "../test-container";
1415
export const getImage = (dirname: string, index = 0): string => {
1516
return fs
1617
.readFileSync(path.resolve(dirname, "..", "Dockerfile"), "utf-8")
17-
.split("\n")
18+
.split(EOL)
1819
[index].split(" ")[1];
1920
};
2021

0 commit comments

Comments
 (0)