Skip to content

Commit d6c2a3a

Browse files
bcoeAce Nassri
authored and
Ace Nassri
committed
test: standardize retry logic (#371)
1 parent 2081197 commit d6c2a3a

5 files changed

+43
-31
lines changed

automl/test/export_dataset.test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ const DATASET_ID = 'TRL8522556519449886720';
2727
const EXPORT_DATASET_REGION_TAG = 'export_dataset';
2828
const LOCATION = 'us-central1';
2929

30+
const {delay} = require('./util');
31+
3032
describe('Automl Translate Dataset Tests', () => {
3133
const client = new AutoMlClient();
3234
const prefix = 'TEST_EXPORT_OUTPUT';
3335

34-
it('should export a datset', async () => {
36+
it('should export a datset', async function() {
37+
this.retries(4);
38+
await delay(this.test);
3539
const projectId = await client.getProjectId();
3640
const bucketName = `${projectId}-automl-translate`;
3741
const export_output = execSync(

automl/test/import_dataset.test.js

+1-14
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,7 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2626
const IMPORT_DATASET_REGION_TAG = 'import_dataset';
2727
const LOCATION = 'us-central1';
2828
const TWENTY_MINUTES_IN_SECONDS = 60 * 20;
29-
30-
// If two suites of tests are running parallel, importing and creating
31-
// datasets can fail, with:
32-
// No other operations should be working on projects/1046198160504/*.
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) * 1000 + Math.random() * 2000;
38-
return new Promise(done => {
39-
console.info(`retrying "${test.title}" in ${ms}ms`);
40-
setTimeout(done, ms);
41-
});
42-
};
29+
const {delay} = require('./util');
4330

4431
describe('Automl Import Dataset Test', () => {
4532
const client = new AutoMlClient();

automl/test/translate_create_model.test.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
'use strict';
1616

1717
const {assert} = require('chai');
18-
const {describe, it, after} = require('mocha');
18+
const {describe, it, afterEach} = require('mocha');
1919
const {AutoMlClient} = require('@google-cloud/automl').v1;
2020

2121
const cp = require('child_process');
@@ -26,11 +26,16 @@ const CREATE_MODEL_REGION_TAG = 'translate_create_model';
2626
const LOCATION = 'us-central1';
2727
const DATASET_ID = 'TRL8522556519449886720';
2828

29+
const {delay} = require('./util');
30+
2931
describe('Automl Translate Create Model Tests', () => {
3032
const client = new AutoMlClient();
3133
let operationId;
3234

33-
it('should create a model', async () => {
35+
it('should create a model', async function() {
36+
this.retries(5);
37+
await delay(this.test);
38+
3439
const projectId = await client.getProjectId();
3540
const create_output = execSync(
3641
`node ${CREATE_MODEL_REGION_TAG}.js ${projectId} ${LOCATION} ${DATASET_ID} translation_test_create_model`
@@ -43,7 +48,7 @@ describe('Automl Translate Create Model Tests', () => {
4348
.split('\n')[0];
4449
});
4550

46-
after('cancel model training', async () => {
51+
afterEach('cancel model training', async () => {
4752
await client.operationsClient.cancelOperation({name: operationId});
4853
});
4954
});

automl/test/util.js

+28
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+
};

automl/test/vision_object_detection_predict.test.js

+1-13
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,7 @@ const MODEL_ID = 'IOD1656537412546854912';
2626
const PREDICT_REGION_TAG = 'vision_object_detection_predict';
2727
const LOCATION = 'us-central1';
2828

29-
// If two suites of tests are running parallel, importing and creating
30-
// datasets can fail, with:
31-
// "Another DEPLOY model operation is running on the model".
32-
const delay = async test => {
33-
const retries = test.currentRetry();
34-
if (retries === 0) return; // no retry on the first failure.
35-
// see: https://cloud.google.com/storage/docs/exponential-backoff:
36-
const ms = Math.pow(2, retries) * 1000 + Math.random() * 2000;
37-
return new Promise(done => {
38-
console.info(`retrying "${test.title}" in ${ms}ms`);
39-
setTimeout(done, ms);
40-
});
41-
};
29+
const {delay} = require('./util');
4230

4331
describe('Automl Vision Object Detection Predict Test', () => {
4432
const client = new AutoMlClient();

0 commit comments

Comments
 (0)