Skip to content

Commit 35fe3bb

Browse files
committed
test: fix flaky tests
those were failing almost always locally on faster machines like mb m4 pro
1 parent d2a1eb7 commit 35fe3bb

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

test/core/crawlers/basic_crawler.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -965,11 +965,11 @@ describe('BasicCrawler', () => {
965965
requestQueue.fetchNextRequest = async () => queue.pop()!;
966966
requestQueue.isEmpty = async () => Promise.resolve(!queue.length);
967967

968-
setTimeout(() => queue.push(request0), 10);
969-
setTimeout(() => queue.push(request1), 100);
968+
setTimeout(() => queue.push(request0), 50);
969+
setTimeout(() => queue.push(request1), 150);
970970
setTimeout(() => {
971971
isFinished = true;
972-
}, 150);
972+
}, 500);
973973

974974
await basicCrawler.run();
975975

@@ -1020,7 +1020,7 @@ describe('BasicCrawler', () => {
10201020
setTimeout(() => queue.push(request1), 100);
10211021
setTimeout(() => {
10221022
void basicCrawler.teardown();
1023-
}, 300);
1023+
}, 500);
10241024

10251025
await basicCrawler.run();
10261026

test/core/sitemap_request_list.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ describe('SitemapRequestList', () => {
371371
signal: controller.signal,
372372
});
373373

374-
await sleep(50); // Loads the first sub-sitemap, but not the second
374+
await sleep(100); // Loads the first sub-sitemap, but not the second
375375
controller.abort();
376376

377377
for await (const request of list) {

test/utils/fixtures/child.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// dummy script for testing ps-tree.ts
22
setTimeout(() => {
33
/* Does nothing, but prevents exit */
4-
}, 1000);
4+
}, 5000);

test/utils/psTree.test.ts

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,42 @@ const scripts = {
88
child: path.join(__dirname, 'fixtures', 'child.js'),
99
};
1010

11+
// Helper to poll for child processes
12+
async function waitForChildren(pid: number, maxWaitMs = 3000): Promise<any[]> {
13+
const startTime = Date.now();
14+
while (Date.now() - startTime < maxWaitMs) {
15+
const children = await psTree(pid);
16+
if (children.length > 0) {
17+
return children;
18+
}
19+
await new Promise((resolve) => setTimeout(resolve, 100));
20+
}
21+
return psTree(pid); // Final attempt
22+
}
23+
24+
// Helper to poll for processes to terminate
25+
async function waitForTermination(pid: number, maxWaitMs = 10000): Promise<void> {
26+
const startTime = Date.now();
27+
while (Date.now() - startTime < maxWaitMs) {
28+
const children = await psTree(pid);
29+
if (children.length === 0) {
30+
return;
31+
}
32+
await new Promise((resolve) => setTimeout(resolve, 100));
33+
}
34+
}
35+
1136
describe('psTree()', () => {
1237
test('Spawn a Parent process which has Ten Child Processes', async () => {
1338
const parent = exec(`node ${scripts.parent}`);
1439

15-
// Wait for the child process(es) to spawn.
16-
await new Promise((resolve) => setTimeout(resolve, 500));
17-
18-
const children = await psTree(parent.pid!);
40+
// Poll until child processes are detected
41+
const children = await waitForChildren(parent.pid!);
1942

2043
expect(children.length).toBeGreaterThan(0);
2144

22-
// Allow time for the processes to be terminated.
23-
await new Promise((resolve) => setTimeout(resolve, 2000));
45+
// Poll until processes terminate
46+
await waitForTermination(parent.pid!);
2447

2548
const postKillChildren = await psTree(parent.pid!);
2649

@@ -30,17 +53,27 @@ describe('psTree()', () => {
3053
test('Includes itself if includeRoot is true', async () => {
3154
const parent = exec(`node ${scripts.parent}`);
3255

33-
// Wait for the child process(es) to spawn.
34-
await new Promise((resolve) => setTimeout(resolve, 500));
35-
36-
const processes = await psTree(parent.pid!, true);
56+
// Poll until processes are detected (parent + children, so > 1)
57+
let processes: any[] = [];
58+
const startTime = Date.now();
59+
while (Date.now() - startTime < 3000) {
60+
processes = await psTree(parent.pid!, true);
61+
// Wait for parent + at least one child (length > 1)
62+
if (processes.length > 1) {
63+
break;
64+
}
65+
await new Promise((resolve) => setTimeout(resolve, 100));
66+
}
67+
if (processes.length <= 1) {
68+
processes = await psTree(parent.pid!, true); // Final attempt
69+
}
3770

3871
const parentProcess = processes.find((process) => Number.parseInt(process.PID, 10) === parent.pid!);
3972

4073
expect(parentProcess).toBeDefined();
4174

42-
// Allow time for the processes to be terminated.
43-
await new Promise((resolve) => setTimeout(resolve, 2000));
75+
// Poll until processes terminate
76+
await waitForTermination(parent.pid!);
4477

4578
const postKillChildren = await psTree(parent.pid!);
4679

0 commit comments

Comments
 (0)