Skip to content

Commit 01ea0b8

Browse files
authored
Address flaky integration tests with retries (google-gemini#4604)
1 parent 12765eb commit 01ea0b8

File tree

1 file changed

+81
-46
lines changed

1 file changed

+81
-46
lines changed

integration-tests/run-tests.js

Lines changed: 81 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ async function main() {
6161
console.log(`\tFound test file: ${testFileName}`);
6262
}
6363

64+
const MAX_RETRIES = 3;
6465
let allTestsPassed = true;
6566

6667
for (const testFile of testFiles) {
@@ -72,63 +73,97 @@ async function main() {
7273
`------------- Running test file: ${testFileName} ------------------------------`,
7374
);
7475

75-
const nodeArgs = ['--test'];
76-
if (verbose) {
77-
nodeArgs.push('--test-reporter=spec');
78-
}
79-
nodeArgs.push(testFile);
80-
81-
const child = spawn('node', nodeArgs, {
82-
stdio: 'pipe',
83-
env: {
84-
...process.env,
85-
GEMINI_CLI_INTEGRATION_TEST: 'true',
86-
INTEGRATION_TEST_FILE_DIR: testFileDir,
87-
KEEP_OUTPUT: keepOutput.toString(),
88-
VERBOSE: verbose.toString(),
89-
TEST_FILE_NAME: testFileName,
90-
},
91-
});
92-
93-
let outputStream;
94-
if (keepOutput) {
95-
const outputFile = join(testFileDir, 'output.log');
96-
outputStream = createWriteStream(outputFile);
97-
console.log(`Output for ${testFileName} written to: ${outputFile}`);
98-
}
99-
100-
child.stdout.on('data', (data) => {
101-
if (verbose) {
102-
process.stdout.write(data);
103-
}
104-
if (outputStream) {
105-
outputStream.write(data);
76+
let attempt = 0;
77+
let testFilePassed = false;
78+
let lastStdout = [];
79+
let lastStderr = [];
80+
81+
while (attempt < MAX_RETRIES && !testFilePassed) {
82+
attempt++;
83+
if (attempt > 1) {
84+
console.log(
85+
`--- Retrying ${testFileName} (attempt ${attempt} of ${MAX_RETRIES}) ---`,
86+
);
10687
}
107-
});
10888

109-
child.stderr.on('data', (data) => {
89+
const nodeArgs = ['--test'];
11090
if (verbose) {
111-
process.stderr.write(data);
91+
nodeArgs.push('--test-reporter=spec');
11292
}
113-
if (outputStream) {
114-
outputStream.write(data);
93+
nodeArgs.push(testFile);
94+
95+
const child = spawn('node', nodeArgs, {
96+
stdio: 'pipe',
97+
env: {
98+
...process.env,
99+
GEMINI_CLI_INTEGRATION_TEST: 'true',
100+
INTEGRATION_TEST_FILE_DIR: testFileDir,
101+
KEEP_OUTPUT: keepOutput.toString(),
102+
VERBOSE: verbose.toString(),
103+
TEST_FILE_NAME: testFileName,
104+
},
105+
});
106+
107+
let outputStream;
108+
if (keepOutput) {
109+
const outputFile = join(testFileDir, `output-attempt-${attempt}.log`);
110+
outputStream = createWriteStream(outputFile);
111+
console.log(`Output for ${testFileName} written to: ${outputFile}`);
115112
}
116-
});
117113

118-
const exitCode = await new Promise((resolve) => {
119-
child.on('close', (code) => {
114+
const stdout = [];
115+
const stderr = [];
116+
117+
child.stdout.on('data', (data) => {
118+
if (verbose) {
119+
process.stdout.write(data);
120+
} else {
121+
stdout.push(data);
122+
}
120123
if (outputStream) {
121-
outputStream.end(() => {
122-
resolve(code);
123-
});
124+
outputStream.write(data);
125+
}
126+
});
127+
128+
child.stderr.on('data', (data) => {
129+
if (verbose) {
130+
process.stderr.write(data);
124131
} else {
125-
resolve(code);
132+
stderr.push(data);
133+
}
134+
if (outputStream) {
135+
outputStream.write(data);
126136
}
127137
});
128-
});
129138

130-
if (exitCode !== 0) {
131-
console.error(`Test file failed: ${testFileName}`);
139+
const exitCode = await new Promise((resolve) => {
140+
child.on('close', (code) => {
141+
if (outputStream) {
142+
outputStream.end(() => {
143+
resolve(code);
144+
});
145+
} else {
146+
resolve(code);
147+
}
148+
});
149+
});
150+
151+
if (exitCode === 0) {
152+
testFilePassed = true;
153+
} else {
154+
lastStdout = stdout;
155+
lastStderr = stderr;
156+
}
157+
}
158+
159+
if (!testFilePassed) {
160+
console.error(
161+
`Test file failed after ${MAX_RETRIES} attempts: ${testFileName}`,
162+
);
163+
if (!verbose) {
164+
process.stdout.write(Buffer.concat(lastStdout).toString('utf8'));
165+
process.stderr.write(Buffer.concat(lastStderr).toString('utf8'));
166+
}
132167
allTestsPassed = false;
133168
}
134169
}

0 commit comments

Comments
 (0)