@@ -117,7 +117,8 @@ var masterBillingAccountEnvVars = []string{
117
117
"GOOGLE_MASTER_BILLING_ACCOUNT" ,
118
118
}
119
119
120
- var mutex = & sync.Mutex {}
120
+ var configsLock = sync.RWMutex {}
121
+ var sourcesLock = sync.RWMutex {}
121
122
122
123
func init () {
123
124
configs = make (map [string ]* Config )
@@ -135,7 +136,10 @@ func init() {
135
136
// VCR requires a single HTTP client to handle all interactions so it can record and replay responses so
136
137
// this caches HTTP clients per test by replacing ConfigureFunc
137
138
func getCachedConfig (ctx context.Context , d * schema.ResourceData , configureFunc schema.ConfigureContextFunc , testName string ) (* Config , diag.Diagnostics ) {
138
- if v , ok := configs [testName ]; ok {
139
+ configsLock .RLock ()
140
+ v , ok := configs [testName ]
141
+ configsLock .RUnlock ()
142
+ if ok {
139
143
return v , nil
140
144
}
141
145
c , diags := configureFunc (ctx , d )
@@ -210,15 +214,18 @@ func getCachedConfig(ctx context.Context, d *schema.ResourceData, configureFunc
210
214
return false
211
215
})
212
216
config .client .Transport = rec
213
- mutex .Lock ()
217
+ configsLock .Lock ()
214
218
configs [testName ] = config
215
- mutex .Unlock ()
219
+ configsLock .Unlock ()
216
220
return config , nil
217
221
}
218
222
219
223
// We need to explicitly close the VCR recorder to save the cassette
220
224
func closeRecorder (t * testing.T ) {
221
- if config , ok := configs [t .Name ()]; ok {
225
+ configsLock .RLock ()
226
+ config , ok := configs [t .Name ()]
227
+ configsLock .RUnlock ()
228
+ if ok {
222
229
// We did not cache the config if it does not use VCR
223
230
if ! t .Failed () && isVcrEnabled () {
224
231
// If a test succeeds, write new seed/yaml to files
@@ -227,23 +234,32 @@ func closeRecorder(t *testing.T) {
227
234
t .Error (err )
228
235
}
229
236
envPath := os .Getenv ("VCR_PATH" )
230
- if vcrSource , ok := sources [t .Name ()]; ok {
237
+
238
+ sourcesLock .RLock ()
239
+ vcrSource , ok := sources [t .Name ()]
240
+ sourcesLock .RUnlock ()
241
+ if ok {
231
242
err = writeSeedToFile (vcrSource .seed , vcrSeedFile (envPath , t .Name ()))
232
243
if err != nil {
233
244
t .Error (err )
234
245
}
235
246
}
236
247
}
237
248
// Clean up test config
238
- mutex .Lock ()
249
+ configsLock .Lock ()
239
250
delete (configs , t .Name ())
251
+ configsLock .Unlock ()
252
+
253
+ sourcesLock .Lock ()
240
254
delete (sources , t .Name ())
241
- mutex .Unlock ()
255
+ sourcesLock .Unlock ()
242
256
}
243
257
}
244
258
245
259
func googleProviderConfig (t * testing.T ) * Config {
260
+ configsLock .RLock ()
246
261
config , ok := configs [t .Name ()]
262
+ configsLock .RUnlock ()
247
263
if ok {
248
264
return config
249
265
}
@@ -300,17 +316,20 @@ func vcrFileName(name string) string {
300
316
// In RECORDING mode, generates a new seed and saves it to a file, using the seed for the source
301
317
// In REPLAYING mode, reads a seed from a file and creates a source from it
302
318
func vcrSource (t * testing.T , path , mode string ) (* VcrSource , error ) {
303
- if s , ok := sources [t .Name ()]; ok {
319
+ sourcesLock .RLock ()
320
+ s , ok := sources [t .Name ()]
321
+ sourcesLock .RUnlock ()
322
+ if ok {
304
323
return & s , nil
305
324
}
306
325
switch mode {
307
326
case "RECORDING" :
308
327
seed := rand .Int63 ()
309
328
s := rand .NewSource (seed )
310
329
vcrSource := VcrSource {seed : seed , source : s }
311
- mutex .Lock ()
330
+ sourcesLock .Lock ()
312
331
sources [t .Name ()] = vcrSource
313
- mutex .Unlock ()
332
+ sourcesLock .Unlock ()
314
333
return & vcrSource , nil
315
334
case "REPLAYING" :
316
335
seed , err := readSeedFromFile (vcrSeedFile (path , t .Name ()))
@@ -319,9 +338,9 @@ func vcrSource(t *testing.T, path, mode string) (*VcrSource, error) {
319
338
}
320
339
s := rand .NewSource (seed )
321
340
vcrSource := VcrSource {seed : seed , source : s }
322
- mutex .Lock ()
341
+ sourcesLock .Lock ()
323
342
sources [t .Name ()] = vcrSource
324
- mutex .Unlock ()
343
+ sourcesLock .Unlock ()
325
344
return & vcrSource , nil
326
345
default :
327
346
log .Printf ("[DEBUG] No valid environment var set for VCR_MODE, expected RECORDING or REPLAYING, skipping VCR. VCR_MODE: %s" , mode )
0 commit comments