github.com/aavshr/aws-sdk-go@v1.41.3/aws/client/default_retryer_test.go (about) 1 package client 2 3 import ( 4 "net/http" 5 "testing" 6 "time" 7 8 "github.com/aavshr/aws-sdk-go/aws/request" 9 ) 10 11 func TestRetryThrottleStatusCodes(t *testing.T) { 12 cases := []struct { 13 expectThrottle bool 14 expectRetry bool 15 r request.Request 16 }{ 17 { 18 false, 19 false, 20 request.Request{ 21 HTTPResponse: &http.Response{StatusCode: 200}, 22 }, 23 }, 24 { 25 true, 26 true, 27 request.Request{ 28 HTTPResponse: &http.Response{StatusCode: 429}, 29 }, 30 }, 31 { 32 true, 33 true, 34 request.Request{ 35 HTTPResponse: &http.Response{StatusCode: 502}, 36 }, 37 }, 38 { 39 true, 40 true, 41 request.Request{ 42 HTTPResponse: &http.Response{StatusCode: 503}, 43 }, 44 }, 45 { 46 true, 47 true, 48 request.Request{ 49 HTTPResponse: &http.Response{StatusCode: 504}, 50 }, 51 }, 52 { 53 false, 54 true, 55 request.Request{ 56 HTTPResponse: &http.Response{StatusCode: 500}, 57 }, 58 }, 59 } 60 61 d := DefaultRetryer{NumMaxRetries: 10} 62 for i, c := range cases { 63 throttle := c.r.IsErrorThrottle() 64 retry := d.ShouldRetry(&c.r) 65 66 if e, a := c.expectThrottle, throttle; e != a { 67 t.Errorf("%d: expected %v, but received %v", i, e, a) 68 } 69 70 if e, a := c.expectRetry, retry; e != a { 71 t.Errorf("%d: expected %v, but received %v", i, e, a) 72 } 73 } 74 } 75 76 func TestCanUseRetryAfter(t *testing.T) { 77 cases := []struct { 78 r request.Request 79 e bool 80 }{ 81 { 82 request.Request{ 83 HTTPResponse: &http.Response{StatusCode: 200}, 84 }, 85 false, 86 }, 87 { 88 request.Request{ 89 HTTPResponse: &http.Response{StatusCode: 500}, 90 }, 91 false, 92 }, 93 { 94 request.Request{ 95 HTTPResponse: &http.Response{StatusCode: 429}, 96 }, 97 true, 98 }, 99 { 100 request.Request{ 101 HTTPResponse: &http.Response{StatusCode: 503}, 102 }, 103 true, 104 }, 105 } 106 107 for i, c := range cases { 108 a := canUseRetryAfterHeader(&c.r) 109 if c.e != a { 110 t.Errorf("%d: expected %v, but received %v", i, c.e, a) 111 } 112 } 113 } 114 115 func TestGetRetryDelay(t *testing.T) { 116 cases := []struct { 117 r request.Request 118 e time.Duration 119 equal bool 120 ok bool 121 }{ 122 { 123 request.Request{ 124 HTTPResponse: &http.Response{StatusCode: 429, Header: http.Header{"Retry-After": []string{"3600"}}}, 125 }, 126 3600 * time.Second, 127 true, 128 true, 129 }, 130 { 131 request.Request{ 132 HTTPResponse: &http.Response{StatusCode: 503, Header: http.Header{"Retry-After": []string{"120"}}}, 133 }, 134 120 * time.Second, 135 true, 136 true, 137 }, 138 { 139 request.Request{ 140 HTTPResponse: &http.Response{StatusCode: 503, Header: http.Header{"Retry-After": []string{"120"}}}, 141 }, 142 1 * time.Second, 143 false, 144 true, 145 }, 146 { 147 request.Request{ 148 HTTPResponse: &http.Response{StatusCode: 503, Header: http.Header{"Retry-After": []string{""}}}, 149 }, 150 0 * time.Second, 151 true, 152 false, 153 }, 154 } 155 156 for i, c := range cases { 157 a, ok := getRetryAfterDelay(&c.r) 158 if c.ok != ok { 159 t.Errorf("%d: expected %v, but received %v", i, c.ok, ok) 160 } 161 162 if (c.e != a) == c.equal { 163 t.Errorf("%d: expected %v, but received %v", i, c.e, a) 164 } 165 } 166 } 167 168 func TestRetryDelay(t *testing.T) { 169 d := DefaultRetryer{NumMaxRetries: 100} 170 r := request.Request{} 171 for i := 0; i < 100; i++ { 172 rTemp := r 173 rTemp.HTTPResponse = &http.Response{StatusCode: 500, Header: http.Header{"Retry-After": []string{"299"}}} 174 rTemp.RetryCount = i 175 a := d.RetryRules(&rTemp) 176 if a > 5*time.Minute { 177 t.Errorf("retry delay should never be greater than five minutes, received %s for retrycount %d", a, i) 178 } 179 } 180 181 for i := 0; i < 100; i++ { 182 rTemp := r 183 rTemp.RetryCount = i 184 rTemp.HTTPResponse = &http.Response{StatusCode: 503, Header: http.Header{"Retry-After": []string{""}}} 185 a := d.RetryRules(&rTemp) 186 if a > 5*time.Minute { 187 t.Errorf("retry delay should not be greater than five minutes, received %s for retrycount %d", a, i) 188 } 189 } 190 191 rTemp := r 192 rTemp.RetryCount = 1 193 rTemp.HTTPResponse = &http.Response{StatusCode: 503, Header: http.Header{"Retry-After": []string{"300"}}} 194 a := d.RetryRules(&rTemp) 195 if a < 5*time.Minute { 196 t.Errorf("retry delay should not be less than retry-after duration, received %s for retrycount %d", a, 1) 197 } 198 199 }