Skip to content

Commit 1ded664

Browse files
bcoeAce Nassri
authored and
Ace Nassri
committed
test: address race condition with setUsageExportBucket (#622)
1 parent c66d806 commit 1ded664

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

compute/test/samples.test.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ const projectsClient = new compute.ProjectsClient({fallback: 'rest'});
2727

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

30+
// A helper for delaying integration tests with an exponential backoff.
31+
// See examples like: https://github.com/googleapis/nodejs-monitoring/issues/190,
32+
// https://github.com/googleapis/nodejs-monitoring/issues/191.
33+
const delay = async test => {
34+
const retries = test.currentRetry();
35+
if (retries === 0) return; // no retry on the first failure.
36+
// see: https://cloud.google.com/storage/docs/exponential-backoff:
37+
const ms = Math.pow(2, retries) * 500 + Math.random() * 1000;
38+
return new Promise(done => {
39+
console.info(`retrying "${test.title}" in ${ms}ms`);
40+
setTimeout(done, ms);
41+
});
42+
};
43+
3044
describe('samples', () => {
3145
const instanceName = `gcloud-test-intance-${uuid.v4().split('-')[0]}`;
3246
const zone = 'europe-central2-b';
@@ -85,7 +99,11 @@ describe('samples', () => {
8599

86100
describe('usage export', () => {
87101
before(async () => {
88-
await storage.createBucket(bucketName);
102+
try {
103+
await storage.createBucket(bucketName);
104+
} catch (err) {
105+
// Resource likely already existed due to retry.
106+
}
89107
});
90108

91109
after(async () => {
@@ -99,7 +117,9 @@ describe('samples', () => {
99117
await storage.bucket(bucketName).delete();
100118
});
101119

102-
it('should set empty default value in reportNamePrefix', async () => {
120+
it('should set empty default value in reportNamePrefix', async function () {
121+
this.retries(3);
122+
await delay(this.test);
103123
const projectId = await instancesClient.getProjectId();
104124

105125
const output = execSync(
@@ -118,11 +138,13 @@ describe('samples', () => {
118138

119139
const usageExportLocation = project.usageExportLocation;
120140

121-
assert.equal(usageExportLocation.bucketName, bucketName);
141+
assert.match(usageExportLocation.bucketName, /test-bucket-name/);
122142
assert.equal(usageExportLocation.reportNamePrefix, '');
123143
});
124144

125-
it('should get current default value in reportNamePrefix', async () => {
145+
it('should get current default value in reportNamePrefix', async function () {
146+
this.retries(3);
147+
await delay(this.test);
126148
const projectId = await instancesClient.getProjectId();
127149

128150
execSync(`node setUsageExportBucket ${projectId} ${bucketName}`);
@@ -137,7 +159,9 @@ describe('samples', () => {
137159
assert.match(output, /Returned reportNamePrefix: usage_gce/);
138160
});
139161

140-
it('should disable usage export', async () => {
162+
it('should disable usage export', async function () {
163+
this.retries(3);
164+
await delay(this.test);
141165
const projectId = await instancesClient.getProjectId();
142166

143167
execSync(`node setUsageExportBucket ${projectId} ${bucketName}`);

0 commit comments

Comments
 (0)