From 1c32b2c8eeed9ec2ddfe124b8dfbfd0a6f53b91f Mon Sep 17 00:00:00 2001 From: Sargun Dhillon Date: Mon, 21 Sep 2020 22:25:03 -0700 Subject: [PATCH] Fix data race in test --- node/node_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/node/node_test.go b/node/node_test.go index a2a2267b5..02df6577a 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -17,6 +17,7 @@ package node import ( "context" "strings" + "sync" "testing" "time" @@ -392,6 +393,8 @@ func TestPingAfterStatusUpdate(t *testing.T) { break } + testP.maxPingIntervalLock.Lock() + defer testP.maxPingIntervalLock.Unlock() assert.Assert(t, testP.maxPingInterval < maxAllowedInterval, "maximum time between node pings (%v) was greater than the maximum expected interval (%v)", testP.maxPingInterval, maxAllowedInterval) } @@ -726,9 +729,10 @@ func (p *testNodeProvider) triggerStatusUpdate(n *corev1.Node) { // testNodeProviderPing tracks the maximum time interval between calls to Ping type testNodeProviderPing struct { testNodeProvider - customPingFunction func(context.Context) error - lastPingTime time.Time - maxPingInterval time.Duration + customPingFunction func(context.Context) error + lastPingTime time.Time + maxPingIntervalLock sync.Mutex + maxPingInterval time.Duration } func (tnp *testNodeProviderPing) Ping(ctx context.Context) error { @@ -741,6 +745,8 @@ func (tnp *testNodeProviderPing) Ping(ctx context.Context) error { tnp.lastPingTime = now return nil } + tnp.maxPingIntervalLock.Lock() + defer tnp.maxPingIntervalLock.Unlock() if now.Sub(tnp.lastPingTime) > tnp.maxPingInterval { tnp.maxPingInterval = now.Sub(tnp.lastPingTime) }