|
| 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