github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/services/integrationtesting/healthcheck_test.go (about)

     1  //go:build ci && docker && !skipintegrationtests
     2  // +build ci,docker,!skipintegrationtests
     3  
     4  package integrationtesting_test
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  	"time"
    10  
    11  	v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
    12  	"github.com/stretchr/testify/require"
    13  	"google.golang.org/grpc"
    14  	healthpb "google.golang.org/grpc/health/grpc_health_v1"
    15  
    16  	"github.com/authzed/spicedb/internal/datastore/memdb"
    17  	tf "github.com/authzed/spicedb/internal/testfixtures"
    18  	"github.com/authzed/spicedb/internal/testserver"
    19  	testdatastore "github.com/authzed/spicedb/internal/testserver/datastore"
    20  	"github.com/authzed/spicedb/internal/testserver/datastore/config"
    21  	dsconfig "github.com/authzed/spicedb/pkg/cmd/datastore"
    22  	"github.com/authzed/spicedb/pkg/datastore"
    23  )
    24  
    25  func TestHealthCheck(t *testing.T) {
    26  	for _, engine := range datastore.Engines {
    27  		b := testdatastore.RunDatastoreEngine(t, engine)
    28  		t.Run(engine, func(t *testing.T) {
    29  			require := require.New(t)
    30  
    31  			t.Logf("Running %s health check test", engine)
    32  
    33  			connPoolConfig := dsconfig.NewConnPoolConfigWithOptionsAndDefaults(
    34  				dsconfig.WithMinOpenConns(1),
    35  				dsconfig.WithMaxOpenConns(5),
    36  				dsconfig.WithHealthCheckInterval(10*time.Second),
    37  			)
    38  			ds := b.NewDatastore(t, config.DatastoreConfigInitFunc(t,
    39  				dsconfig.WithWatchBufferLength(0),
    40  				dsconfig.WithGCWindow(time.Duration(90_000_000_000_000)),
    41  				dsconfig.WithRevisionQuantization(10),
    42  				dsconfig.WithMaxRetries(50),
    43  				dsconfig.WithReadConnPool(*connPoolConfig),
    44  				dsconfig.WithWriteConnPool(*connPoolConfig),
    45  				dsconfig.WithRequestHedgingEnabled(false)))
    46  			ds, _ = tf.StandardDatastoreWithData(ds, require)
    47  
    48  			dispatchConns, cleanup := testserver.TestClusterWithDispatch(t, 2, ds)
    49  			t.Cleanup(cleanup)
    50  
    51  			runHealthChecks(require, dispatchConns[0])
    52  		})
    53  	}
    54  
    55  	require := require.New(t)
    56  	// Check server without dispatching
    57  	conn, cleanup, _, _ := testserver.NewTestServer(require, 0, memdb.DisableGC, true, tf.StandardDatastoreWithData)
    58  	t.Cleanup(cleanup)
    59  	runHealthChecks(require, conn)
    60  }
    61  
    62  func runHealthChecks(require *require.Assertions, conn *grpc.ClientConn) {
    63  	hclient := healthpb.NewHealthClient(conn)
    64  
    65  	require.Eventually(func() bool {
    66  		resp, err := hclient.Check(context.Background(), &healthpb.HealthCheckRequest{Service: v1.PermissionsService_ServiceDesc.ServiceName})
    67  		require.NoError(err)
    68  		return healthpb.HealthCheckResponse_SERVING == resp.GetStatus()
    69  	}, 60*time.Second, 100*time.Millisecond)
    70  }