Enable race detection in integration tests (#224)

* Enable race detection in integration tests

Move tests specific to race detection to their own source file under the
race build tag.
Removed error checking for concurrent test and updated comment.

* add missing target

* add missing test case reporting

* check for error on Recover()
This commit is contained in:
Joel Hendrix 2021-07-15 14:59:46 -07:00 коммит произвёл GitHub
Родитель 6e9e517e48
Коммит 03123a928b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 59 добавлений и 40 удалений

Просмотреть файл

@ -11,6 +11,7 @@ GOFMT = gofmt
GOCYCLO = gocyclo
GOLINT = $(BIN)/golint
GOSTATICCHECK = $(BIN)/staticcheck
GOJUNITRPT = go-junit-report
V = 0
Q = $(if $(filter 1,$V),,@)
@ -26,17 +27,20 @@ build: | ; $(info $(M) building library…) @ ## Build program
# Tests
TEST_TARGETS := test-default test-bench test-verbose test-race test-debug test-cover
TEST_TARGETS := test-default test-bench test-verbose test-race test-debug test-cover test-full
.PHONY: $(TEST_TARGETS) test-xml check test tests
test-bench: ARGS=-run=__absolutelynothing__ -bench=. ## Run benchmarks
test-verbose: ARGS=-v ## Run tests in verbose mode
test-debug: ARGS=-v -debug ## Run tests in verbose mode with debug output
test-race: ARGS=-race ## Run tests with race detector
test-cover: ARGS=-cover -coverprofile=cover.out -v ## Run tests in verbose mode with coverage
test-full: ARGS=-cover -coverprofile=cover.out -v -race ## Run tests with code coverage and race detection
$(TEST_TARGETS): NAME=$(MAKECMDGOALS:test-%=%)
$(TEST_TARGETS): test
check test tests: cyclo lint vet terraform.tfstate; $(info $(M) running $(NAME:%=% )tests) @ ## Run tests
$(GO) test -timeout $(TIMEOUT)s $(ARGS) ./...
$(GO) test -timeout $(TIMEOUT)s $(ARGS) ./... 2>&1 | tee gotestoutput.log && \
$(GOJUNITRPT) < gotestoutput.log > report.xml && \
rm -f gotestoutput.log
.PHONY: vet
vet: ; $(info $(M) running vet) @ ## Run vet

Просмотреть файл

@ -40,7 +40,7 @@ steps:
export PATH="~/bin:$PATH"
export GO111MODULE=on
cd '$(sdkPath)'
make test-cover
make test-full
make destroy
gocov convert cover.out > coverage.json
gocov-xml < coverage.json > coverage.xml

Просмотреть файл

@ -306,27 +306,6 @@ func (suite *eventHubSuite) TestWebSocket() {
}
}
func (suite *eventHubSuite) TestConcurrency() {
tests := map[string]func(context.Context, *testing.T, *Hub, string){
"TestConcurrentSendWithRecover": testConcurrentSendWithRecover,
}
for name, testFunc := range tests {
setupTestTeardown := func(t *testing.T) {
hub, cleanup := suite.RandomHub()
defer cleanup()
partitionID := (*hub.PartitionIds)[0]
client, closer := suite.newClient(t, *hub.Name, HubWithPartitionedSender(partitionID))
defer closer()
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
testFunc(ctx, t, client, partitionID)
}
suite.T().Run(name, setupTestTeardown)
}
}
func testBasicSend(ctx context.Context, t *testing.T, client *Hub, _ string) {
err := client.Send(ctx, NewEventFromString("Hello!"))
assert.NoError(t, err)
@ -425,22 +404,6 @@ func testBasicSendAndReceive(ctx context.Context, t *testing.T, client *Hub, par
}
}
func testConcurrentSendWithRecover(ctx context.Context, t *testing.T, client *Hub, _ string) {
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
err := client.Send(ctx, NewEventFromString("Hello!"))
assert.NoError(t, err)
err = client.sender.Recover(ctx)
assert.NoError(t, err)
}()
}
end, _ := ctx.Deadline()
waitUntil(t, &wg, time.Until(end))
}
func (suite *eventHubSuite) TestEpochReceivers() {
tests := map[string]func(context.Context, *testing.T, *Hub, []string, string){
"TestEpochGreaterThenLess": testEpochGreaterThenLess,

52
races_test.go Normal file
Просмотреть файл

@ -0,0 +1,52 @@
// +build race
package eventhub
import (
"context"
"sync"
"testing"
"time"
)
func (suite *eventHubSuite) TestConcurrency() {
tests := map[string]func(context.Context, *testing.T, *Hub, string){
"TestConcurrentSendWithRecover": testConcurrentSendWithRecover,
}
for name, testFunc := range tests {
setupTestTeardown := func(t *testing.T) {
hub, cleanup := suite.RandomHub()
defer cleanup()
partitionID := (*hub.PartitionIds)[0]
client, closer := suite.newClient(t, *hub.Name, HubWithPartitionedSender(partitionID))
defer closer()
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
testFunc(ctx, t, client, partitionID)
}
suite.T().Run(name, setupTestTeardown)
}
}
func testConcurrentSendWithRecover(ctx context.Context, t *testing.T, client *Hub, _ string) {
var wg sync.WaitGroup
var err error
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
// we don't check for errors here as the call to Recover()
// can cancel any in-flight calls to Send(). this is only
// an interesting test when race detection is enabled.
client.Send(ctx, NewEventFromString("Hello!"))
if inner := client.sender.Recover(ctx); inner != nil {
err = inner
}
}()
}
end, _ := ctx.Deadline()
waitUntil(t, &wg, time.Until(end))
assert.NoError(t, err)
}