You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

84 lines
2.1 KiB

  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. package mock_routeguide_test
  19. import (
  20. "context"
  21. "fmt"
  22. "testing"
  23. "time"
  24. "github.com/golang/mock/gomock"
  25. "github.com/golang/protobuf/proto"
  26. rgmock "google.golang.org/grpc/examples/route_guide/mock_routeguide"
  27. rgpb "google.golang.org/grpc/examples/route_guide/routeguide"
  28. )
  29. var msg = &rgpb.RouteNote{
  30. Location: &rgpb.Point{Latitude: 17, Longitude: 29},
  31. Message: "Taxi-cab",
  32. }
  33. func TestRouteChat(t *testing.T) {
  34. ctrl := gomock.NewController(t)
  35. defer ctrl.Finish()
  36. // Create mock for the stream returned by RouteChat
  37. stream := rgmock.NewMockRouteGuide_RouteChatClient(ctrl)
  38. // set expectation on sending.
  39. stream.EXPECT().Send(
  40. gomock.Any(),
  41. ).Return(nil)
  42. // Set expectation on receiving.
  43. stream.EXPECT().Recv().Return(msg, nil)
  44. stream.EXPECT().CloseSend().Return(nil)
  45. // Create mock for the client interface.
  46. rgclient := rgmock.NewMockRouteGuideClient(ctrl)
  47. // Set expectation on RouteChat
  48. rgclient.EXPECT().RouteChat(
  49. gomock.Any(),
  50. ).Return(stream, nil)
  51. if err := testRouteChat(rgclient); err != nil {
  52. t.Fatalf("Test failed: %v", err)
  53. }
  54. }
  55. func testRouteChat(client rgpb.RouteGuideClient) error {
  56. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  57. defer cancel()
  58. stream, err := client.RouteChat(ctx)
  59. if err != nil {
  60. return err
  61. }
  62. if err := stream.Send(msg); err != nil {
  63. return err
  64. }
  65. if err := stream.CloseSend(); err != nil {
  66. return err
  67. }
  68. got, err := stream.Recv()
  69. if err != nil {
  70. return err
  71. }
  72. if !proto.Equal(got, msg) {
  73. return fmt.Errorf("stream.Recv() = %v, want %v", got, msg)
  74. }
  75. return nil
  76. }