Skip to content

Commit fc5addd

Browse files
author
Benjamin E. Coe
authored
test: retry pubsub test, as it relies on many moving parts (#186)
adds retry with exponential backoff to the flaky pubsub test. fixes: #184, #185
1 parent c155555 commit fc5addd

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

container-analysis/snippets/test/containerAnalysis.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
const {assert} = require('chai');
1818
const {describe, it, before, after, afterEach, beforeEach} = require('mocha');
1919
const cp = require('child_process');
20+
const {delay} = require('./util');
2021
const uuid = require('uuid');
2122

2223
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
@@ -343,7 +344,10 @@ describe('pubsub', () => {
343344
await pubsub.subscription(subscriptionId).delete();
344345
});
345346

346-
it('should get count of occurrences from pubsub topic', async () => {
347+
it('should get count of occurrences from pubsub topic', async function() {
348+
this.retries(3);
349+
await delay(this.test);
350+
347351
const occurrenceCount = 3;
348352
const pubSubOccurrenceReq = {
349353
parent: formattedParent,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// ML tests frequently run into concurrency and quota issues, for which
16+
// retrying with a backoff is a good strategy:
17+
module.exports = {
18+
async delay(test) {
19+
const retries = test.currentRetry();
20+
if (retries === 0) return; // no retry on the first failure.
21+
// see: https://cloud.google.com/storage/docs/exponential-backoff:
22+
const ms = Math.pow(2, retries) * 1000 + Math.random() * 2000;
23+
return new Promise(done => {
24+
console.info(`retrying "${test.title}" in ${ms}ms`);
25+
setTimeout(done, ms);
26+
});
27+
},
28+
};

0 commit comments

Comments
 (0)