Skip to content

Commit

Permalink
Rework soak test to error on unlock failure
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhouston committed Jun 5, 2020
1 parent b0f7e27 commit 81ecc55
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions backend/remote-state/kubernetes/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package kubernetes

import (
"fmt"
"math/rand"
"os"
"sync"
"testing"
"time"

"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/state"
Expand Down Expand Up @@ -77,8 +75,8 @@ func TestBackendLocksSoak(t *testing.T) {
testACC(t)
defer cleanupK8sResources(t)

clientCount := 1000
lockCount := 0
clientCount := 100
lockAttempts := 100

lockers := []statemgr.Locker{}
for i := 0; i < clientCount; i++ {
Expand All @@ -97,30 +95,28 @@ func TestBackendLocksSoak(t *testing.T) {
wg := sync.WaitGroup{}
for i, l := range lockers {
wg.Add(1)
go func(locker statemgr.Locker, i int) {
r := rand.Intn(10)
time.Sleep(time.Duration(r) * time.Microsecond)
go func(locker statemgr.Locker, n int) {
defer wg.Done()

li := state.NewLockInfo()
li.Operation = "test"
li.Who = fmt.Sprintf("client-%v", i)
_, err := locker.Lock(li)
if err == nil {
t.Logf("[INFO] Client %v got the lock\r\n", i)
lockCount++
li.Who = fmt.Sprintf("client-%v", n)

for i := 0; i < lockAttempts; i++ {
id, err := locker.Lock(li)
if err != nil {
continue
}

err = locker.Unlock(id)
if err != nil {
t.Errorf("failed to unlock: %v", err)
}
}
wg.Done()
}(l, i)
}

wg.Wait()

if lockCount > 1 {
t.Fatalf("multiple backend clients were able to acquire a lock, count: %v", lockCount)
}

if lockCount == 0 {
t.Fatal("no clients were able to acquire a lock")
}
}

func cleanupK8sResources(t *testing.T) {
Expand Down

0 comments on commit 81ecc55

Please sign in to comment.