3 * Copyright 2017 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 package mock_routeguide_test
25 "github.com/golang/mock/gomock"
26 "github.com/golang/protobuf/proto"
27 "golang.org/x/net/context"
28 rgmock "google.golang.org/grpc/examples/route_guide/mock_routeguide"
29 rgpb "google.golang.org/grpc/examples/route_guide/routeguide"
32 var msg = &rgpb.RouteNote{
33 Location: &rgpb.Point{Latitude: 17, Longitude: 29},
37 func TestRouteChat(t *testing.T) {
38 ctrl := gomock.NewController(t)
41 // Create mock for the stream returned by RouteChat
42 stream := rgmock.NewMockRouteGuide_RouteChatClient(ctrl)
43 // set expectation on sending.
47 // Set expectation on receiving.
48 stream.EXPECT().Recv().Return(msg, nil)
49 stream.EXPECT().CloseSend().Return(nil)
50 // Create mock for the client interface.
51 rgclient := rgmock.NewMockRouteGuideClient(ctrl)
52 // Set expectation on RouteChat
53 rgclient.EXPECT().RouteChat(
56 if err := testRouteChat(rgclient); err != nil {
57 t.Fatalf("Test failed: %v", err)
61 func testRouteChat(client rgpb.RouteGuideClient) error {
62 stream, err := client.RouteChat(context.Background())
66 if err := stream.Send(msg); err != nil {
69 if err := stream.CloseSend(); err != nil {
72 got, err := stream.Recv()
76 if !proto.Equal(got, msg) {
77 return fmt.Errorf("stream.Recv() = %v, want %v", got, msg)