From 9a7b7d81b0ae520794647db748163d88836a0f29 Mon Sep 17 00:00:00 2001 From: Juana De La Cuesta Date: Mon, 3 Jul 2023 10:03:24 -0400 Subject: [PATCH] style: add mutex to avoid warning on race condition --- ha/lock_test.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ha/lock_test.go b/ha/lock_test.go index ccd6b911..6ab8306a 100644 --- a/ha/lock_test.go +++ b/ha/lock_test.go @@ -3,6 +3,7 @@ package ha import ( "context" "errors" + "sync" "testing" "time" @@ -16,11 +17,15 @@ type mockLock struct { locked bool acquiresCalls map[string]int renewsCounter int + mu sync.Mutex leaseStartTime time.Time } func (ml *mockLock) Acquire(_ context.Context, callerID string) (string, error) { + ml.mu.Lock() + defer ml.mu.Unlock() + ml.acquiresCalls[callerID] += 1 if ml.locked { return "", nil @@ -32,6 +37,9 @@ func (ml *mockLock) Acquire(_ context.Context, callerID string) (string, error) } func (ml *mockLock) Release(_ context.Context) error { + ml.mu.Lock() + defer ml.mu.Unlock() + if !ml.locked { return errors.New("error") } @@ -45,6 +53,9 @@ func (ml *mockLock) Release(_ context.Context) error { // the lock work, its intended to test the behavior of the // multiple instances running. func (ml *mockLock) Renew(_ context.Context) error { + ml.mu.Lock() + defer ml.mu.Unlock() + if !ml.locked { return errors.New("error") } @@ -104,7 +115,7 @@ func TestAcquireLock_MultipleInstances(t *testing.T) { logger: testlog.HCLogger(t), renewalPeriod: time.Duration(float64(lease) * renewalFactor), waitPeriod: time.Duration(float64(lease) * waitFactor), - randomDelay: 5 * time.Millisecond, + randomDelay: 6 * time.Millisecond, } must.False(t, l.locked)