Address flaky integration tests with retries (#4604)

This commit is contained in:
Shreya Keshive 2025-07-21 18:46:00 -04:00 committed by GitHub
parent 12765eb775
commit 01ea0b8657
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 82 additions and 47 deletions

View File

@ -61,6 +61,7 @@ async function main() {
console.log(`\tFound test file: ${testFileName}`); console.log(`\tFound test file: ${testFileName}`);
} }
const MAX_RETRIES = 3;
let allTestsPassed = true; let allTestsPassed = true;
for (const testFile of testFiles) { for (const testFile of testFiles) {
@ -72,63 +73,97 @@ async function main() {
`------------- Running test file: ${testFileName} ------------------------------`, `------------- Running test file: ${testFileName} ------------------------------`,
); );
const nodeArgs = ['--test']; let attempt = 0;
if (verbose) { let testFilePassed = false;
nodeArgs.push('--test-reporter=spec'); let lastStdout = [];
} let lastStderr = [];
nodeArgs.push(testFile);
const child = spawn('node', nodeArgs, { while (attempt < MAX_RETRIES && !testFilePassed) {
stdio: 'pipe', attempt++;
env: { if (attempt > 1) {
...process.env, console.log(
GEMINI_CLI_INTEGRATION_TEST: 'true', `--- Retrying ${testFileName} (attempt ${attempt} of ${MAX_RETRIES}) ---`,
INTEGRATION_TEST_FILE_DIR: testFileDir, );
KEEP_OUTPUT: keepOutput.toString(), }
VERBOSE: verbose.toString(),
TEST_FILE_NAME: testFileName,
},
});
let outputStream; const nodeArgs = ['--test'];
if (keepOutput) {
const outputFile = join(testFileDir, 'output.log');
outputStream = createWriteStream(outputFile);
console.log(`Output for ${testFileName} written to: ${outputFile}`);
}
child.stdout.on('data', (data) => {
if (verbose) { if (verbose) {
process.stdout.write(data); nodeArgs.push('--test-reporter=spec');
} }
if (outputStream) { nodeArgs.push(testFile);
outputStream.write(data);
}
});
child.stderr.on('data', (data) => { const child = spawn('node', nodeArgs, {
if (verbose) { stdio: 'pipe',
process.stderr.write(data); env: {
} ...process.env,
if (outputStream) { GEMINI_CLI_INTEGRATION_TEST: 'true',
outputStream.write(data); INTEGRATION_TEST_FILE_DIR: testFileDir,
} KEEP_OUTPUT: keepOutput.toString(),
}); VERBOSE: verbose.toString(),
TEST_FILE_NAME: testFileName,
},
});
const exitCode = await new Promise((resolve) => { let outputStream;
child.on('close', (code) => { if (keepOutput) {
if (outputStream) { const outputFile = join(testFileDir, `output-attempt-${attempt}.log`);
outputStream.end(() => { outputStream = createWriteStream(outputFile);
resolve(code); console.log(`Output for ${testFileName} written to: ${outputFile}`);
}); }
const stdout = [];
const stderr = [];
child.stdout.on('data', (data) => {
if (verbose) {
process.stdout.write(data);
} else { } else {
resolve(code); stdout.push(data);
}
if (outputStream) {
outputStream.write(data);
} }
}); });
});
if (exitCode !== 0) { child.stderr.on('data', (data) => {
console.error(`Test file failed: ${testFileName}`); if (verbose) {
process.stderr.write(data);
} else {
stderr.push(data);
}
if (outputStream) {
outputStream.write(data);
}
});
const exitCode = await new Promise((resolve) => {
child.on('close', (code) => {
if (outputStream) {
outputStream.end(() => {
resolve(code);
});
} else {
resolve(code);
}
});
});
if (exitCode === 0) {
testFilePassed = true;
} else {
lastStdout = stdout;
lastStderr = stderr;
}
}
if (!testFilePassed) {
console.error(
`Test file failed after ${MAX_RETRIES} attempts: ${testFileName}`,
);
if (!verbose) {
process.stdout.write(Buffer.concat(lastStdout).toString('utf8'));
process.stderr.write(Buffer.concat(lastStderr).toString('utf8'));
}
allTestsPassed = false; allTestsPassed = false;
} }
} }