Fix data race in client-go UpdateTransportConfig

Kubernetes-commit: 7518a44b18d8b225a9572f1e0c902776eb4a6eb0
This commit is contained in:
danielqsj
2019-07-18 12:46:03 +08:00
committed by Kubernetes Publisher
parent a56922bade
commit 0dd469e42b
2 changed files with 61 additions and 4 deletions

View File

@@ -713,6 +713,52 @@ func TestTLSCredentials(t *testing.T) {
get(t, "valid TLS cert again", false)
}
func TestConcurrentUpdateTransportConfig(t *testing.T) {
n := time.Now()
now := func() time.Time { return n }
env := []string{""}
environ := func() []string {
s := make([]string, len(env))
copy(s, env)
return s
}
c := api.ExecConfig{
Command: "./testdata/test-plugin.sh",
APIVersion: "client.authentication.k8s.io/v1alpha1",
}
a, err := newAuthenticator(newCache(), &c)
if err != nil {
t.Fatal(err)
}
a.environ = environ
a.now = now
a.stderr = ioutil.Discard
stopCh := make(chan struct{})
defer close(stopCh)
numConcurrent := 2
for i := 0; i < numConcurrent; i++ {
go func() {
for {
tc := &transport.Config{}
a.UpdateTransportConfig(tc)
select {
case <-stopCh:
return
default:
continue
}
}
}()
}
time.Sleep(2 * time.Second)
}
// genClientCert generates an x509 certificate for testing. Certificate and key
// are returned in PEM encoding.
func genClientCert(t *testing.T) ([]byte, []byte) {