internal: delete mock from examples (#3766)
This commit is contained in:
Родитель
1c32b02682
Коммит
d6c4e49aab
|
@ -1,182 +0,0 @@
|
|||
# Mocking Service for gRPC
|
||||
|
||||
[Example code unary RPC](https://github.com/grpc/grpc-go/tree/master/examples/helloworld/mock_helloworld)
|
||||
|
||||
[Example code streaming RPC](https://github.com/grpc/grpc-go/tree/master/examples/route_guide/mock_routeguide)
|
||||
|
||||
## Why?
|
||||
|
||||
To test client-side logic without the overhead of connecting to a real server. Mocking enables users to write light-weight unit tests to check functionalities on client-side without invoking RPC calls to a server.
|
||||
|
||||
## Idea: Mock the client stub that connects to the server.
|
||||
|
||||
We use Gomock to mock the client interface (in the generated code) and programmatically set its methods to expect and return pre-determined values. This enables users to write tests around the client logic and use this mocked stub while making RPC calls.
|
||||
|
||||
## How to use Gomock?
|
||||
|
||||
Documentation on Gomock can be found [here](https://github.com/golang/mock).
|
||||
A quick reading of the documentation should enable users to follow the code below.
|
||||
|
||||
Consider a gRPC service based on following proto file:
|
||||
|
||||
```proto
|
||||
//helloworld.proto
|
||||
|
||||
package helloworld;
|
||||
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message HelloReply {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
service Greeter {
|
||||
rpc SayHello (HelloRequest) returns (HelloReply) {}
|
||||
}
|
||||
```
|
||||
|
||||
The generated file helloworld.pb.go will have a client interface for each service defined in the proto file. This interface will have methods corresponding to each rpc inside that service.
|
||||
|
||||
```Go
|
||||
type GreeterClient interface {
|
||||
SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error)
|
||||
}
|
||||
```
|
||||
|
||||
The generated code also contains a struct that implements this interface.
|
||||
|
||||
```Go
|
||||
type greeterClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
func (c *greeterClient) SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error){
|
||||
// ...
|
||||
// gRPC specific code here
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
Along with this the generated code has a method to create an instance of this struct.
|
||||
```Go
|
||||
func NewGreeterClient(cc *grpc.ClientConn) GreeterClient
|
||||
```
|
||||
|
||||
The user code uses this function to create an instance of the struct greeterClient which then can be used to make rpc calls to the server.
|
||||
We will mock this interface GreeterClient and use an instance of that mock to make rpc calls. These calls instead of going to server will return pre-determined values.
|
||||
|
||||
To create a mock we’ll use [mockgen](https://github.com/golang/mock#running-mockgen).
|
||||
From the directory ``` examples/helloworld/ ``` run ``` mockgen google.golang.org/grpc/examples/helloworld/helloworld GreeterClient > mock_helloworld/hw_mock.go ```
|
||||
|
||||
Notice that in the above command we specify GreeterClient as the interface to be mocked.
|
||||
|
||||
The user test code can import the package generated by mockgen along with library package gomock to write unit tests around client-side logic.
|
||||
```Go
|
||||
import "github.com/golang/mock/gomock"
|
||||
import hwmock "google.golang.org/grpc/examples/helloworld/mock_helloworld"
|
||||
```
|
||||
|
||||
An instance of the mocked interface can be created as:
|
||||
```Go
|
||||
mockGreeterClient := hwmock.NewMockGreeterClient(ctrl)
|
||||
```
|
||||
This mocked object can be programmed to expect calls to its methods and return pre-determined values. For instance, we can program mockGreeterClient to expect a call to its method SayHello and return a HelloReply with message “Mocked RPC”.
|
||||
|
||||
```Go
|
||||
mockGreeterClient.EXPECT().SayHello(
|
||||
gomock.Any(), // expect any value for first parameter
|
||||
gomock.Any(), // expect any value for second parameter
|
||||
).Return(&helloworld.HelloReply{Message: “Mocked RPC”}, nil)
|
||||
```
|
||||
|
||||
gomock.Any() indicates that the parameter can have any value or type. We can indicate specific values for built-in types with gomock.Eq().
|
||||
However, if the test code needs to specify the parameter to have a proto message type, we can replace gomock.Any() with an instance of a struct that implements gomock.Matcher interface.
|
||||
|
||||
```Go
|
||||
type rpcMsg struct {
|
||||
msg proto.Message
|
||||
}
|
||||
|
||||
func (r *rpcMsg) Matches(msg interface{}) bool {
|
||||
m, ok := msg.(proto.Message)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return proto.Equal(m, r.msg)
|
||||
}
|
||||
|
||||
func (r *rpcMsg) String() string {
|
||||
return fmt.Sprintf("is %s", r.msg)
|
||||
}
|
||||
|
||||
...
|
||||
|
||||
req := &helloworld.HelloRequest{Name: "unit_test"}
|
||||
mockGreeterClient.EXPECT().SayHello(
|
||||
gomock.Any(),
|
||||
&rpcMsg{msg: req},
|
||||
).Return(&helloworld.HelloReply{Message: "Mocked Interface"}, nil)
|
||||
```
|
||||
|
||||
## Mock streaming RPCs:
|
||||
|
||||
For our example we consider the case of bi-directional streaming RPCs. Concretely, we'll write a test for RouteChat function from the route guide example to demonstrate how to write mocks for streams.
|
||||
|
||||
RouteChat is a bi-directional streaming RPC, which means calling RouteChat returns a stream that can __Send__ and __Recv__ messages to and from the server, respectively. We'll start by creating a mock of this stream interface returned by RouteChat and then we'll mock the client interface and set expectation on the method RouteChat to return our mocked stream.
|
||||
|
||||
### Generating mocking code:
|
||||
Like before we'll use [mockgen](https://github.com/golang/mock#running-mockgen). From the `examples/route_guide` directory run: `mockgen google.golang.org/grpc/examples/route_guide/routeguide RouteGuideClient,RouteGuide_RouteChatClient > mock_route_guide/rg_mock.go`
|
||||
|
||||
Notice that we are mocking both client(`RouteGuideClient`) and stream(`RouteGuide_RouteChatClient`) interfaces here.
|
||||
|
||||
This will create a file `rg_mock.go` under directory `mock_route_guide`. This file contains all the mocking code we need to write our test.
|
||||
|
||||
In our test code, like before, we import the this mocking code along with the generated code
|
||||
|
||||
```go
|
||||
import (
|
||||
rgmock "google.golang.org/grpc/examples/route_guide/mock_routeguide"
|
||||
rgpb "google.golang.org/grpc/examples/route_guide/routeguide"
|
||||
)
|
||||
```
|
||||
|
||||
Now considering a test that takes the RouteGuide client object as a parameter, makes a RouteChat rpc call and sends a message on the resulting stream. Furthermore, this test expects to see the same message to be received on the stream.
|
||||
|
||||
```go
|
||||
var msg = ...
|
||||
|
||||
// Creates a RouteChat call and sends msg on it.
|
||||
// Checks if the received message was equal to msg.
|
||||
func testRouteChat(client rgb.RouteChatClient) error{
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
We can inject our mock in here by simply passing it as an argument to the method.
|
||||
|
||||
Creating mock for stream interface:
|
||||
|
||||
```go
|
||||
stream := rgmock.NewMockRouteGuide_RouteChatClient(ctrl)
|
||||
}
|
||||
```
|
||||
|
||||
Setting Expectations:
|
||||
|
||||
```go
|
||||
stream.EXPECT().Send(gomock.Any()).Return(nil)
|
||||
stream.EXPECT().Recv().Return(msg, nil)
|
||||
```
|
||||
|
||||
Creating mock for client interface:
|
||||
|
||||
```go
|
||||
rgclient := rgmock.NewMockRouteGuideClient(ctrl)
|
||||
```
|
||||
|
||||
Setting Expectations:
|
||||
|
||||
```go
|
||||
rgclient.EXPECT().RouteChat(gomock.Any()).Return(stream, nil)
|
||||
```
|
|
@ -3,7 +3,6 @@ module google.golang.org/grpc/examples
|
|||
go 1.11
|
||||
|
||||
require (
|
||||
github.com/golang/mock v1.1.1
|
||||
github.com/golang/protobuf v1.4.2
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
|
||||
google.golang.org/genproto v0.0.0-20200624020401-64a14ca9d1ad
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: google.golang.org/grpc/examples/helloworld/helloworld (interfaces: GreeterClient)
|
||||
|
||||
package mock_helloworld
|
||||
|
||||
import (
|
||||
context "context"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
grpc "google.golang.org/grpc"
|
||||
helloworld "google.golang.org/grpc/examples/helloworld/helloworld"
|
||||
)
|
||||
|
||||
// Mock of GreeterClient interface
|
||||
type MockGreeterClient struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *_MockGreeterClientRecorder
|
||||
}
|
||||
|
||||
// Recorder for MockGreeterClient (not exported)
|
||||
type _MockGreeterClientRecorder struct {
|
||||
mock *MockGreeterClient
|
||||
}
|
||||
|
||||
func NewMockGreeterClient(ctrl *gomock.Controller) *MockGreeterClient {
|
||||
mock := &MockGreeterClient{ctrl: ctrl}
|
||||
mock.recorder = &_MockGreeterClientRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
func (_m *MockGreeterClient) EXPECT() *_MockGreeterClientRecorder {
|
||||
return _m.recorder
|
||||
}
|
||||
|
||||
func (_m *MockGreeterClient) SayHello(_param0 context.Context, _param1 *helloworld.HelloRequest, _param2 ...grpc.CallOption) (*helloworld.HelloReply, error) {
|
||||
_s := []interface{}{_param0, _param1}
|
||||
for _, _x := range _param2 {
|
||||
_s = append(_s, _x)
|
||||
}
|
||||
ret := _m.ctrl.Call(_m, "SayHello", _s...)
|
||||
ret0, _ := ret[0].(*helloworld.HelloReply)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
func (_mr *_MockGreeterClientRecorder) SayHello(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call {
|
||||
_s := append([]interface{}{arg0, arg1}, arg2...)
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "SayHello", _s...)
|
||||
}
|
|
@ -1,79 +0,0 @@
|
|||
/*
|
||||
*
|
||||
* Copyright 2017 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package mock_helloworld_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/golang/protobuf/proto"
|
||||
helloworld "google.golang.org/grpc/examples/helloworld/helloworld"
|
||||
hwmock "google.golang.org/grpc/examples/helloworld/mock_helloworld"
|
||||
"google.golang.org/grpc/internal/grpctest"
|
||||
)
|
||||
|
||||
type s struct {
|
||||
grpctest.Tester
|
||||
}
|
||||
|
||||
func Test(t *testing.T) {
|
||||
grpctest.RunSubTests(t, s{})
|
||||
}
|
||||
|
||||
// rpcMsg implements the gomock.Matcher interface
|
||||
type rpcMsg struct {
|
||||
msg proto.Message
|
||||
}
|
||||
|
||||
func (r *rpcMsg) Matches(msg interface{}) bool {
|
||||
m, ok := msg.(proto.Message)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return proto.Equal(m, r.msg)
|
||||
}
|
||||
|
||||
func (r *rpcMsg) String() string {
|
||||
return fmt.Sprintf("is %s", r.msg)
|
||||
}
|
||||
|
||||
func (s) TestSayHello(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
mockGreeterClient := hwmock.NewMockGreeterClient(ctrl)
|
||||
req := &helloworld.HelloRequest{Name: "unit_test"}
|
||||
mockGreeterClient.EXPECT().SayHello(
|
||||
gomock.Any(),
|
||||
&rpcMsg{msg: req},
|
||||
).Return(&helloworld.HelloReply{Message: "Mocked Interface"}, nil)
|
||||
testSayHello(t, mockGreeterClient)
|
||||
}
|
||||
|
||||
func testSayHello(t *testing.T, client helloworld.GreeterClient) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
r, err := client.SayHello(ctx, &helloworld.HelloRequest{Name: "unit_test"})
|
||||
if err != nil || r.Message != "Mocked Interface" {
|
||||
t.Errorf("mocking failed")
|
||||
}
|
||||
t.Log("Reply : ", r.Message)
|
||||
}
|
|
@ -1,200 +0,0 @@
|
|||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: google.golang.org/grpc/examples/route_guide/routeguide (interfaces: RouteGuideClient,RouteGuide_RouteChatClient)
|
||||
|
||||
package mock_routeguide
|
||||
|
||||
import (
|
||||
context "context"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
grpc "google.golang.org/grpc"
|
||||
routeguide "google.golang.org/grpc/examples/route_guide/routeguide"
|
||||
metadata "google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
// Mock of RouteGuideClient interface
|
||||
type MockRouteGuideClient struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *_MockRouteGuideClientRecorder
|
||||
}
|
||||
|
||||
// Recorder for MockRouteGuideClient (not exported)
|
||||
type _MockRouteGuideClientRecorder struct {
|
||||
mock *MockRouteGuideClient
|
||||
}
|
||||
|
||||
func NewMockRouteGuideClient(ctrl *gomock.Controller) *MockRouteGuideClient {
|
||||
mock := &MockRouteGuideClient{ctrl: ctrl}
|
||||
mock.recorder = &_MockRouteGuideClientRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuideClient) EXPECT() *_MockRouteGuideClientRecorder {
|
||||
return _m.recorder
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuideClient) GetFeature(_param0 context.Context, _param1 *routeguide.Point, _param2 ...grpc.CallOption) (*routeguide.Feature, error) {
|
||||
_s := []interface{}{_param0, _param1}
|
||||
for _, _x := range _param2 {
|
||||
_s = append(_s, _x)
|
||||
}
|
||||
ret := _m.ctrl.Call(_m, "GetFeature", _s...)
|
||||
ret0, _ := ret[0].(*routeguide.Feature)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
func (_mr *_MockRouteGuideClientRecorder) GetFeature(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call {
|
||||
_s := append([]interface{}{arg0, arg1}, arg2...)
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetFeature", _s...)
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuideClient) ListFeatures(_param0 context.Context, _param1 *routeguide.Rectangle, _param2 ...grpc.CallOption) (routeguide.RouteGuide_ListFeaturesClient, error) {
|
||||
_s := []interface{}{_param0, _param1}
|
||||
for _, _x := range _param2 {
|
||||
_s = append(_s, _x)
|
||||
}
|
||||
ret := _m.ctrl.Call(_m, "ListFeatures", _s...)
|
||||
ret0, _ := ret[0].(routeguide.RouteGuide_ListFeaturesClient)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
func (_mr *_MockRouteGuideClientRecorder) ListFeatures(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call {
|
||||
_s := append([]interface{}{arg0, arg1}, arg2...)
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "ListFeatures", _s...)
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuideClient) RecordRoute(_param0 context.Context, _param1 ...grpc.CallOption) (routeguide.RouteGuide_RecordRouteClient, error) {
|
||||
_s := []interface{}{_param0}
|
||||
for _, _x := range _param1 {
|
||||
_s = append(_s, _x)
|
||||
}
|
||||
ret := _m.ctrl.Call(_m, "RecordRoute", _s...)
|
||||
ret0, _ := ret[0].(routeguide.RouteGuide_RecordRouteClient)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
func (_mr *_MockRouteGuideClientRecorder) RecordRoute(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
|
||||
_s := append([]interface{}{arg0}, arg1...)
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "RecordRoute", _s...)
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuideClient) RouteChat(_param0 context.Context, _param1 ...grpc.CallOption) (routeguide.RouteGuide_RouteChatClient, error) {
|
||||
_s := []interface{}{_param0}
|
||||
for _, _x := range _param1 {
|
||||
_s = append(_s, _x)
|
||||
}
|
||||
ret := _m.ctrl.Call(_m, "RouteChat", _s...)
|
||||
ret0, _ := ret[0].(routeguide.RouteGuide_RouteChatClient)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
func (_mr *_MockRouteGuideClientRecorder) RouteChat(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
|
||||
_s := append([]interface{}{arg0}, arg1...)
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "RouteChat", _s...)
|
||||
}
|
||||
|
||||
// Mock of RouteGuide_RouteChatClient interface
|
||||
type MockRouteGuide_RouteChatClient struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *_MockRouteGuide_RouteChatClientRecorder
|
||||
}
|
||||
|
||||
// Recorder for MockRouteGuide_RouteChatClient (not exported)
|
||||
type _MockRouteGuide_RouteChatClientRecorder struct {
|
||||
mock *MockRouteGuide_RouteChatClient
|
||||
}
|
||||
|
||||
func NewMockRouteGuide_RouteChatClient(ctrl *gomock.Controller) *MockRouteGuide_RouteChatClient {
|
||||
mock := &MockRouteGuide_RouteChatClient{ctrl: ctrl}
|
||||
mock.recorder = &_MockRouteGuide_RouteChatClientRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuide_RouteChatClient) EXPECT() *_MockRouteGuide_RouteChatClientRecorder {
|
||||
return _m.recorder
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuide_RouteChatClient) CloseSend() error {
|
||||
ret := _m.ctrl.Call(_m, "CloseSend")
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockRouteGuide_RouteChatClientRecorder) CloseSend() *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "CloseSend")
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuide_RouteChatClient) Context() context.Context {
|
||||
ret := _m.ctrl.Call(_m, "Context")
|
||||
ret0, _ := ret[0].(context.Context)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockRouteGuide_RouteChatClientRecorder) Context() *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "Context")
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuide_RouteChatClient) Header() (metadata.MD, error) {
|
||||
ret := _m.ctrl.Call(_m, "Header")
|
||||
ret0, _ := ret[0].(metadata.MD)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
func (_mr *_MockRouteGuide_RouteChatClientRecorder) Header() *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "Header")
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuide_RouteChatClient) Recv() (*routeguide.RouteNote, error) {
|
||||
ret := _m.ctrl.Call(_m, "Recv")
|
||||
ret0, _ := ret[0].(*routeguide.RouteNote)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
func (_mr *_MockRouteGuide_RouteChatClientRecorder) Recv() *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "Recv")
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuide_RouteChatClient) RecvMsg(_param0 interface{}) error {
|
||||
ret := _m.ctrl.Call(_m, "RecvMsg", _param0)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockRouteGuide_RouteChatClientRecorder) RecvMsg(arg0 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "RecvMsg", arg0)
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuide_RouteChatClient) Send(_param0 *routeguide.RouteNote) error {
|
||||
ret := _m.ctrl.Call(_m, "Send", _param0)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockRouteGuide_RouteChatClientRecorder) Send(arg0 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "Send", arg0)
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuide_RouteChatClient) SendMsg(_param0 interface{}) error {
|
||||
ret := _m.ctrl.Call(_m, "SendMsg", _param0)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockRouteGuide_RouteChatClientRecorder) SendMsg(arg0 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "SendMsg", arg0)
|
||||
}
|
||||
|
||||
func (_m *MockRouteGuide_RouteChatClient) Trailer() metadata.MD {
|
||||
ret := _m.ctrl.Call(_m, "Trailer")
|
||||
ret0, _ := ret[0].(metadata.MD)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockRouteGuide_RouteChatClientRecorder) Trailer() *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "Trailer")
|
||||
}
|
|
@ -1,92 +0,0 @@
|
|||
/*
|
||||
*
|
||||
* Copyright 2017 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package mock_routeguide_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/golang/protobuf/proto"
|
||||
rgmock "google.golang.org/grpc/examples/route_guide/mock_routeguide"
|
||||
rgpb "google.golang.org/grpc/examples/route_guide/routeguide"
|
||||
"google.golang.org/grpc/internal/grpctest"
|
||||
)
|
||||
|
||||
type s struct {
|
||||
grpctest.Tester
|
||||
}
|
||||
|
||||
func Test(t *testing.T) {
|
||||
grpctest.RunSubTests(t, s{})
|
||||
}
|
||||
|
||||
var msg = &rgpb.RouteNote{
|
||||
Location: &rgpb.Point{Latitude: 17, Longitude: 29},
|
||||
Message: "Taxi-cab",
|
||||
}
|
||||
|
||||
func (s) TestRouteChat(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
// Create mock for the stream returned by RouteChat
|
||||
stream := rgmock.NewMockRouteGuide_RouteChatClient(ctrl)
|
||||
// set expectation on sending.
|
||||
stream.EXPECT().Send(
|
||||
gomock.Any(),
|
||||
).Return(nil)
|
||||
// Set expectation on receiving.
|
||||
stream.EXPECT().Recv().Return(msg, nil)
|
||||
stream.EXPECT().CloseSend().Return(nil)
|
||||
// Create mock for the client interface.
|
||||
rgclient := rgmock.NewMockRouteGuideClient(ctrl)
|
||||
// Set expectation on RouteChat
|
||||
rgclient.EXPECT().RouteChat(
|
||||
gomock.Any(),
|
||||
).Return(stream, nil)
|
||||
if err := testRouteChat(rgclient); err != nil {
|
||||
t.Fatalf("Test failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func testRouteChat(client rgpb.RouteGuideClient) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
stream, err := client.RouteChat(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := stream.Send(msg); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := stream.CloseSend(); err != nil {
|
||||
return err
|
||||
}
|
||||
got, err := stream.Recv()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !proto.Equal(got, msg) {
|
||||
return fmt.Errorf("stream.Recv() = %v, want %v", got, msg)
|
||||
}
|
||||
return nil
|
||||
}
|
4
vet.sh
4
vet.sh
|
@ -95,8 +95,8 @@ go list -f {{.Dir}} ./... | xargs go run test/go_vet/vet.go
|
|||
|
||||
# - gofmt, goimports, golint (with exceptions for generated code), go vet.
|
||||
gofmt -s -d -l . 2>&1 | fail_on_output
|
||||
goimports -l . 2>&1 | not grep -vE "(_mock|\.pb)\.go"
|
||||
golint ./... 2>&1 | not grep -vE "(_mock|\.pb)\.go:"
|
||||
goimports -l . 2>&1 | not grep -vE "\.pb\.go"
|
||||
golint ./... 2>&1 | not grep -vE "\.pb\.go:"
|
||||
go vet -all ./...
|
||||
|
||||
misspell -error .
|
||||
|
|
Загрузка…
Ссылка в новой задаче