Skip to content

Commit

Permalink
refactor(test): use exponential backoff and retry some integration te…
Browse files Browse the repository at this point in the history
…sts (#402)

* refactor(test): use exponential backoff and retry for integration tests

* chore: remove p-retry dependency, in favor of this.retries

* chore: slight tweak to retry logic

* chore: add stagger as suggested
  • Loading branch information
bcoe authored Apr 3, 2020
1 parent 112243b commit 629a7f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
1 change: 0 additions & 1 deletion packages/google-cloud-monitoring/samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^7.0.0",
"p-retry": "^4.0.0",
"uuid": "^7.0.0"
}
}
34 changes: 18 additions & 16 deletions packages/google-cloud-monitoring/samples/test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,26 @@
const {assert} = require('chai');
const {describe, it} = require('mocha');
const cp = require('child_process');
const retry = require('p-retry');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

// A helper for delaying integration tests with an exponential backoff.
// See examples like: https://github.com/googleapis/nodejs-monitoring/issues/190,
// https://github.com/googleapis/nodejs-monitoring/issues/191.
const delay = async test => {
const retries = test.currentRetry();
if (retries === 0) return; // no retry on the first failure.
// see: https://cloud.google.com/storage/docs/exponential-backoff:
const ms = Math.pow(2, retries) * 250 + Math.random() * 1000;
return new Promise(done => {
console.info(`retrying "${test.title}" in ${ms}ms`);
setTimeout(done, ms);
});
};
describe('quickstart', () => {
it('should run the quickstart', async () => {
// The write in the quickstart appears to be very, very flaky.
// This should not be needed. The tracking bug is here:
// https://github.com/googleapis/nodejs-monitoring/issues/191
await retry(
async () => {
const result = execSync('node quickstart');
assert.match(result, /Done writing time series data/);
},
{
retries: 10,
onFailedAttempt: () => console.warn('Write failed, retrying...'),
}
);
it('should run the quickstart', async function() {
this.retries(8);
await delay(this.test); // delay the start of the test, if this is a retry.
const result = execSync('node quickstart');
assert.match(result, /Done writing time series data/);
});
});

0 comments on commit 629a7f1

Please sign in to comment.