From 629a7f112583c2667f81f5c9aba5afc2a31a8be2 Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Fri, 3 Apr 2020 08:53:06 -0700 Subject: [PATCH] refactor(test): use exponential backoff and retry some integration tests (#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 --- .../samples/package.json | 1 - .../samples/test/quickstart.test.js | 34 ++++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/packages/google-cloud-monitoring/samples/package.json b/packages/google-cloud-monitoring/samples/package.json index 147004e47b7..4698ed8fc0d 100644 --- a/packages/google-cloud-monitoring/samples/package.json +++ b/packages/google-cloud-monitoring/samples/package.json @@ -20,7 +20,6 @@ "devDependencies": { "chai": "^4.2.0", "mocha": "^7.0.0", - "p-retry": "^4.0.0", "uuid": "^7.0.0" } } diff --git a/packages/google-cloud-monitoring/samples/test/quickstart.test.js b/packages/google-cloud-monitoring/samples/test/quickstart.test.js index 50bbdde1616..82bee1637c7 100644 --- a/packages/google-cloud-monitoring/samples/test/quickstart.test.js +++ b/packages/google-cloud-monitoring/samples/test/quickstart.test.js @@ -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/); }); });