Skip to content

Commit ac16520

Browse files
authored
test: handle a couple more flaky tests (#405)
1 parent 740e69b commit ac16520

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

monitoring/snippets/test/alerts.test.js

+28-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ const cmd = 'node alerts';
3232
let policyOneName, policyTwoName, channelName;
3333
const testPrefix = `gcloud-test-${uuid.v4().split('-')[0]}`;
3434

35+
// Tests in this suite can trigger the error,
36+
// "Too many concurrent edits to the project configuration. Please try again".
37+
const delay = async test => {
38+
const retries = test.currentRetry();
39+
if (retries === 0) return; // no retry on the first failure.
40+
// see: https://cloud.google.com/storage/docs/exponential-backoff:
41+
const ms = Math.pow(2, retries) * 500 + Math.random() * 2000;
42+
return new Promise(done => {
43+
console.info(`retrying "${test.title}" in ${ms}ms`);
44+
setTimeout(done, ms);
45+
});
46+
};
47+
3548
describe('alerts', () => {
3649
before(async () => {
3750
await reapPolicies();
@@ -162,13 +175,17 @@ describe('alerts', () => {
162175
await deleteChannels();
163176
});
164177

165-
it('should replace notification channels', () => {
178+
it('should replace notification channels', async function() {
179+
this.retries(8);
180+
await delay(this.test);
166181
const stdout = execSync(`${cmd} replace ${policyOneName} ${channelName}`);
167182
assert.include(stdout, 'Updated projects');
168183
assert.include(stdout, policyOneName);
169184
});
170185

171-
it('should disable policies', () => {
186+
it('should disable policies', async function() {
187+
this.retries(8);
188+
await delay(this.test);
172189
const stdout = execSync(
173190
`${cmd} disable ${projectId} 'display_name.size < 28'`
174191
);
@@ -177,7 +194,9 @@ describe('alerts', () => {
177194
assert.include(stdout, policyTwoName);
178195
});
179196

180-
it('should enable policies', () => {
197+
it('should enable policies', async function() {
198+
this.retries(8);
199+
await delay(this.test);
181200
const stdout = execSync(
182201
`${cmd} enable ${projectId} 'display_name.size < 28'`
183202
);
@@ -194,14 +213,18 @@ describe('alerts', () => {
194213
assert.include(stdout, 'second');
195214
});
196215

197-
it('should backup all policies', async () => {
216+
it('should backup all policies', async function() {
217+
this.retries(8);
218+
await delay(this.test);
198219
const output = execSync(`${cmd} backup ${projectId}`);
199220
assert.include(output, 'Saved policies to ./policies_backup.json');
200221
assert.ok(fs.existsSync(path.join(__dirname, '../policies_backup.json')));
201222
await client.deleteAlertPolicy({name: policyOneName});
202223
});
203224

204-
it('should restore policies', () => {
225+
it('should restore policies', async function() {
226+
this.retries(8);
227+
await delay(this.test);
205228
const output = execSync(`${cmd} restore ${projectId}`);
206229
assert.include(output, 'Loading policies from ./policies_backup.json');
207230
const matches = output.match(

monitoring/snippets/test/quickstart.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ const {describe, it} = require('mocha');
1919
const cp = require('child_process');
2020

2121
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
22-
// A helper for delaying integration tests with an exponential backoff.
23-
// See examples like: https://github.com/googleapis/nodejs-monitoring/issues/190,
24-
// https://github.com/googleapis/nodejs-monitoring/issues/191.
22+
// Tests in this suite can trigger the upstream error,
23+
// "points were written more frequently than the maximum
24+
// sampling period configured for the metric":
2525
const delay = async test => {
2626
const retries = test.currentRetry();
2727
if (retries === 0) return; // no retry on the first failure.

0 commit comments

Comments
 (0)