Skip to content

Commit 1976d4f

Browse files
docs(samples): add usage samples to show handling of LRO response Operation (#519)
* improvements: add samples * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * test: add tests for the samples * fix: merge conflict * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * cleanup: rearrange region tags * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: replace beforeEach with before * fix: failing test cases * cleanup: rearrange code lines * test: fix the timeout and race issues in tests * cleanup: rename fn * fix: mocha test definition * cleanup: change console log * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: failing test due package * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: linting errors * fix: add netowkr as arg * lint: fix test js * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * test: add ci network to test * cleanup: ammend as per PR comments * cleanup: remove unused dep * fix: test case * doc: add explanation to the test util file * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * doc: fix typos * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * lint: fix errors * fix: move after block Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 667b184 commit 1976d4f

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

container/snippets/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
"*.js"
1212
],
1313
"scripts": {
14-
"test": "mocha system-test --timeout 10000"
14+
"test": "mocha system-test --timeout 1000000"
1515
},
1616
"dependencies": {
17-
"@google-cloud/container": "^2.6.0"
17+
"@google-cloud/container": "^2.6.0",
18+
"uuid": "^8.3.2"
1819
},
1920
"devDependencies": {
2021
"chai": "^4.2.0",

container/snippets/quickstart.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
'use strict';
1616

17+
// [START gke_list_cluster]
1718
async function main() {
1819
// [START container_quickstart]
1920
const container = require('@google-cloud/container');
@@ -30,11 +31,11 @@ async function main() {
3031
};
3132

3233
const [response] = await client.listClusters(request);
33-
console.log('Clusters:');
34-
console.log(response);
34+
console.log('Clusters: ', response);
3535
}
3636
quickstart();
3737
// [END container_quickstart]
3838
}
3939

4040
main().catch(console.error);
41+
// [END gke_list_cluster]

container/snippets/system-test/sample.test.js renamed to container/snippets/system-test/quicktest.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const cp = require('child_process');
2020

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

23-
describe('container samples', () => {
23+
describe('container samples - quickstart', () => {
2424
it('should run the quickstart', async () => {
2525
const stdout = execSync('node quickstart');
2626
assert.match(stdout, /Clusters:/);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2022 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+
// http://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+
'use strict';
16+
17+
const container = require('@google-cloud/container');
18+
const STATUS_ENUM = container.protos.google.container.v1.Operation.Status;
19+
20+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
21+
const maxRetries = 20;
22+
let retryCount;
23+
let prevDelay;
24+
let currDelay;
25+
26+
/**
27+
* We use a custom wait and retry method for the test cases, which is different
28+
* from the approach we have for the samples itself. The samples use an async
29+
* function with delayed setIntervals. Since, the last function call of the
30+
* samples is the wait for the long running operation to complete, the program
31+
* waits until the delayed setInterval async functions resolve.
32+
*
33+
* However, when running the tests we have certain setup to be done in the
34+
* before() hook which are also long running operations. We would want to block
35+
* until these setup steps are fully complete before allowing for the tests to
36+
* start. If we use the same approach used in the samples (with an async
37+
* function scheduled on setIntervals), the before() hook will not block. Rather
38+
* it will return and the tests will start running.
39+
*
40+
* To prevent this scenario, we have employed a sleep based retry and wait
41+
* method below to be used only for the test cases.
42+
*
43+
* @param {container.v1.ClusterManagerClient} client the google cloud API client used to submit the request
44+
* @param {string} opIdentifier the unique identifier of the operation we want to check
45+
*/
46+
async function retryUntilDone(client, opIdentifier) {
47+
retryCount = 0;
48+
prevDelay = 0;
49+
currDelay = 1000;
50+
51+
while (retryCount <= maxRetries) {
52+
const [longRunningOp] = await client.getOperation({name: opIdentifier});
53+
const status = longRunningOp.status;
54+
if (status !== STATUS_ENUM[STATUS_ENUM.DONE]) {
55+
const fibonacciDelay = prevDelay + currDelay;
56+
prevDelay = currDelay;
57+
currDelay = fibonacciDelay;
58+
await sleep(fibonacciDelay);
59+
} else {
60+
break;
61+
}
62+
retryCount += 1;
63+
}
64+
}
65+
66+
module.exports = retryUntilDone;

0 commit comments

Comments
 (0)