|
1 | | -import { HttpRequest } from "@aws-sdk/protocol-http"; |
| 1 | +import { HttpRequest, HttpResponse } from "@aws-sdk/protocol-http"; |
2 | 2 | import { isThrottlingError } from "@aws-sdk/service-error-classification"; |
3 | 3 | import { v4 } from "uuid"; |
4 | 4 |
|
@@ -88,6 +88,9 @@ describe("defaultStrategy", () => { |
88 | 88 | (HttpRequest as unknown as jest.Mock).mockReturnValue({ |
89 | 89 | isInstance: jest.fn().mockReturnValue(false), |
90 | 90 | }); |
| 91 | + (HttpResponse as unknown as jest.Mock).mockReturnValue({ |
| 92 | + isInstance: jest.fn().mockReturnValue(false), |
| 93 | + }); |
91 | 94 | (v4 as jest.Mock).mockReturnValue("42"); |
92 | 95 | }); |
93 | 96 |
|
@@ -220,22 +223,101 @@ describe("defaultStrategy", () => { |
220 | 223 | }); |
221 | 224 | }); |
222 | 225 |
|
223 | | - it("delay value returned", async () => { |
224 | | - jest.spyOn(global, "setTimeout"); |
| 226 | + describe("totalRetryDelay", () => { |
| 227 | + describe("when retry-after is not set", () => { |
| 228 | + it("should be equal to sum of values computed by delayDecider", async () => { |
| 229 | + jest.spyOn(global, "setTimeout"); |
| 230 | + |
| 231 | + const FIRST_DELAY = 100; |
| 232 | + const SECOND_DELAY = 200; |
| 233 | + |
| 234 | + (defaultDelayDecider as jest.Mock).mockReturnValueOnce(FIRST_DELAY).mockReturnValueOnce(SECOND_DELAY); |
| 235 | + |
| 236 | + const maxAttempts = 3; |
| 237 | + const error = await mockFailedOperation(maxAttempts); |
| 238 | + expect(error.$metadata.totalRetryDelay).toEqual(FIRST_DELAY + SECOND_DELAY); |
| 239 | + |
| 240 | + expect(defaultDelayDecider as jest.Mock).toHaveBeenCalledTimes(maxAttempts - 1); |
| 241 | + expect(setTimeout).toHaveBeenCalledTimes(maxAttempts - 1); |
| 242 | + expect((setTimeout as unknown as jest.Mock).mock.calls[0][1]).toBe(FIRST_DELAY); |
| 243 | + expect((setTimeout as unknown as jest.Mock).mock.calls[1][1]).toBe(SECOND_DELAY); |
| 244 | + }); |
| 245 | + }); |
225 | 246 |
|
226 | | - const FIRST_DELAY = 100; |
227 | | - const SECOND_DELAY = 200; |
| 247 | + describe("when retry-after is set", () => { |
| 248 | + const getErrorWithValues = async ( |
| 249 | + delayDeciderInMs: number, |
| 250 | + retryAfter: number | string, |
| 251 | + retryAfterHeaderName?: string |
| 252 | + ) => { |
| 253 | + (defaultDelayDecider as jest.Mock).mockReturnValueOnce(delayDeciderInMs); |
| 254 | + |
| 255 | + const maxAttempts = 2; |
| 256 | + const mockError = new Error(); |
| 257 | + Object.defineProperty(mockError, "$response", { |
| 258 | + value: { |
| 259 | + headers: { [retryAfterHeaderName ? retryAfterHeaderName : "retry-after"]: String(retryAfter) }, |
| 260 | + }, |
| 261 | + }); |
| 262 | + const error = await mockFailedOperation(maxAttempts, { mockError }); |
| 263 | + expect(defaultDelayDecider as jest.Mock).toHaveBeenCalledTimes(maxAttempts - 1); |
| 264 | + expect(setTimeout).toHaveBeenCalledTimes(maxAttempts - 1); |
| 265 | + |
| 266 | + return error; |
| 267 | + }; |
| 268 | + |
| 269 | + beforeEach(() => { |
| 270 | + jest.spyOn(global, "setTimeout"); |
| 271 | + }); |
228 | 272 |
|
229 | | - (defaultDelayDecider as jest.Mock).mockReturnValueOnce(FIRST_DELAY).mockReturnValueOnce(SECOND_DELAY); |
| 273 | + describe("uses retry-after value if it's greater than that from delayDecider", () => { |
| 274 | + beforeEach(() => { |
| 275 | + const { isInstance } = HttpResponse; |
| 276 | + (isInstance as unknown as jest.Mock).mockReturnValueOnce(true); |
| 277 | + }); |
| 278 | + |
| 279 | + describe("when value is in seconds", () => { |
| 280 | + const testWithHeaderName = async (retryAfterHeaderName: string) => { |
| 281 | + const delayDeciderInMs = 2000; |
| 282 | + const retryAfterInSeconds = 3; |
| 283 | + |
| 284 | + const error = await getErrorWithValues(delayDeciderInMs, retryAfterInSeconds, retryAfterHeaderName); |
| 285 | + expect(error.$metadata.totalRetryDelay).toEqual(retryAfterInSeconds * 1000); |
| 286 | + expect((setTimeout as unknown as jest.Mock).mock.calls[0][1]).toBe(retryAfterInSeconds * 1000); |
| 287 | + }; |
| 288 | + |
| 289 | + it("with header in small case", async () => { |
| 290 | + testWithHeaderName("retry-after"); |
| 291 | + }); |
| 292 | + |
| 293 | + it("with header with first letter capital", async () => { |
| 294 | + testWithHeaderName("Retry-After"); |
| 295 | + }); |
| 296 | + }); |
| 297 | + |
| 298 | + it("when value is a Date", async () => { |
| 299 | + const mockDateNow = Date.now(); |
| 300 | + jest.spyOn(Date, "now").mockReturnValue(mockDateNow); |
| 301 | + |
| 302 | + const delayDeciderInMs = 2000; |
| 303 | + const retryAfterInSeconds = 3; |
| 304 | + const retryAfterDate = new Date(mockDateNow + retryAfterInSeconds * 1000); |
| 305 | + |
| 306 | + const error = await getErrorWithValues(delayDeciderInMs, retryAfterDate.toISOString()); |
| 307 | + expect(error.$metadata.totalRetryDelay).toEqual(retryAfterInSeconds * 1000); |
| 308 | + expect((setTimeout as unknown as jest.Mock).mock.calls[0][1]).toBe(retryAfterInSeconds * 1000); |
| 309 | + }); |
| 310 | + }); |
230 | 311 |
|
231 | | - const maxAttempts = 3; |
232 | | - const error = await mockFailedOperation(maxAttempts); |
233 | | - expect(error.$metadata.totalRetryDelay).toEqual(FIRST_DELAY + SECOND_DELAY); |
| 312 | + it("ignores retry-after value if it's smaller than that from delayDecider", async () => { |
| 313 | + const delayDeciderInMs = 3000; |
| 314 | + const retryAfterInSeconds = 2; |
234 | 315 |
|
235 | | - expect(defaultDelayDecider as jest.Mock).toHaveBeenCalledTimes(maxAttempts - 1); |
236 | | - expect(setTimeout).toHaveBeenCalledTimes(maxAttempts - 1); |
237 | | - expect((setTimeout as unknown as jest.Mock).mock.calls[0][1]).toBe(FIRST_DELAY); |
238 | | - expect((setTimeout as unknown as jest.Mock).mock.calls[1][1]).toBe(SECOND_DELAY); |
| 316 | + const error = await getErrorWithValues(delayDeciderInMs, retryAfterInSeconds); |
| 317 | + expect(error.$metadata.totalRetryDelay).toEqual(delayDeciderInMs); |
| 318 | + expect((setTimeout as unknown as jest.Mock).mock.calls[0][1]).toBe(delayDeciderInMs); |
| 319 | + }); |
| 320 | + }); |
239 | 321 | }); |
240 | 322 | }); |
241 | 323 |
|
|
0 commit comments