|
| 1 | +// Copyright 2017 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 {assert} = require('chai'); |
| 18 | +const {describe, it, before} = require('mocha'); |
| 19 | +const cp = require('child_process'); |
| 20 | +const monitoring = require('@google-cloud/monitoring'); |
| 21 | + |
| 22 | +const client = new monitoring.AlertPolicyServiceClient(); |
| 23 | + |
| 24 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 25 | +// Tests in this suite can trigger the upstream error, |
| 26 | +// "points were written more frequently than the maximum |
| 27 | +// sampling period configured for the metric": |
| 28 | +const delay = async test => { |
| 29 | + const retries = test.currentRetry(); |
| 30 | + if (retries === 0) return; // no retry on the first failure. |
| 31 | + // see: https://cloud.google.com/storage/docs/exponential-backoff: |
| 32 | + const ms = Math.pow(2, retries) * 250 + Math.random() * 1000; |
| 33 | + return new Promise(done => { |
| 34 | + console.info(`retrying "${test.title}" in ${ms}ms`); |
| 35 | + setTimeout(done, ms); |
| 36 | + }); |
| 37 | +}; |
| 38 | +describe('quickstart', () => { |
| 39 | + let projectId; |
| 40 | + before(async () => { |
| 41 | + projectId = await client.getProjectId(); |
| 42 | + }); |
| 43 | + it('should run the quickstart', async function () { |
| 44 | + this.retries(8); |
| 45 | + await delay(this.test); // delay the start of the test, if this is a retry. |
| 46 | + const result = execSync(`node quickstart.js ${projectId}`); |
| 47 | + assert.match(result, /Done writing time series data/); |
| 48 | + }); |
| 49 | +}); |
0 commit comments