Skip to content

Commit

Permalink
fix: tests for race condition in GetParents()
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian Vidmar committed Mar 15, 2023
1 parent 7a02232 commit 65c1022
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ build:
## Run tests
test:
go run ./main.go -exportfile ./test/test-space-export.json ./test/testapi
go test ./...
go test -count=1 ./...

race:
go run ./main.go -exportfile ./test/test-space-export.json ./test/testapi
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/foomo/gocontentful/erm"
)

var VERSION = "v1.0.16"
var VERSION = "v1.0.17"

type contentfulRc struct {
ManagementToken string `json:"managementToken"`
Expand Down
39 changes: 34 additions & 5 deletions test/concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package test

import (
"context"
"github.com/foomo/gocontentful/test/testapi"
"sync"
"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 {
product, err := contentfulClient.GetProductByID(testProductID)
Expand All @@ -19,6 +24,19 @@ func readWorker(contentfulClient *testapi.ContentfulClient, i int) error {
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 {
product, err := contentfulClient.GetProductByID(testProductID)
if err != nil {
Expand All @@ -39,7 +57,7 @@ func TestConcurrentReadWrites(t *testing.T) {
testLogger.Errorf("testConcurrentReadWrites: %v", err)
}
var wg sync.WaitGroup
for i := 1; i <= 1000; i++ {
for i := 1; i <= concurrency; i++ {
wg.Add(1)
i := i
go func() {
Expand All @@ -51,7 +69,7 @@ func TestConcurrentReadWrites(t *testing.T) {
}
}()
}
for i := 1; i <= 1000; i++ {
for i := 1; i <= concurrency; i++ {
wg.Add(1)
i := i
go func() {
Expand All @@ -62,7 +80,7 @@ func TestConcurrentReadWrites(t *testing.T) {
}
}()
}
for i := 1; i <= 1000; i++ {
for i := 1; i <= concurrency; i++ {
wg.Add(1)
i := i
go func() {
Expand All @@ -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()
}

0 comments on commit 65c1022

Please sign in to comment.