Skip to content

Commit 39cbfaf

Browse files
use rwmutex instead of mutex (#5732) (#11154)
Signed-off-by: Modular Magician <[email protected]>
1 parent 75f9d94 commit 39cbfaf

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

.changelog/5732.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:none
2+
3+
```

google/provider_test.go

+32-13
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ var masterBillingAccountEnvVars = []string{
117117
"GOOGLE_MASTER_BILLING_ACCOUNT",
118118
}
119119

120-
var mutex = &sync.Mutex{}
120+
var configsLock = sync.RWMutex{}
121+
var sourcesLock = sync.RWMutex{}
121122

122123
func init() {
123124
configs = make(map[string]*Config)
@@ -135,7 +136,10 @@ func init() {
135136
// VCR requires a single HTTP client to handle all interactions so it can record and replay responses so
136137
// this caches HTTP clients per test by replacing ConfigureFunc
137138
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 {
139143
return v, nil
140144
}
141145
c, diags := configureFunc(ctx, d)
@@ -210,15 +214,18 @@ func getCachedConfig(ctx context.Context, d *schema.ResourceData, configureFunc
210214
return false
211215
})
212216
config.client.Transport = rec
213-
mutex.Lock()
217+
configsLock.Lock()
214218
configs[testName] = config
215-
mutex.Unlock()
219+
configsLock.Unlock()
216220
return config, nil
217221
}
218222

219223
// We need to explicitly close the VCR recorder to save the cassette
220224
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 {
222229
// We did not cache the config if it does not use VCR
223230
if !t.Failed() && isVcrEnabled() {
224231
// If a test succeeds, write new seed/yaml to files
@@ -227,23 +234,32 @@ func closeRecorder(t *testing.T) {
227234
t.Error(err)
228235
}
229236
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 {
231242
err = writeSeedToFile(vcrSource.seed, vcrSeedFile(envPath, t.Name()))
232243
if err != nil {
233244
t.Error(err)
234245
}
235246
}
236247
}
237248
// Clean up test config
238-
mutex.Lock()
249+
configsLock.Lock()
239250
delete(configs, t.Name())
251+
configsLock.Unlock()
252+
253+
sourcesLock.Lock()
240254
delete(sources, t.Name())
241-
mutex.Unlock()
255+
sourcesLock.Unlock()
242256
}
243257
}
244258

245259
func googleProviderConfig(t *testing.T) *Config {
260+
configsLock.RLock()
246261
config, ok := configs[t.Name()]
262+
configsLock.RUnlock()
247263
if ok {
248264
return config
249265
}
@@ -300,17 +316,20 @@ func vcrFileName(name string) string {
300316
// In RECORDING mode, generates a new seed and saves it to a file, using the seed for the source
301317
// In REPLAYING mode, reads a seed from a file and creates a source from it
302318
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 {
304323
return &s, nil
305324
}
306325
switch mode {
307326
case "RECORDING":
308327
seed := rand.Int63()
309328
s := rand.NewSource(seed)
310329
vcrSource := VcrSource{seed: seed, source: s}
311-
mutex.Lock()
330+
sourcesLock.Lock()
312331
sources[t.Name()] = vcrSource
313-
mutex.Unlock()
332+
sourcesLock.Unlock()
314333
return &vcrSource, nil
315334
case "REPLAYING":
316335
seed, err := readSeedFromFile(vcrSeedFile(path, t.Name()))
@@ -319,9 +338,9 @@ func vcrSource(t *testing.T, path, mode string) (*VcrSource, error) {
319338
}
320339
s := rand.NewSource(seed)
321340
vcrSource := VcrSource{seed: seed, source: s}
322-
mutex.Lock()
341+
sourcesLock.Lock()
323342
sources[t.Name()] = vcrSource
324-
mutex.Unlock()
343+
sourcesLock.Unlock()
325344
return &vcrSource, nil
326345
default:
327346
log.Printf("[DEBUG] No valid environment var set for VCR_MODE, expected RECORDING or REPLAYING, skipping VCR. VCR_MODE: %s", mode)

0 commit comments

Comments
 (0)