Skip to content

Commit 9d56da0

Browse files
committed
Address comments
1 parent 095256c commit 9d56da0

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

src/common/atlas/cluster.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
import type { ClusterDescription20240805, FlexClusterDescription20241113 } from "./openapi.js";
22
import type { ApiClient } from "./apiClient.js";
33
import { LogId } from "../logger.js";
4+
import { ConnectionString } from "mongodb-connection-string-url";
45

5-
const DEFAULT_PORT = "27017";
6+
type AtlasProcessId = `${string}:${number}`;
67

7-
function extractProcessIds(connectionString: string): Array<string> {
8-
if (!connectionString || connectionString === "") return [];
9-
10-
// Extract host:port pairs from connection string
11-
const matches = connectionString.match(/^mongodb:\/\/([^/]+)/);
12-
if (!matches) {
8+
function extractProcessIds(connectionString: string): Array<AtlasProcessId> {
9+
if (!connectionString) {
1310
return [];
1411
}
15-
16-
// matches[1] gives us the host:port pairs
17-
const hostsString = matches[1];
18-
const hosts = hostsString?.split(",") ?? [];
19-
20-
return hosts?.map((host) => {
21-
const [hostname, port] = host.split(":");
22-
return `${hostname}:${port || DEFAULT_PORT}`;
23-
});
12+
const connectionStringUrl = new ConnectionString(connectionString);
13+
return connectionStringUrl.hosts as Array<AtlasProcessId>;
2414
}
2515
export interface Cluster {
2616
name?: string;

tests/integration/tools/atlas/atlasHelpers.ts

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,32 @@ export function sleep(ms: number): Promise<void> {
149149
return new Promise((resolve) => setTimeout(resolve, ms));
150150
}
151151

152+
export async function assertClusterIsAvailable(
153+
session: Session,
154+
projectId: string,
155+
clusterName: string
156+
): Promise<boolean> {
157+
try {
158+
await session.apiClient.getCluster({
159+
params: {
160+
path: {
161+
groupId: projectId,
162+
clusterName,
163+
},
164+
},
165+
});
166+
return true;
167+
} catch {
168+
return false;
169+
}
170+
}
171+
152172
export async function deleteAndWaitCluster(
153173
session: Session,
154174
projectId: string,
155175
clusterName: string,
156-
pollingInterval: number = 1000
176+
pollingInterval: number = 1000,
177+
maxPollingIterations: number = 300
157178
): Promise<void> {
158179
await session.apiClient.deleteCluster({
159180
params: {
@@ -163,31 +184,28 @@ export async function deleteAndWaitCluster(
163184
},
164185
},
165186
});
166-
while (true) {
167-
try {
168-
await session.apiClient.getCluster({
169-
params: {
170-
path: {
171-
groupId: projectId,
172-
clusterName,
173-
},
174-
},
175-
});
176-
await sleep(pollingInterval);
177-
} catch {
178-
break;
187+
188+
for (let i = 0; i < maxPollingIterations; i++) {
189+
const isAvailable = await assertClusterIsAvailable(session, projectId, clusterName);
190+
if (!isAvailable) {
191+
return;
179192
}
193+
await sleep(pollingInterval);
180194
}
195+
throw new Error(
196+
`Cluster deletion timeout: ${clusterName} did not delete within ${maxPollingIterations} iterations`
197+
);
181198
}
182199

183200
export async function waitCluster(
184201
session: Session,
185202
projectId: string,
186203
clusterName: string,
187204
check: (cluster: ClusterDescription20240805) => boolean | Promise<boolean>,
188-
pollingInterval: number = 1000
205+
pollingInterval: number = 1000,
206+
maxPollingIterations: number = 300
189207
): Promise<void> {
190-
while (true) {
208+
for (let i = 0; i < maxPollingIterations; i++) {
191209
const cluster = await session.apiClient.getCluster({
192210
params: {
193211
path: {
@@ -201,4 +219,8 @@ export async function waitCluster(
201219
}
202220
await sleep(pollingInterval);
203221
}
222+
223+
throw new Error(
224+
`Cluster wait timeout: ${clusterName} did not meet condition within ${maxPollingIterations} iterations`
225+
);
204226
}

tests/integration/tools/atlas/performanceAdvisor.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describeWithAtlas("performanceAdvisor", (integration) => {
1414
const projectId = getProjectId();
1515
if (projectId) {
1616
const session: Session = integration.mcpServer().session;
17-
await deleteAndWaitCluster(session, projectId, clusterName, 1000);
17+
await deleteAndWaitCluster(session, projectId, clusterName, 1000, 1200);
1818
}
1919
}, 1200000);
2020

@@ -62,7 +62,8 @@ describeWithAtlas("performanceAdvisor", (integration) => {
6262
(cluster) => {
6363
return cluster.stateName === "IDLE";
6464
},
65-
10000
65+
10000,
66+
120
6667
);
6768

6869
await sleep(10000);

0 commit comments

Comments
 (0)