github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/datastore/test/stats.go (about)

     1  package test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/authzed/spicedb/internal/testfixtures"
    11  )
    12  
    13  const statsRetryCount = 3
    14  
    15  func StatsTest(t *testing.T, tester DatastoreTester) {
    16  	ctx := context.Background()
    17  	require := require.New(t)
    18  
    19  	ds, err := tester.New(0, veryLargeGCInterval, veryLargeGCWindow, 1)
    20  	require.NoError(err)
    21  
    22  	ds, _ = testfixtures.StandardDatastoreWithData(ds, require)
    23  
    24  	// stats use follower reads, need to wait a bit so that the base tables
    25  	// have a chance to be follower-read
    26  	time.Sleep(5 * time.Second)
    27  
    28  	for retryCount := statsRetryCount; retryCount >= 0; retryCount-- {
    29  		stats, err := ds.Statistics(ctx)
    30  		require.NoError(err)
    31  
    32  		require.Len(stats.UniqueID, 36, "unique ID must be a valid UUID")
    33  		require.Len(stats.ObjectTypeStatistics, 3, "must report object stats")
    34  
    35  		if stats.EstimatedRelationshipCount == uint64(0) && retryCount > 0 {
    36  			// Sleep for a bit to get the stats table to update.
    37  			time.Sleep(500 * time.Millisecond)
    38  			continue
    39  		}
    40  
    41  		require.Greater(stats.EstimatedRelationshipCount, uint64(0), "must report some relationships")
    42  
    43  		newStats, err := ds.Statistics(ctx)
    44  		require.NoError(err)
    45  		require.Equal(newStats.UniqueID, stats.UniqueID, "unique ID must be stable")
    46  	}
    47  }