fix: tests for race condition in GetParents()

This commit is contained in:
Cristian Vidmar 2023-03-15 16:17:13 +01:00
parent 7a022326d7
commit 65c1022c2b
3 changed files with 36 additions and 7 deletions

View File

@ -16,7 +16,7 @@ build:
## Run tests ## Run tests
test: test:
go run ./main.go -exportfile ./test/test-space-export.json ./test/testapi go run ./main.go -exportfile ./test/test-space-export.json ./test/testapi
go test ./... go test -count=1 ./...
race: race:
go run ./main.go -exportfile ./test/test-space-export.json ./test/testapi go run ./main.go -exportfile ./test/test-space-export.json ./test/testapi

View File

@ -13,7 +13,7 @@ import (
"github.com/foomo/gocontentful/erm" "github.com/foomo/gocontentful/erm"
) )
var VERSION = "v1.0.16" var VERSION = "v1.0.17"
type contentfulRc struct { type contentfulRc struct {
ManagementToken string `json:"managementToken"` ManagementToken string `json:"managementToken"`

View File

@ -2,12 +2,17 @@ package test
import ( import (
"context" "context"
"github.com/foomo/gocontentful/test/testapi"
"sync" "sync"
"testing" "testing"
"github.com/foomo/gocontentful/test/testapi"
) )
var testProductID = "6dbjWqNd9SqccegcqYq224" var (
testProductID = "6dbjWqNd9SqccegcqYq224"
testBrandID = "651CQ8rLoIYCeY6G0QG22q"
concurrency = 10000
)
func readWorker(contentfulClient *testapi.ContentfulClient, i int) error { func readWorker(contentfulClient *testapi.ContentfulClient, i int) error {
product, err := contentfulClient.GetProductByID(testProductID) product, err := contentfulClient.GetProductByID(testProductID)
@ -19,6 +24,19 @@ func readWorker(contentfulClient *testapi.ContentfulClient, i int) error {
return nil return nil
} }
func parentWorker(contentfulClient *testapi.ContentfulClient, i int) error {
brand, err := contentfulClient.GetBrandByID(testBrandID)
if err != nil {
return err
}
parents, err := brand.GetParents()
if err != nil {
return err
}
testLogger.Infof("Parent worker %d found %d brand parents", i, len(parents))
return nil
}
func writeWorker(contentfulClient *testapi.ContentfulClient, i int) error { func writeWorker(contentfulClient *testapi.ContentfulClient, i int) error {
product, err := contentfulClient.GetProductByID(testProductID) product, err := contentfulClient.GetProductByID(testProductID)
if err != nil { if err != nil {
@ -39,7 +57,7 @@ func TestConcurrentReadWrites(t *testing.T) {
testLogger.Errorf("testConcurrentReadWrites: %v", err) testLogger.Errorf("testConcurrentReadWrites: %v", err)
} }
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 1; i <= 1000; i++ { for i := 1; i <= concurrency; i++ {
wg.Add(1) wg.Add(1)
i := i i := i
go func() { go func() {
@ -51,7 +69,7 @@ func TestConcurrentReadWrites(t *testing.T) {
} }
}() }()
} }
for i := 1; i <= 1000; i++ { for i := 1; i <= concurrency; i++ {
wg.Add(1) wg.Add(1)
i := i i := i
go func() { go func() {
@ -62,7 +80,7 @@ func TestConcurrentReadWrites(t *testing.T) {
} }
}() }()
} }
for i := 1; i <= 1000; i++ { for i := 1; i <= concurrency; i++ {
wg.Add(1) wg.Add(1)
i := i i := i
go func() { go func() {
@ -73,5 +91,16 @@ func TestConcurrentReadWrites(t *testing.T) {
} }
}() }()
} }
for i := 1; i <= concurrency; i++ {
wg.Add(1)
i := i
go func() {
defer wg.Done()
err := parentWorker(contentfulClient, i)
if err != nil {
testLogger.Errorf("testConcurrentReadWrites: %v", err)
}
}()
}
wg.Wait() wg.Wait()
} }