Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

89 linhas
3.0 KiB

  1. // Copyright 2016, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. package gax
  30. import (
  31. "testing"
  32. "time"
  33. "google.golang.org/grpc/codes"
  34. "google.golang.org/grpc/status"
  35. )
  36. var _ Retryer = &boRetryer{}
  37. func TestBackofDefault(t *testing.T) {
  38. backoff := Backoff{}
  39. max := []time.Duration{1, 2, 4, 8, 16, 30, 30, 30, 30, 30}
  40. for i, m := range max {
  41. max[i] = m * time.Second
  42. }
  43. for i, w := range max {
  44. if d := backoff.Pause(); d > w {
  45. t.Errorf("Backoff duration should be at most %s, got %s", w, d)
  46. } else if i < len(max)-1 && backoff.cur != max[i+1] {
  47. t.Errorf("current envelope is %s, want %s", backoff.cur, max[i+1])
  48. }
  49. }
  50. }
  51. func TestBackoffExponential(t *testing.T) {
  52. backoff := Backoff{Initial: 1, Max: 20, Multiplier: 2}
  53. want := []time.Duration{1, 2, 4, 8, 16, 20, 20, 20, 20, 20}
  54. for _, w := range want {
  55. if d := backoff.Pause(); d > w {
  56. t.Errorf("Backoff duration should be at most %s, got %s", w, d)
  57. }
  58. }
  59. }
  60. func TestOnCodes(t *testing.T) {
  61. // Lint errors grpc.Errorf in 1.6. It mistakenly expects the first arg to Errorf to be a string.
  62. errf := status.Errorf
  63. apiErr := errf(codes.Unavailable, "")
  64. tests := []struct {
  65. c []codes.Code
  66. retry bool
  67. }{
  68. {nil, false},
  69. {[]codes.Code{codes.DeadlineExceeded}, false},
  70. {[]codes.Code{codes.DeadlineExceeded, codes.Unavailable}, true},
  71. {[]codes.Code{codes.Unavailable}, true},
  72. }
  73. for _, tst := range tests {
  74. b := OnCodes(tst.c, Backoff{})
  75. if _, retry := b.Retry(apiErr); retry != tst.retry {
  76. t.Errorf("retriable codes: %v, error: %s, retry: %t, want %t", tst.c, apiErr, retry, tst.retry)
  77. }
  78. }
  79. }