From 8052b16b88ac109ae6b529c23221551d7705842e Mon Sep 17 00:00:00 2001 From: Mark Rushakoff Date: Tue, 5 Nov 2024 16:46:11 -0500 Subject: [PATCH] test: add initial gRPC server test Including a fixture to simplify upcoming gRPC server tests. Also call shimGordianClient where it probably needed to be called, although I did not manually test that it is working as intended. --- gserver/internal/ggrpc/ggrpctest/fixture.go | 72 +++++++++++++++++++++ gserver/internal/ggrpc/server.go | 1 + gserver/internal/ggrpc/server_test.go | 34 ++++++++++ internal/gci/command.go | 4 +- 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 gserver/internal/ggrpc/ggrpctest/fixture.go create mode 100644 gserver/internal/ggrpc/server_test.go diff --git a/gserver/internal/ggrpc/ggrpctest/fixture.go b/gserver/internal/ggrpc/ggrpctest/fixture.go new file mode 100644 index 0000000..074841c --- /dev/null +++ b/gserver/internal/ggrpc/ggrpctest/fixture.go @@ -0,0 +1,72 @@ +package ggrpctest + +import ( + "context" + "net" + "testing" + + "github.com/gordian-engine/gcosmos/gserver/internal/ggrpc" + "github.com/gordian-engine/gcosmos/internal/copy/gtest" + "github.com/gordian-engine/gordian/tm/tmstore/tmmemstore" + "github.com/stretchr/testify/require" + "google.golang.org/grpc" +) + +// Fixture contains a gRPC server and client, +// ready to use for testing the gRPC services. +type Fixture struct { + Server *ggrpc.GordianGRPC + Client ggrpc.GordianGRPCClient + + // TODO: either pull up the rest of the necessary fields from GRPCServerConfig + // or just outright expose GRPCServerConfig. + FinalizationStore *tmmemstore.FinalizationStore + MirrorStore *tmmemstore.MirrorStore +} + +// NewFixture returns a new fixture whose lifecycle is associated +// with the given context. +// It registers cleanup through t.Cleanup. +// The caller must be sure to defer context cancellation +// in order for the fixture to shut down properly. +func NewFixture(t *testing.T, ctx context.Context) Fixture { + t.Helper() + + log := gtest.NewLogger(t) + + ln, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + t.Cleanup(func() { + _ = ln.Close() + }) + + fs := tmmemstore.NewFinalizationStore() + ms := tmmemstore.NewMirrorStore() + + srv := ggrpc.NewGordianGRPCServer( + ctx, + log.With("sys", "server"), + ggrpc.GRPCServerConfig{ + Listener: ln, + + FinalizationStore: fs, + MirrorStore: ms, + }, + ) + t.Cleanup(srv.Wait) + + gc, err := grpc.NewClient( + ln.Addr().String(), + grpc.WithInsecure(), + ) + require.NoError(t, err) + c := ggrpc.NewGordianGRPCClient(gc) + + return Fixture{ + Server: srv, + Client: c, + + FinalizationStore: fs, + MirrorStore: ms, + } +} diff --git a/gserver/internal/ggrpc/server.go b/gserver/internal/ggrpc/server.go index f486ae0..1d1241f 100644 --- a/gserver/internal/ggrpc/server.go +++ b/gserver/internal/ggrpc/server.go @@ -20,6 +20,7 @@ var _ GordianGRPCServer = (*GordianGRPC)(nil) type GordianGRPC struct { UnimplementedGordianGRPCServer + log *slog.Logger fs tmstore.FinalizationStore diff --git a/gserver/internal/ggrpc/server_test.go b/gserver/internal/ggrpc/server_test.go new file mode 100644 index 0000000..366199b --- /dev/null +++ b/gserver/internal/ggrpc/server_test.go @@ -0,0 +1,34 @@ +package ggrpc_test + +import ( + context "context" + "testing" + + "github.com/gordian-engine/gcosmos/gserver/internal/ggrpc" + "github.com/gordian-engine/gcosmos/gserver/internal/ggrpc/ggrpctest" + "github.com/stretchr/testify/require" +) + +func TestGRPCServer_GetBlocksWatermark(t *testing.T) { + t.Parallel() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + fx := ggrpctest.NewFixture(t, ctx) + defer cancel() + + require.NoError(t, fx.MirrorStore.SetNetworkHeightRound( + ctx, + 10, 1, + 9, 3, + )) + + resp, err := fx.Client.GetBlocksWatermark(ctx, new(ggrpc.CurrentBlockRequest)) + require.NoError(t, err) + + require.Equal(t, uint64(10), resp.VotingHeight) + require.Equal(t, uint32(1), resp.VotingRound) + require.Equal(t, uint64(9), resp.CommittingHeight) + require.Equal(t, uint32(3), resp.CommittingRound) +} diff --git a/internal/gci/command.go b/internal/gci/command.go index c265f4d..54fec69 100644 --- a/internal/gci/command.go +++ b/internal/gci/command.go @@ -175,11 +175,11 @@ func NewGcosmosCommand( panic(fmt.Errorf("failed to enhance root command: %w", err)) } + shimGordianClient(rootCmd) + return rootCmd } -// shimGordianClient used to be part of the old gcosmos command, -// and we probably need to use this in NewGcosmosCommand. func shimGordianClient(cmd *cobra.Command) { origPersistentPreRunE := cmd.PersistentPreRunE