Skip to content

Commit

Permalink
add data race detection in test (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
haouc authored and yash97 committed Aug 28, 2024
1 parent 25057d2 commit e4ac94b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ verify:

## Run unit tests
test: verify
go test ./pkg/... ./controllers/... ./webhooks/... -coverprofile cover.out
go test -race ./pkg/... ./controllers/... ./webhooks/... -coverprofile cover.out

test-e2e:
KUBE_CONFIG_PATH=${KUBE_CONFIG_PATH} REGION=${AWS_REGION} CLUSTER_NAME=${CLUSTER_NAME} ./scripts/test/run-integration-tests.sh
Expand Down
21 changes: 18 additions & 3 deletions pkg/worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package worker
import (
"context"
"fmt"
"sync"
"testing"
"time"

Expand All @@ -34,15 +35,18 @@ var (
maxRequeue = 3
)

var mu sync.RWMutex

func GetMockWorkerPool(ctx context.Context) Worker {
log := zap.New(zap.UseDevMode(true)).WithValues("worker resource Id", resourceName)
return NewDefaultWorkerPool(resourceName, workerCount, maxRequeue, log, ctx)
}

func MockWorkerFunc(job interface{}) (result ctrl.Result, err error) {
mu.Lock()
defer mu.Unlock()
v := job.(*int)
*v++
time.Sleep(time.Millisecond * mockTimeToProcessWorkerFunc)

return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -75,15 +79,20 @@ func TestWorker_SubmitJob(t *testing.T) {
time.Sleep(time.Millisecond * (mockTimeToProcessWorkerFunc + bufferTimeBwWorkerFuncExecution) * time.Duration(jobCount))

// Verify job completed.
assert.Equal(t, job1, 1)
assert.Equal(t, job2, 1)
mu.RLock()
defer mu.RUnlock()
for _, j := range []int{job1, job2} {
assert.Equal(t, j, 1)
}
}

func TestWorker_SubmitJob_RequeueOnError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

workerFunc := func(job interface{}) (result ctrl.Result, err error) {
mu.Lock()
defer mu.Unlock()
invoked := job.(*int)
*invoked++

Expand All @@ -100,14 +109,18 @@ func TestWorker_SubmitJob_RequeueOnError(t *testing.T) {
time.Sleep((mockTimeToProcessWorkerFunc + bufferTimeBwWorkerFuncExecution) * time.Millisecond * time.Duration(maxRequeue))

// expected invocation = max requeue + the first invocation
mu.RLock()
assert.Equal(t, maxRequeue+1, invoked)
mu.RUnlock()
}

func TestWorker_SubmitJob_NotRequeueOnError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

workerFunc := func(job interface{}) (result ctrl.Result, err error) {
mu.Lock()
defer mu.Unlock()
invoked := job.(*int)
*invoked++

Expand All @@ -127,5 +140,7 @@ func TestWorker_SubmitJob_NotRequeueOnError(t *testing.T) {
actualInqueue := 1
// invoked should be only incremented once
assert.NotEqual(t, maxRequeue, actualInqueue)
mu.RLock()
assert.Equal(t, actualInqueue, invoked)
mu.RUnlock()
}

0 comments on commit e4ac94b

Please sign in to comment.