Skip to content

Commit

Permalink
style: add mutex to avoid warning on race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanadelacuesta committed Jul 3, 2023
1 parent 22a78f8 commit 9a7b7d8
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion ha/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ha
import (
"context"
"errors"
"sync"
"testing"
"time"

Expand All @@ -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
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9a7b7d8

Please sign in to comment.