OSDN Git Service

new repo
[bytom/vapor.git] / vendor / google.golang.org / grpc / examples / route_guide / mock_routeguide / rg_mock_test.go
1 /*
2  *
3  * Copyright 2017 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18
19 package mock_routeguide_test
20
21 import (
22         "fmt"
23         "testing"
24
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"
30 )
31
32 var msg = &rgpb.RouteNote{
33         Location: &rgpb.Point{Latitude: 17, Longitude: 29},
34         Message:  "Taxi-cab",
35 }
36
37 func TestRouteChat(t *testing.T) {
38         ctrl := gomock.NewController(t)
39         defer ctrl.Finish()
40
41         // Create mock for the stream returned by RouteChat
42         stream := rgmock.NewMockRouteGuide_RouteChatClient(ctrl)
43         // set expectation on sending.
44         stream.EXPECT().Send(
45                 gomock.Any(),
46         ).Return(nil)
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(
54                 gomock.Any(),
55         ).Return(stream, nil)
56         if err := testRouteChat(rgclient); err != nil {
57                 t.Fatalf("Test failed: %v", err)
58         }
59 }
60
61 func testRouteChat(client rgpb.RouteGuideClient) error {
62         stream, err := client.RouteChat(context.Background())
63         if err != nil {
64                 return err
65         }
66         if err := stream.Send(msg); err != nil {
67                 return err
68         }
69         if err := stream.CloseSend(); err != nil {
70                 return err
71         }
72         got, err := stream.Recv()
73         if err != nil {
74                 return err
75         }
76         if !proto.Equal(got, msg) {
77                 return fmt.Errorf("stream.Recv() = %v, want %v", got, msg)
78         }
79         return nil
80 }