forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make client.Context implement gRPC ClientConn (cosmos#6342)
* 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
1 parent
c44813b
commit 4e0a475
Showing
6 changed files
with
87 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |