mirror of
https://github.com/foomo/gocontentful.git
synced 2025-10-16 12:25:39 +00:00
fix: tests for race condition in GetParents()
This commit is contained in:
parent
7a022326d7
commit
65c1022c2b
2
Makefile
2
Makefile
@ -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
|
||||
|
||||
2
main.go
2
main.go
@ -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"`
|
||||
|
||||
@ -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)
|
||||
@ -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 {
|
||||
@ -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() {
|
||||
@ -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() {
|
||||
@ -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() {
|
||||
@ -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()
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user