Skip to content

Commit

Permalink
fix concurrent map writes error in new vcr test (GoogleCloudPlatform#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shuyama1 authored and lcaggio committed Mar 16, 2022
1 parent 3afaf9f commit 7ece88e
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions mmv1/third_party/terraform/utils/provider_test.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -121,6 +122,8 @@ var masterBillingAccountEnvVars = []string{
"GOOGLE_MASTER_BILLING_ACCOUNT",
}

var mutex = &sync.Mutex{}

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

Expand All @@ -251,8 +256,10 @@ func closeRecorder(t *testing.T) {
}
}
// Clean up test config
mutex.Lock()
delete(configs, t.Name())
delete(sources, t.Name())
mutex.Unlock()
}
}

Expand Down Expand Up @@ -322,7 +329,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()
sources[t.Name()] = vcrSource
mutex.Unlock()
return &vcrSource, nil
case "REPLAYING":
seed, err := readSeedFromFile(vcrSeedFile(path, t.Name()))
Expand All @@ -331,7 +340,9 @@ func vcrSource(t *testing.T, path, mode string) (*VcrSource, error) {
}
s := rand.NewSource(seed)
vcrSource := VcrSource{seed: seed, source: s}
mutex.Lock()
sources[t.Name()] = vcrSource
mutex.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 7ece88e

Please sign in to comment.