-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: work on retry UT & add http retry policy
- Loading branch information
Showing
6 changed files
with
95 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* @see https://fr.wikipedia.org/wiki/Liste_des_codes_HTTP | ||
*/ | ||
const kDefaultCodes = new Set([307, 408, 429, 444, 500, 503, 504, 520, 521, 522, 523, 524]); | ||
|
||
export function httpcode(codes: Set<number> = kDefaultCodes, useDefault = false) { | ||
if (useDefault) { | ||
[...kDefaultCodes].forEach((code) => codes.add(code)); | ||
} | ||
|
||
return ({ statusCode }) => !codes.has(statusCode); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./none"; | ||
export * from "./httpcode"; | ||
|
||
export type PolicyCallback = (error?: any) => boolean; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
jest.setTimeout(15000); | ||
jest.setTimeout(20000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Import Internal Dependencies | ||
import { retry } from "../src/index"; | ||
|
||
describe("retry (with default policy)", () => { | ||
it("should throw an Error because the number of retries has been exceeded", async() => { | ||
expect.assertions(1); | ||
|
||
try { | ||
await retry(() => { | ||
throw new Error("exceed"); | ||
}); | ||
} | ||
catch (error) { | ||
expect(error.message).toStrictEqual("Exceeded the maximum number of allowed retries!"); | ||
} | ||
}); | ||
|
||
it("should succeed after one try", async() => { | ||
let count = 0; | ||
|
||
const { data, metrics } = await retry<string>(() => { | ||
count++; | ||
if (count === 1) { | ||
throw new Error("oops"); | ||
} | ||
|
||
return "hello world!"; | ||
}); | ||
|
||
expect(data).toStrictEqual("hello world!"); | ||
expect(metrics.attempt).toStrictEqual(1); | ||
expect(typeof metrics.elapsedTimeoutTime).toStrictEqual("number"); | ||
expect(typeof metrics.executionTimestamp).toStrictEqual("number"); | ||
}); | ||
|
||
it("should be stopped with Node.js AbortController", async() => { | ||
expect.assertions(1); | ||
|
||
let count = 0; | ||
const controller = new AbortController(); | ||
|
||
try { | ||
await retry(() => { | ||
count++; | ||
if (count <= 2) { | ||
throw new Error("oops"); | ||
} | ||
controller.abort(); | ||
|
||
throw new Error("oops"); | ||
}, { forever: true, signal: controller.signal }); | ||
} | ||
catch (error) { | ||
expect(error.message).toStrictEqual("Aborted"); | ||
} | ||
}); | ||
}); |