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.
 
 
 

151 lines
4.0 KiB

  1. // Copyright 2017 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 gensupport
  15. import (
  16. "context"
  17. "errors"
  18. "io"
  19. "net"
  20. "net/http"
  21. "testing"
  22. )
  23. func TestRetry(t *testing.T) {
  24. testCases := []struct {
  25. desc string
  26. respStatus []int // HTTP status codes returned (length indicates number of calls we expect).
  27. maxRetry int // Max number of calls allowed by the BackoffStrategy.
  28. wantStatus int // StatusCode of returned response.
  29. }{
  30. {
  31. desc: "First call successful",
  32. respStatus: []int{200},
  33. maxRetry: 3,
  34. wantStatus: 200,
  35. },
  36. {
  37. desc: "Retry before success",
  38. respStatus: []int{500, 500, 500, 200},
  39. maxRetry: 3,
  40. wantStatus: 200,
  41. },
  42. {
  43. desc: "Backoff strategy abandons after 3 retries",
  44. respStatus: []int{500, 500, 500, 500},
  45. maxRetry: 3,
  46. wantStatus: 500,
  47. },
  48. {
  49. desc: "Backoff strategy abandons after 2 retries",
  50. respStatus: []int{500, 500, 500},
  51. maxRetry: 2,
  52. wantStatus: 500,
  53. },
  54. }
  55. for _, tt := range testCases {
  56. // Function consumes tt.respStatus
  57. f := func() (*http.Response, error) {
  58. if len(tt.respStatus) == 0 {
  59. return nil, errors.New("too many requests to function")
  60. }
  61. resp := &http.Response{StatusCode: tt.respStatus[0]}
  62. tt.respStatus = tt.respStatus[1:]
  63. return resp, nil
  64. }
  65. backoff := &LimitRetryStrategy{
  66. Max: tt.maxRetry,
  67. Strategy: NoPauseStrategy,
  68. }
  69. resp, err := Retry(context.Background(), f, backoff)
  70. if err != nil {
  71. t.Errorf("%s: Retry returned err %v", tt.desc, err)
  72. }
  73. if got := resp.StatusCode; got != tt.wantStatus {
  74. t.Errorf("%s: Retry returned response with StatusCode=%d; want %d", tt.desc, got, tt.wantStatus)
  75. }
  76. if len(tt.respStatus) != 0 {
  77. t.Errorf("%s: f was not called enough; status codes remaining: %v", tt.desc, tt.respStatus)
  78. }
  79. }
  80. }
  81. type checkCloseReader struct {
  82. closed bool
  83. }
  84. func (c *checkCloseReader) Read(p []byte) (n int, err error) { return 0, io.EOF }
  85. func (c *checkCloseReader) Close() error {
  86. c.closed = true
  87. return nil
  88. }
  89. func TestRetryClosesBody(t *testing.T) {
  90. var i int
  91. responses := []*http.Response{
  92. {StatusCode: 500, Body: &checkCloseReader{}},
  93. {StatusCode: 500, Body: &checkCloseReader{}},
  94. {StatusCode: 200, Body: &checkCloseReader{}},
  95. }
  96. f := func() (*http.Response, error) {
  97. resp := responses[i]
  98. i++
  99. return resp, nil
  100. }
  101. resp, err := Retry(context.Background(), f, NoPauseStrategy)
  102. if err != nil {
  103. t.Fatalf("Retry returned error: %v", err)
  104. }
  105. if resp != responses[2] {
  106. t.Errorf("Retry returned %v; want %v", resp, responses[2])
  107. }
  108. for i, resp := range responses {
  109. want := i != 2 // Only the last response should not be closed.
  110. got := resp.Body.(*checkCloseReader).closed
  111. if got != want {
  112. t.Errorf("response[%d].Body closed = %t, want %t", i, got, want)
  113. }
  114. }
  115. }
  116. func TestShouldRetry(t *testing.T) {
  117. testCases := []struct {
  118. status int
  119. err error
  120. want bool
  121. }{
  122. {status: 200, want: false},
  123. {status: 308, want: false},
  124. {status: 403, want: false},
  125. {status: 429, want: true},
  126. {status: 500, want: true},
  127. {status: 503, want: true},
  128. {status: 600, want: false},
  129. {err: io.EOF, want: false},
  130. {err: errors.New("random badness"), want: false},
  131. {err: io.ErrUnexpectedEOF, want: true},
  132. {err: &net.AddrError{}, want: false}, // Not temporary.
  133. {err: &net.DNSError{IsTimeout: true}, want: true}, // Temporary.
  134. }
  135. for _, tt := range testCases {
  136. if got := shouldRetry(tt.status, tt.err); got != tt.want {
  137. t.Errorf("shouldRetry(%d, %v) = %t; want %t", tt.status, tt.err, got, tt.want)
  138. }
  139. }
  140. }