Skip to content

Commit 2f40b3d

Browse files
samples: reduce calls to API (#515)
1 parent e257e92 commit 2f40b3d

File tree

1 file changed

+44
-31
lines changed

1 file changed

+44
-31
lines changed

asset/snippets/test/sample.test.js

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,9 @@ let vm;
4242
// Some of these tests can take an extremely long time, and occasionally
4343
// timeout, see:
4444
// "Timeout of 180000ms exceeded. For async tests and hooks".
45-
const delay = async test => {
46-
const retries = test.currentRetry();
47-
if (retries === 0) return; // no retry on the first failure.
48-
// see: https://cloud.google.com/storage/docs/exponential-backoff:
49-
const ms = Math.pow(2, retries) * 1000 + Math.random() * 2000;
50-
return new Promise(done => {
51-
console.info(`retrying "${test.title}" in ${ms}ms`);
52-
setTimeout(done, ms);
53-
});
54-
};
45+
function sleep(ms) {
46+
return new Promise(resolve => setTimeout(resolve, ms));
47+
}
5548

5649
describe('quickstart sample tests', () => {
5750
before(async () => {
@@ -67,13 +60,17 @@ describe('quickstart sample tests', () => {
6760
await vm.delete();
6861
});
6962

70-
it('should export assets to specified path', async function () {
71-
this.retries(2);
72-
await delay(this.test);
63+
it('should export assets to specified path', async () => {
7364
const dumpFilePath = `gs://${bucketName}/my-assets.txt`;
7465
execSync(`node exportAssets ${dumpFilePath}`);
75-
const file = await bucket.file('my-assets.txt');
76-
const exists = await file.exists();
66+
let waitMs = 1000;
67+
let exists = false;
68+
let file;
69+
for (let retry = 0; retry < 3 && !exists; ++retry) {
70+
await sleep((waitMs *= 2));
71+
file = await bucket.file('my-assets.txt');
72+
exists = await file.exists();
73+
}
7774
assert.ok(exists);
7875
await file.delete();
7976
});
@@ -115,33 +112,49 @@ describe('quickstart sample tests', () => {
115112
assert.include(stdout, '//cloudresourcemanager.googleapis.com/projects');
116113
});
117114

118-
it('should analyze iam policy and write analysis results to gcs successfully', async function () {
119-
this.retries(2);
120-
await delay(this.test);
115+
it('should analyze iam policy and write analysis results to gcs successfully', async () => {
121116
const uri = `gs://${bucketName}/my-analysis.json`;
122117
execSync(`node analyzeIamPolicyLongrunningGcs ${uri}`);
123-
const file = await bucket.file('my-analysis.json');
124-
const exists = await file.exists();
118+
let waitMs = 1000;
119+
let exists = false;
120+
let file;
121+
for (let retry = 0; retry < 3 && !exists; ++retry) {
122+
await sleep((waitMs *= 2));
123+
file = await bucket.file('my-analysis.json');
124+
exists = await file.exists();
125+
}
125126
assert.ok(exists);
126127
await file.delete();
127128
});
128129

129-
it('should analyze iam policy and write analysis results to bigquery successfully', async function () {
130-
this.retries(2);
131-
await delay(this.test);
130+
it('should analyze iam policy and write analysis results to bigquery successfully', async () => {
132131
const tablePrefix = 'analysis_nodejs';
133132
execSync(
134133
`node analyzeIamPolicyLongrunningBigquery ${datasetId} ${tablePrefix}`
135134
);
136-
const metadataTable = await bigquery
137-
.dataset(datasetId)
138-
.table('analysis_nodejs_analysis');
139-
const metadataTable_exists = await metadataTable.exists();
135+
let waitMs = 1000;
136+
let metadataTable;
137+
let metadataTable_exists = false;
138+
let resultsTable;
139+
let resultsTable_exists = false;
140+
141+
for (
142+
let retry = 0;
143+
retry < 3 && !(metadataTable_exists || resultsTable_exists);
144+
++retry
145+
) {
146+
await sleep((waitMs *= 2));
147+
metadataTable = await bigquery
148+
.dataset(datasetId)
149+
.table('analysis_nodejs_analysis');
150+
metadataTable_exists = await metadataTable.exists();
151+
resultsTable = await bigquery
152+
.dataset(datasetId)
153+
.table('analysis_nodejs_analysis_result');
154+
resultsTable_exists = await resultsTable.exists();
155+
}
156+
140157
assert.ok(metadataTable_exists);
141-
const resultsTable = await bigquery
142-
.dataset(datasetId)
143-
.table('analysis_nodejs_analysis_result');
144-
const resultsTable_exists = await resultsTable.exists();
145158
assert.ok(resultsTable_exists);
146159
await metadataTable.delete();
147160
await resultsTable.delete();

0 commit comments

Comments
 (0)