Skip to content

Commit 9d069fb

Browse files
committed
retry test + sleep mock
1 parent 8e16b6e commit 9d069fb

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"eslint-plugin-node": "^11.1.0",
9898
"eslint-plugin-unused-imports": "^2.0.0",
9999
"jest": "^29.5.0",
100+
"nock": "^13.3.3",
100101
"prettier": "^3.0.0",
101102
"ts-jest": "^29.1.0",
102103
"ts-node": "^10.9.1",

tests/api/retry.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import nock from "nock";
2+
import { IsomorphicFetchHttpLibrary } from "../../packages/datadog-api-client-common/http/isomorphic-fetch";
3+
import {
4+
RequestContext,
5+
HttpMethod,
6+
} from "../../packages/datadog-api-client-common/";
7+
8+
9+
describe("IsomorphicFetchHttpLibrary Retry Test", () => {
10+
11+
const fakeRequestContext = new RequestContext("https://retry.test.com",HttpMethod.GET);
12+
13+
beforeAll(() => {
14+
nock.disableNetConnect(); // Prevent actual network requests
15+
});
16+
17+
afterAll(() => {
18+
nock.cleanAll();
19+
nock.enableNetConnect(); // Re-enable network connections
20+
});
21+
22+
it("should retry based on response code and retry settings", async () => {
23+
// Set up your mock responses
24+
nock("https://retry.test.com")
25+
.get("/")
26+
.reply(429, {}, { 'x-ratelimit-reset': '1' })
27+
28+
.get("/")
29+
.reply(429, {}, { 'x-ratelimit-reset': '1' })
30+
31+
.get("/")
32+
.reply(429, {}, { 'x-ratelimit-reset': '1' })
33+
34+
.get("/")
35+
.reply(200, { data: "success" }); // Simulate successful response
36+
37+
const httpLibrary = new IsomorphicFetchHttpLibrary();
38+
httpLibrary['sleep'] = jest.fn(() => Promise.resolve());
39+
40+
httpLibrary.enableRetry = true;
41+
httpLibrary.maxRetries = 3;
42+
httpLibrary.backoffBase = 2;
43+
httpLibrary.backoffMultiplier = 2;
44+
45+
const response = await httpLibrary.send(fakeRequestContext);
46+
47+
expect(response.httpStatusCode).toBe(200);
48+
49+
expect(nock.isDone()).toBe(true); // Ensure all expected requests were made
50+
});
51+
});

0 commit comments

Comments
 (0)