Fix data race in test

This commit is contained in:
Sargun Dhillon
2020-09-21 22:25:03 -07:00
parent cf2d5264a5
commit 1c32b2c8ee

View File

@@ -17,6 +17,7 @@ package node
import ( import (
"context" "context"
"strings" "strings"
"sync"
"testing" "testing"
"time" "time"
@@ -392,6 +393,8 @@ func TestPingAfterStatusUpdate(t *testing.T) {
break 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) assert.Assert(t, testP.maxPingInterval < maxAllowedInterval, "maximum time between node pings (%v) was greater than the maximum expected interval (%v)", testP.maxPingInterval, maxAllowedInterval)
} }
@@ -728,6 +731,7 @@ type testNodeProviderPing struct {
testNodeProvider testNodeProvider
customPingFunction func(context.Context) error customPingFunction func(context.Context) error
lastPingTime time.Time lastPingTime time.Time
maxPingIntervalLock sync.Mutex
maxPingInterval time.Duration maxPingInterval time.Duration
} }
@@ -741,6 +745,8 @@ func (tnp *testNodeProviderPing) Ping(ctx context.Context) error {
tnp.lastPingTime = now tnp.lastPingTime = now
return nil return nil
} }
tnp.maxPingIntervalLock.Lock()
defer tnp.maxPingIntervalLock.Unlock()
if now.Sub(tnp.lastPingTime) > tnp.maxPingInterval { if now.Sub(tnp.lastPingTime) > tnp.maxPingInterval {
tnp.maxPingInterval = now.Sub(tnp.lastPingTime) tnp.maxPingInterval = now.Sub(tnp.lastPingTime)
} }