Skip to content

Commit 92c2626

Browse files
bcoeAce Nassri
authored and
Ace Nassri
committed
test: deal with concurrent import (#368)
1 parent a87c289 commit 92c2626

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

automl/test/import_dataset.test.js

+62-4
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, before, after} = require('mocha');
18+
const {describe, it, before} = require('mocha');
1919
const {AutoMlClient} = require('@google-cloud/automl').v1;
2020

2121
const cp = require('child_process');
@@ -25,12 +25,66 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2525

2626
const IMPORT_DATASET_REGION_TAG = 'import_dataset';
2727
const LOCATION = 'us-central1';
28+
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+
};
2843

2944
describe('Automl Import Dataset Test', () => {
3045
const client = new AutoMlClient();
3146
let datasetId;
3247

33-
before('should create a dataset', async () => {
48+
before(async () => {
49+
await cleanupOldDatasets();
50+
});
51+
52+
async function cleanupOldDatasets() {
53+
const projectId = await client.getProjectId();
54+
let request = {
55+
parent: client.locationPath(projectId, LOCATION),
56+
filter: 'translation_dataset_metadata:*',
57+
};
58+
const [response] = await client.listDatasets(request);
59+
for (const dataset of response) {
60+
try {
61+
const id = dataset.name
62+
.split('/')
63+
[dataset.name.split('/').length - 1].split('\n')[0];
64+
console.info(`checking dataset ${id}`);
65+
if (id.match(/test_[0-9a-f]{8}/)) {
66+
console.info(`deleting dataset ${id}`);
67+
if (
68+
dataset.createTime.seconds - Date.now() / 1000 >
69+
TWENTY_MINUTES_IN_SECONDS
70+
) {
71+
console.info(`dataset ${id} is greater than 20 minutes old`);
72+
request = {
73+
name: client.datasetPath(projectId, LOCATION, id),
74+
};
75+
const [operation] = await client.deleteDataset(request);
76+
await operation.promise();
77+
}
78+
}
79+
} catch (err) {
80+
console.warn(err);
81+
}
82+
}
83+
}
84+
85+
it('should create a dataset', async function() {
86+
this.retries(5);
87+
await delay(this.test);
3488
const projectId = await client.getProjectId();
3589
const displayName = `test_${uuid
3690
.v4()
@@ -53,7 +107,9 @@ describe('Automl Import Dataset Test', () => {
53107
[response.name.split('/').length - 1].split('\n')[0];
54108
});
55109

56-
it('should create, import, and delete a dataset', async () => {
110+
it('should import dataset', async function() {
111+
this.retries(5);
112+
await delay(this.test);
57113
const projectId = await client.getProjectId();
58114
const data = `gs://${projectId}-automl-translate/en-ja-short.csv`;
59115
const import_output = execSync(
@@ -62,7 +118,9 @@ describe('Automl Import Dataset Test', () => {
62118
assert.match(import_output, /Dataset imported/);
63119
});
64120

65-
after('delete created dataset', async () => {
121+
it('should delete created dataset', async function() {
122+
this.retries(5);
123+
await delay(this.test);
66124
const projectId = await client.getProjectId();
67125
const request = {
68126
name: client.datasetPath(projectId, LOCATION, datasetId),

0 commit comments

Comments
 (0)