@@ -21,13 +21,25 @@ const compute = require('../');
2121
2222const DiskType = compute . protos . google . cloud . compute . v1 . AttachedDisk . Type ;
2323
24+ const delay = async test => {
25+ const retries = test . currentRetry ( ) ;
26+ if ( retries === 0 ) return ; // no retry on the first failure.
27+ // see: https://github.com/grpc/proposal/blob/master/A6-client-retries.md#exponential-backoff
28+ const ms = Math . pow ( 2 , retries ) * 500 + Math . random ( ) * 1000 ;
29+ return new Promise ( done => {
30+ console . info ( `retrying "${ test . title } " in ${ ms } ms` ) ;
31+ setTimeout ( done , ms ) ;
32+ } ) ;
33+ } ;
34+
2435describe ( 'Compute' , ( ) => {
2536 const region = 'us-central1' ;
2637 const zone = 'us-central1-a' ;
2738 let client = null ;
2839 let project = null ;
2940 let dirty = false ;
3041 let operationsClient = null ;
42+ let instancesClient = null ;
3143
3244 before ( async ( ) => {
3345 operationsClient = new compute . ZoneOperationsClient ( { fallback : 'rest' } ) ;
@@ -143,6 +155,27 @@ describe('Compute', () => {
143155
144156 before ( async ( ) => {
145157 client = new compute . FirewallsClient ( { fallback : 'rest' } ) ;
158+ instancesClient = new compute . InstancesClient ( { fallback : 'rest' } ) ;
159+
160+ const FOUR_HOURS = 1000 * 60 * 60 * 4 ;
161+ const projectId = await instancesClient . getProjectId ( ) ;
162+ for await ( const rule of client . listAsync ( {
163+ project : projectId ,
164+ } ) ) {
165+ const created = new Date ( rule . creationTimestamp ) . getTime ( ) ;
166+ // Delete firewalls that are older than 4 hours and match our
167+ // test prefix.
168+ if (
169+ created < Date . now ( ) - FOUR_HOURS &&
170+ rule . name . startsWith ( 'tsgapic-firewall' )
171+ ) {
172+ console . info ( `deleting stale firewall ${ rule . name } ` ) ;
173+ await client . delete ( {
174+ project : projectId ,
175+ firewall : rule . name ,
176+ } ) ;
177+ }
178+ }
146179 NAME = generateName ( 'firewall' ) ;
147180 } ) ;
148181
@@ -155,6 +188,9 @@ describe('Compute', () => {
155188 } ) ;
156189
157190 it ( 'create and fetch firewall, test capital letter field like "IPProtocol"' , async function ( ) {
191+ this . retries ( 3 ) ;
192+ await delay ( this . test ) ;
193+
158194 this . timeout ( 10 * 60 * 1000 ) ;
159195 const resource = {
160196 name : NAME ,
0 commit comments