Skip to content

Commit

Permalink
test: check race condition during unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeamon committed Nov 4, 2023
1 parent b772a02 commit 0740780
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ clean: ## Remove temporary files and cached tests results.
test: clean ## Remove cache and Run unit tests only.
go test -v -timeout 2m -count=1 ./...

.PHONY: test-race
test-race: clean ## Remove cache and Run unit tests with race flag.
CGO_ENABLED=1 go test -race -v -timeout 2m -count=1 ./...

.PHONY: test-cover
test-cover: clean ## Remove tests cache and run tests with coverage.
go test -v -timeout 2m -coverprofile=coverage.out ./... && go tool cover -func=coverage.out
go test -race -v -timeout 2m -coverprofile=coverage.out ./... && go tool cover -func=coverage.out

.PHONY: coverc
coverc: clean ## Testing coverage and view stats in console.
Expand Down
27 changes: 21 additions & 6 deletions gorqs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"reflect"
"strings"
"sync"
"testing"
"time"
)
Expand Down Expand Up @@ -302,11 +303,14 @@ func TestSyncQueue_Basic(t *testing.T) {
done <- struct{}{}
}()

mu := &sync.Mutex{}
results := make([]string, 0, 3)
makeJob := func(id string) Runner {
return basicTestJob(func() error {
time.Sleep(10 * time.Millisecond)
mu.Lock()
results = append(results, id)
mu.Unlock()
return nil
})
}
Expand Down Expand Up @@ -402,12 +406,14 @@ func TestAsyncQueue_Basic(t *testing.T) {
}
done <- struct{}{}
}()

mu := &sync.Mutex{}
results := make([]string, 0, 3)
makeJob := func(id string) Runner {
return basicTestJob(func() error {
time.Sleep(time.Second)
mu.Lock()
results = append(results, id)
mu.Unlock()
return nil
})
}
Expand Down Expand Up @@ -460,12 +466,14 @@ func TestSyncQueue_StopOngoing(t *testing.T) {
}
done <- struct{}{}
}()

mu := &sync.Mutex{}
results := make([]string, 0, 2)
makeJob := func(id string) Runner {
return basicTestJob(func() error {
time.Sleep(time.Second)
mu.Lock()
results = append(results, id)
mu.Unlock()
return nil
})
}
Expand All @@ -489,8 +497,10 @@ func TestSyncQueue_StopOngoing(t *testing.T) {
case <-time.After(2 * time.Second):
t.Error("running queue did not exit.")
}

if lg := len(results); lg != 2 {
mu.Lock()
lg := len(results)
mu.Unlock()
if lg != 2 {
t.Fatalf("invalid results length. expected 2 but got %d", lg)
return
}
Expand All @@ -517,12 +527,14 @@ func TestSyncQueue_TimeoutOngoing(t *testing.T) {
}
done <- struct{}{}
}()

mu := &sync.Mutex{}
results := make([]string, 0, 2)
makeJob := func(id string) Runner {
return basicTestJob(func() error {
time.Sleep(time.Second)
mu.Lock()
results = append(results, id)
mu.Unlock()
return nil
})
}
Expand All @@ -540,7 +552,10 @@ func TestSyncQueue_TimeoutOngoing(t *testing.T) {
t.Error("running queue did not exit.")
}

if lg := len(results); lg != 2 {
mu.Lock()
lg := len(results)
mu.Unlock()
if lg != 2 {
t.Fatalf("invalid results. expected 2 items but got %d", lg)
return
}
Expand Down

0 comments on commit 0740780

Please sign in to comment.