Skip to content

Commit

Permalink
build: add retries for flaky test (#109)
Browse files Browse the repository at this point in the history
* build: add retries for flaky test
  • Loading branch information
sofisl authored Mar 31, 2022
1 parent 8352d78 commit aefe463
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
22 changes: 21 additions & 1 deletion contact-center-insights/test/createAnalysis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,26 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const {
ContactCenterInsightsClient,
// eslint-disable-next-line node/no-missing-require
} = require('@google-cloud/contact-center-insights');
const client = new ContactCenterInsightsClient();

const delay = async (test, addMs) => {
if (!test) {
return;
}
const retries = test.currentRetry();
await new Promise(r => setTimeout(r, addMs));
// No retry on the first failure.
if (retries === 0) return;
// See: https://cloud.google.com/storage/docs/exponential-backoff
const ms = Math.pow(2, retries) + Math.random() * 1000;
return new Promise(done => {
console.info(`retrying "${test.title}" in ${ms}ms`);
setTimeout(done, ms);
});
};

describe('CreateAnalysis', () => {
let projectId;
let conversationName;
Expand All @@ -40,7 +57,10 @@ describe('CreateAnalysis', () => {
});
});

it('should create a conversation and an analysis', async () => {
// eslint-disable-next-line prefer-arrow-callback
it('should create a conversation and an analysis', async function () {
this.retries(2);
await delay(this.test, 4000);
const stdoutCreateConversation = execSync(
`node ./createConversation.js ${projectId}`
);
Expand Down
20 changes: 19 additions & 1 deletion contact-center-insights/test/exportToBigquery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ const bigquery = new BigQuery();
const bigqueryDataset = generateUuid();
const bigqueryTable = generateUuid();

const delay = async (test, addMs) => {
if (!test) {
return;
}
const retries = test.currentRetry();
await new Promise(r => setTimeout(r, addMs));
// No retry on the first failure.
if (retries === 0) return;
// See: https://cloud.google.com/storage/docs/exponential-backoff
const ms = Math.pow(2, retries) + Math.random() * 1000;
return new Promise(done => {
console.info(`retrying "${test.title}" in ${ms}ms`);
setTimeout(done, ms);
});
};

describe('ExportToBigQuery', () => {
let projectId;
let bigqueryProjectId;
Expand All @@ -62,7 +78,9 @@ describe('ExportToBigQuery', () => {
.catch(console.warn);
});

it('should export data to BigQuery', async () => {
it('should export data to BigQuery', async function () {
this.retries(2);
await delay(this.test, 4000);
const stdout = execSync(`node ./exportToBigquery.js \
${projectId} ${bigqueryProjectId} ${bigqueryDataset} ${bigqueryTable}`);
assert.match(stdout, new RegExp('Exported data to BigQuery'));
Expand Down

0 comments on commit aefe463

Please sign in to comment.