@@ -18,7 +18,6 @@ const monitoring = require('@google-cloud/monitoring');
18
18
const { assert} = require ( 'chai' ) ;
19
19
const { describe, it} = require ( 'mocha' ) ;
20
20
const cp = require ( 'child_process' ) ;
21
- const retry = require ( 'p-retry' ) ;
22
21
23
22
const execSync = cmd => cp . execSync ( cmd , { encoding : 'utf-8' } ) ;
24
23
@@ -30,28 +29,32 @@ const filter = `metric.type="${computeMetricId}"`;
30
29
const projectId = process . env . GCLOUD_PROJECT ;
31
30
const resourceId = 'cloudsql_database' ;
32
31
33
- describe ( 'metrics' , ( ) => {
32
+ // A helper for delaying integration tests with an exponential backoff.
33
+ // See examples like: https://github.com/googleapis/nodejs-monitoring/issues/190,
34
+ // https://github.com/googleapis/nodejs-monitoring/issues/191.
35
+ const delay = async test => {
36
+ const retries = test . currentRetry ( ) ;
37
+ if ( retries === 0 ) return ; // no retry on the first failure.
38
+ // see: https://cloud.google.com/storage/docs/exponential-backoff:
39
+ const ms = Math . pow ( 2 , retries ) * 250 + Math . random ( ) * 1000 ;
40
+ return new Promise ( done => {
41
+ console . info ( `retrying "${ test . title } " in ${ ms } ms` ) ;
42
+ setTimeout ( done , ms ) ;
43
+ } ) ;
44
+ } ;
45
+ describe ( 'metrics' , async ( ) => {
34
46
it ( 'should create a metric descriptors' , ( ) => {
35
47
const output = execSync ( `${ cmd } create` ) ;
36
48
assert . include ( output , 'Created custom Metric' ) ;
37
49
assert . include ( output , `Type: ${ customMetricId } ` ) ;
38
50
} ) ;
39
51
40
- it ( 'should list metric descriptors, including the new custom one' , async ( ) => {
41
- // The write above appears to be eventually consistent. This retry should
42
- // not be needed. The tracking bug is here:
43
- // https://github.com/googleapis/nodejs-monitoring/issues/190
44
- await retry (
45
- async ( ) => {
46
- const output = execSync ( `${ cmd } list` ) ;
47
- assert . include ( output , customMetricId ) ;
48
- assert . include ( output , computeMetricId ) ;
49
- } ,
50
- {
51
- retries : 10 ,
52
- onFailedAttempt : ( ) => console . warn ( 'Read failed, retrying...' ) ,
53
- }
54
- ) ;
52
+ it ( 'should list metric descriptors, including the new custom one' , async function ( ) {
53
+ this . retries ( 8 ) ;
54
+ await delay ( this . test ) ; // delay the start of the test, if this is a retry.
55
+ const output = execSync ( `${ cmd } list` ) ;
56
+ assert . include ( output , customMetricId ) ;
57
+ assert . include ( output , computeMetricId ) ;
55
58
} ) ;
56
59
57
60
it ( 'should get a metric descriptor' , ( ) => {
@@ -130,7 +133,9 @@ describe('metrics', () => {
130
133
} ) ;
131
134
} ) ;
132
135
133
- it ( 'should read time series data aggregated' , async ( ) => {
136
+ it ( 'should read time series data aggregated' , async function ( ) {
137
+ this . retries ( 5 ) ;
138
+ await delay ( this . test ) ; // delay the start of the test, if this is a retry.
134
139
const [ timeSeries ] = await client . listTimeSeries ( {
135
140
name : client . projectPath ( projectId ) ,
136
141
filter : filter ,
0 commit comments