Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add data race detection in test #357

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}