Skip to content

Commit

Permalink
Make client.Context implement gRPC ClientConn (cosmos#6342)
Browse files Browse the repository at this point in the history
* Add QueryConn to client.Context

* Add integration test

* imports

* spelling

* Make client.Context implement grpc ClientConn

Co-authored-by: Alexander Bezobchuk <[email protected]>
Co-authored-by: Federico Kunze <[email protected]>
  • Loading branch information
3 people authored Jun 8, 2020
1 parent c44813b commit 4e0a475
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 18 deletions.
2 changes: 1 addition & 1 deletion baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ func TestGRPCQuery(t *testing.T) {
grpcQueryOpt := func(bapp *BaseApp) {
testdata.RegisterTestServiceServer(
bapp.GRPCQueryRouter(),
testServer{},
testdata.TestServiceImpl{},
)
}

Expand Down
16 changes: 1 addition & 15 deletions baseapp/grpcrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package baseapp

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -11,22 +10,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

type testServer struct{}

func (e testServer) Echo(_ context.Context, req *testdata.EchoRequest) (*testdata.EchoResponse, error) {
return &testdata.EchoResponse{Message: req.Message}, nil
}

func (e testServer) SayHello(_ context.Context, request *testdata.SayHelloRequest) (*testdata.SayHelloResponse, error) {
greeting := fmt.Sprintf("Hello %s!", request.Name)
return &testdata.SayHelloResponse{Greeting: greeting}, nil
}

var _ testdata.TestServiceServer = testServer{}

func TestGRPCRouter(t *testing.T) {
qr := NewGRPCQueryRouter()
testdata.RegisterTestServiceServer(qr, testServer{})
testdata.RegisterTestServiceServer(qr, testdata.TestServiceImpl{})
helper := &QueryServiceTestHelper{
GRPCQueryRouter: qr,
ctx: sdk.Context{},
Expand Down
33 changes: 33 additions & 0 deletions client/grpc_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package client

import (
gocontext "context"
"fmt"

gogogrpc "github.com/gogo/protobuf/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding"
"google.golang.org/grpc/encoding/proto"
)

var _ gogogrpc.ClientConn = Context{}

var protoCodec = encoding.GetCodec(proto.Name)

// Invoke implements the grpc ClientConn.Invoke method
func (ctx Context) Invoke(_ gocontext.Context, method string, args, reply interface{}, _ ...grpc.CallOption) error {
reqBz, err := protoCodec.Marshal(args)
if err != nil {
return err
}
resBz, _, err := ctx.QueryWithData(method, reqBz)
if err != nil {
return err
}
return protoCodec.Unmarshal(resBz, reply)
}

// NewStream implements the grpc ClientConn.NewStream method
func (Context) NewStream(gocontext.Context, *grpc.StreamDesc, string, ...grpc.CallOption) (grpc.ClientStream, error) {
return nil, fmt.Errorf("streaming rpc not supported")
}
19 changes: 19 additions & 0 deletions codec/testdata/test_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package testdata

import (
"context"
"fmt"
)

type TestServiceImpl struct{}

func (e TestServiceImpl) Echo(_ context.Context, req *EchoRequest) (*EchoResponse, error) {
return &EchoResponse{Message: req.Message}, nil
}

func (e TestServiceImpl) SayHello(_ context.Context, request *SayHelloRequest) (*SayHelloResponse, error) {
greeting := fmt.Sprintf("Hello %s!", request.Name)
return &SayHelloResponse{Greeting: greeting}, nil
}

var _ TestServiceServer = TestServiceImpl{}
7 changes: 5 additions & 2 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"io"
"os"

"github.com/cosmos/cosmos-sdk/codec/types"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/testdata"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/std"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand Down Expand Up @@ -306,6 +306,9 @@ func NewSimApp(
app.mm.RegisterRoutes(app.Router(), app.QueryRouter())
app.mm.RegisterQueryServices(app.GRPCQueryRouter())

// add test gRPC service for testing gRPC queries in isolation
testdata.RegisterTestServiceServer(app.GRPCQueryRouter(), testdata.TestServiceImpl{})

// create the simulation manager and define the order of the modules for deterministic simulations
//
// NOTE: this is not required apps that don't use the simulator for fuzz testing
Expand Down
28 changes: 28 additions & 0 deletions tests/cli/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// +build cli_test

package cli

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec/testdata"
)

func TestCliQueryConn(t *testing.T) {
t.Parallel()
f := NewFixtures(t)

// start simd server
proc := f.SDStart()
t.Cleanup(func() { proc.Stop(false) })

ctx := client.NewContext()
testClient := testdata.NewTestServiceClient(ctx)
res, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"})
require.NoError(t, err)
require.Equal(t, "hello", res.Message)
}

0 comments on commit 4e0a475

Please sign in to comment.