@@ -27,6 +27,20 @@ const projectsClient = new compute.ProjectsClient({fallback: 'rest'});
27
27
28
28
const execSync = cmd => cp . execSync ( cmd , { encoding : 'utf-8' } ) ;
29
29
30
+ // A helper for delaying integration tests with an exponential backoff.
31
+ // See examples like: https://github.com/googleapis/nodejs-monitoring/issues/190,
32
+ // https://github.com/googleapis/nodejs-monitoring/issues/191.
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 ) * 500 + Math . random ( ) * 1000 ;
38
+ return new Promise ( done => {
39
+ console . info ( `retrying "${ test . title } " in ${ ms } ms` ) ;
40
+ setTimeout ( done , ms ) ;
41
+ } ) ;
42
+ } ;
43
+
30
44
describe ( 'samples' , ( ) => {
31
45
const instanceName = `gcloud-test-intance-${ uuid . v4 ( ) . split ( '-' ) [ 0 ] } ` ;
32
46
const zone = 'europe-central2-b' ;
@@ -85,7 +99,11 @@ describe('samples', () => {
85
99
86
100
describe ( 'usage export' , ( ) => {
87
101
before ( async ( ) => {
88
- await storage . createBucket ( bucketName ) ;
102
+ try {
103
+ await storage . createBucket ( bucketName ) ;
104
+ } catch ( err ) {
105
+ // Resource likely already existed due to retry.
106
+ }
89
107
} ) ;
90
108
91
109
after ( async ( ) => {
@@ -99,7 +117,9 @@ describe('samples', () => {
99
117
await storage . bucket ( bucketName ) . delete ( ) ;
100
118
} ) ;
101
119
102
- it ( 'should set empty default value in reportNamePrefix' , async ( ) => {
120
+ it ( 'should set empty default value in reportNamePrefix' , async function ( ) {
121
+ this . retries ( 3 ) ;
122
+ await delay ( this . test ) ;
103
123
const projectId = await instancesClient . getProjectId ( ) ;
104
124
105
125
const output = execSync (
@@ -118,11 +138,13 @@ describe('samples', () => {
118
138
119
139
const usageExportLocation = project . usageExportLocation ;
120
140
121
- assert . equal ( usageExportLocation . bucketName , bucketName ) ;
141
+ assert . match ( usageExportLocation . bucketName , / t e s t - b u c k e t - n a m e / ) ;
122
142
assert . equal ( usageExportLocation . reportNamePrefix , '' ) ;
123
143
} ) ;
124
144
125
- it ( 'should get current default value in reportNamePrefix' , async ( ) => {
145
+ it ( 'should get current default value in reportNamePrefix' , async function ( ) {
146
+ this . retries ( 3 ) ;
147
+ await delay ( this . test ) ;
126
148
const projectId = await instancesClient . getProjectId ( ) ;
127
149
128
150
execSync ( `node setUsageExportBucket ${ projectId } ${ bucketName } ` ) ;
@@ -137,7 +159,9 @@ describe('samples', () => {
137
159
assert . match ( output , / R e t u r n e d r e p o r t N a m e P r e f i x : u s a g e _ g c e / ) ;
138
160
} ) ;
139
161
140
- it ( 'should disable usage export' , async ( ) => {
162
+ it ( 'should disable usage export' , async function ( ) {
163
+ this . retries ( 3 ) ;
164
+ await delay ( this . test ) ;
141
165
const projectId = await instancesClient . getProjectId ( ) ;
142
166
143
167
execSync ( `node setUsageExportBucket ${ projectId } ${ bucketName } ` ) ;
0 commit comments