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.
 
 
 

57 lines
1.5 KiB

  1. // Copyright 2014 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 storage
  15. import (
  16. "context"
  17. "errors"
  18. "testing"
  19. "google.golang.org/api/googleapi"
  20. )
  21. func TestInvoke(t *testing.T) {
  22. t.Parallel()
  23. ctx := context.Background()
  24. // Time-based tests are flaky. We just make sure that invoke eventually
  25. // returns with the right error.
  26. for _, test := range []struct {
  27. count int // number of times to return retryable error
  28. retryCode int // error code for retryable error
  29. err error // error to return after count returns of retryCode
  30. }{
  31. {0, 0, nil},
  32. {0, 0, errors.New("foo")},
  33. {1, 429, nil},
  34. {1, 429, errors.New("bar")},
  35. {2, 518, nil},
  36. {2, 599, &googleapi.Error{Code: 428}},
  37. } {
  38. counter := 0
  39. call := func() error {
  40. counter++
  41. if counter <= test.count {
  42. return &googleapi.Error{Code: test.retryCode}
  43. }
  44. return test.err
  45. }
  46. got := runWithRetry(ctx, call)
  47. if got != test.err {
  48. t.Errorf("%v: got %v, want %v", test, got, test.err)
  49. }
  50. }
  51. }