Skip to content

Commit

Permalink
use rwmutex instead of mutex (GoogleCloudPlatform#5732)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuyama1 authored and lcaggio committed Mar 18, 2022
1 parent 9385fb7 commit 8bd08b9
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions mmv1/third_party/terraform/utils/provider_test.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ var masterBillingAccountEnvVars = []string{
"GOOGLE_MASTER_BILLING_ACCOUNT",
}

var mutex = &sync.Mutex{}
var configsLock = sync.RWMutex{}
var sourcesLock = sync.RWMutex{}

func init() {
configs = make(map[string]*Config)
Expand Down Expand Up @@ -234,9 +235,9 @@ func getCachedConfig(ctx context.Context, d *schema.ResourceData, configureFunc
return false
})
config.client.Transport = rec
mutex.Lock()
configsLock.Lock()
configs[testName] = config
mutex.Unlock()
configsLock.Unlock()
return config, nil
}

Expand Down Expand Up @@ -266,13 +267,13 @@ func closeRecorder(t *testing.T) {
}
}
// Clean up test config
mutex.Lock()
configsLock.Lock()
delete(configs, t.Name())
configsLock.Unlock()

sourcesLock.Lock()
delete(sources, t.Name())
mutex.Unlock()
sourcesLock.Unlock()
}
}

Expand Down Expand Up @@ -347,9 +348,9 @@ func vcrSource(t *testing.T, path, mode string) (*VcrSource, error) {
seed := rand.Int63()
s := rand.NewSource(seed)
vcrSource := VcrSource{seed: seed, source: s}
mutex.Lock()
sourcesLock.Lock()
sources[t.Name()] = vcrSource
mutex.Unlock()
sourcesLock.Unlock()
return &vcrSource, nil
case "REPLAYING":
seed, err := readSeedFromFile(vcrSeedFile(path, t.Name()))
Expand All @@ -358,9 +359,9 @@ func vcrSource(t *testing.T, path, mode string) (*VcrSource, error) {
}
s := rand.NewSource(seed)
vcrSource := VcrSource{seed: seed, source: s}
mutex.Lock()
sourcesLock.Lock()
sources[t.Name()] = vcrSource
mutex.Unlock()
sourcesLock.Unlock()
return &vcrSource, nil
default:
log.Printf("[DEBUG] No valid environment var set for VCR_MODE, expected RECORDING or REPLAYING, skipping VCR. VCR_MODE: %s", mode)
Expand Down

0 comments on commit 8bd08b9

Please sign in to comment.