Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions pkg/datalayer/hashcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"

"github.com/authzed/spicedb/pkg/datastore"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
Expand Down Expand Up @@ -112,48 +113,37 @@ func TestSchemaHashCache_GetOrLoadEmptyHash(t *testing.T) {
func TestSchemaHashCache_Singleflight(t *testing.T) {
shc := newSchemaHashCache(newTestSchemaCache())

loadCalls := 0
var loadCalls atomic.Int32
loadStarted := make(chan struct{})
loadContinue := make(chan struct{})

loader := func(_ context.Context) (*datastore.ReadOnlyStoredSchema, error) {
loadCalls++
close(loadStarted)
if loadCalls.Add(1) == 1 {
close(loadStarted)
}
<-loadContinue
return makeTestSchema("loaded definition"), nil
}

const numGoroutines = 10
var wg sync.WaitGroup
wg.Add(numGoroutines)

results := make(chan error, numGoroutines)
for i := 0; i < numGoroutines; i++ {
go func() {
defer wg.Done()
var g errgroup.Group
for range numGoroutines {
g.Go(func() error {
schema, err := shc.GetOrLoad(t.Context(), datastore.NoRevision, SchemaHash("hash1"), loader)
if err != nil {
results <- err
return
return err
}
if schema == nil {
results <- fmt.Errorf("schema is nil")
return
return fmt.Errorf("schema is nil")
}
results <- nil
}()
return nil
})
}

<-loadStarted
close(loadContinue)
wg.Wait()
close(results)

for err := range results {
require.NoError(t, err)
}

require.Equal(t, 1, loadCalls)
require.NoError(t, g.Wait())
require.Equal(t, int32(1), loadCalls.Load())
}

func TestSchemaHashCache_LoadError(t *testing.T) {
Expand Down
Loading