Skip to content

Commit

Permalink
test: add initial gRPC server test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mark-rushakoff committed Nov 5, 2024
1 parent 2763096 commit 8052b16
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
72 changes: 72 additions & 0 deletions gserver/internal/ggrpc/ggrpctest/fixture.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
1 change: 1 addition & 0 deletions gserver/internal/ggrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var _ GordianGRPCServer = (*GordianGRPC)(nil)

type GordianGRPC struct {
UnimplementedGordianGRPCServer

log *slog.Logger

fs tmstore.FinalizationStore
Expand Down
34 changes: 34 additions & 0 deletions gserver/internal/ggrpc/server_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions internal/gci/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8052b16

Please sign in to comment.