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.
 
 
 

78 lines
2.2 KiB

  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package pubsub
  15. import (
  16. "testing"
  17. gax "github.com/googleapis/gax-go"
  18. "golang.org/x/net/context"
  19. pb "google.golang.org/genproto/googleapis/pubsub/v1"
  20. "google.golang.org/grpc/codes"
  21. "google.golang.org/grpc/status"
  22. )
  23. func TestPullStreamGet(t *testing.T) {
  24. // Test that we retry on the initial Send call from pullstream.get. We don't do this
  25. // test with the server in fake_test.go because there's no clear way to get Send
  26. // to fail from the server.
  27. for _, test := range []struct {
  28. desc string
  29. errors []error
  30. wantCode codes.Code
  31. }{
  32. {
  33. desc: "nil error",
  34. errors: []error{nil},
  35. wantCode: codes.OK,
  36. },
  37. {
  38. desc: "non-retryable error",
  39. errors: []error{status.Errorf(codes.InvalidArgument, "")},
  40. wantCode: codes.InvalidArgument,
  41. },
  42. {
  43. desc: "retryable errors",
  44. errors: []error{
  45. status.Errorf(codes.Unavailable, "first"),
  46. status.Errorf(codes.Unavailable, "second"),
  47. nil,
  48. },
  49. wantCode: codes.OK,
  50. },
  51. } {
  52. streamingPull := func(context.Context, ...gax.CallOption) (pb.Subscriber_StreamingPullClient, error) {
  53. if len(test.errors) == 0 {
  54. panic("out of errors")
  55. }
  56. err := test.errors[0]
  57. test.errors = test.errors[1:]
  58. return &testStreamingPullClient{sendError: err}, nil
  59. }
  60. ps := newPullStream(context.Background(), streamingPull, "", 0)
  61. _, err := ps.get(nil)
  62. if got := status.Code(err); got != test.wantCode {
  63. t.Errorf("%s: got %s, want %s", test.desc, got, test.wantCode)
  64. }
  65. }
  66. }
  67. type testStreamingPullClient struct {
  68. pb.Subscriber_StreamingPullClient
  69. sendError error
  70. }
  71. func (c *testStreamingPullClient) Send(*pb.StreamingPullRequest) error { return c.sendError }