core test utils p1
This commit is contained in:
parent
d1ceea73a3
commit
a5d69f35fa
5 changed files with 93 additions and 8 deletions
|
|
@ -1,7 +1,11 @@
|
|||
name: Write Core Utility Test
|
||||
description: Generate unit tests for core utilities
|
||||
---
|
||||
Write jest tests for the provided code. Use best practices. Be clear and consise.
|
||||
Write jest tests for the provided code.
|
||||
Use jest version ^29 (e.g. jest 29.7.0)
|
||||
|
||||
Do NOT write tests for core/test/util/fixtures.ts` or `core/gui/src/components/mainInput/types.d.ts`, only use them as helpers for testing other files.
|
||||
Use best practices. Be clear and consise.
|
||||
Aim for 100% code coverage where reasonable.
|
||||
Multiple tests can be written, split up tests for best clarity and readability.
|
||||
Only use typescript, and if the file/code is not typescript, warn the user.
|
||||
|
|
@ -18,4 +22,4 @@ Existing test fixtures can be found in:
|
|||
- import and use `ideSettingsPromise` for any IdeSettings needs
|
||||
|
||||
// This note is for if using edit mode to encourage it to create new files
|
||||
The tests are to be put in a new file with the same filepath but with a .test.ts extension rather than a .ts extension
|
||||
The tests are to be put in a new file with the same filepath but with a .test.ts extension rather than a .ts extension
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ export default {
|
|||
moduleNameMapper: {
|
||||
"^(\\.{1,2}/.*)\\.js$": "$1",
|
||||
"^uuid$": "uuid", // https://stackoverflow.com/a/73626360
|
||||
'^@azure/(.*)$': '<rootDir>/node_modules/@azure/$1',
|
||||
'^mssql$': '<rootDir>/node_modules/mssql'
|
||||
"^@azure/(.*)$": "<rootDir>/node_modules/@azure/$1",
|
||||
"^mssql$": "<rootDir>/node_modules/mssql",
|
||||
},
|
||||
extensionsToTreatAsEsm: [".ts"],
|
||||
preset: "ts-jest/presets/default-esm",
|
||||
|
|
@ -21,4 +21,5 @@ export default {
|
|||
},
|
||||
globalSetup: "<rootDir>/jest.global-setup.ts",
|
||||
setupFilesAfterEnv: ["<rootDir>/jest.setup-after-env.ts"],
|
||||
maxWorkers: 1 // equivalent to CLI --runInBand
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
"version": "1.0.13",
|
||||
"description": "The Continue Core contains functionality that can be shared across web, VS Code, or Node.js",
|
||||
"scripts": {
|
||||
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand",
|
||||
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
|
||||
"tsc:check": "tsc -p ./ --noEmit",
|
||||
"build:npm": "tsc -p ./tsconfig.npm.json",
|
||||
"lint": "eslint . --ext ts",
|
||||
|
|
|
|||
76
core/util/withExponentialBackoff.test.ts
Normal file
76
core/util/withExponentialBackoff.test.ts
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
// core/util/withExponentialBackoff.test.ts
|
||||
|
||||
import {
|
||||
withExponentialBackoff,
|
||||
RETRY_AFTER_HEADER,
|
||||
} from "./withExponentialBackoff";
|
||||
|
||||
// Helper function to simulate delay
|
||||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
describe("withExponentialBackoff", () => {
|
||||
it("should return result immediately if apiCall is successful", async () => {
|
||||
const mockApiCall = jest.fn().mockResolvedValue("success");
|
||||
|
||||
const result = await withExponentialBackoff(mockApiCall);
|
||||
expect(result).toBe("success");
|
||||
expect(mockApiCall).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("should retry on API error 429 and eventually succeed", async () => {
|
||||
// Mock an API function that returns HTTP 429 twice then succeeds
|
||||
const mockApiCall = jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce({
|
||||
response: { status: 429, headers: new Map() },
|
||||
})
|
||||
.mockRejectedValueOnce({
|
||||
response: { status: 429, headers: new Map() },
|
||||
})
|
||||
.mockResolvedValue("success");
|
||||
|
||||
const result = await withExponentialBackoff(mockApiCall, 3, 0.001); // Use a short delay for testing
|
||||
expect(result).toBe("success");
|
||||
expect(mockApiCall).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it("should respect Retry-After header value for delay", async () => {
|
||||
const mockApiCall = jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce({
|
||||
response: {
|
||||
status: 429,
|
||||
headers: new Map([[RETRY_AFTER_HEADER, "1"]]),
|
||||
},
|
||||
})
|
||||
.mockResolvedValue("success");
|
||||
|
||||
const startTime = Date.now();
|
||||
const result = await withExponentialBackoff(mockApiCall, 2, 1);
|
||||
const endTime = Date.now();
|
||||
|
||||
expect(result).toBe("success");
|
||||
expect(endTime - startTime).toBeGreaterThanOrEqual(1000); // At least 1 second delay
|
||||
expect(mockApiCall).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("should throw error after maxRetries are exhausted", async () => {
|
||||
const mockApiCall = jest.fn().mockRejectedValue(new Error("Network Error"));
|
||||
|
||||
await expect(withExponentialBackoff(mockApiCall, 3, 0.001)).rejects.toThrow(
|
||||
"Network Error",
|
||||
);
|
||||
expect(mockApiCall).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it("should throw the error immediately if it is not rate limit reached error", async () => {
|
||||
const mockApiCall = jest
|
||||
.fn()
|
||||
.mockRejectedValue(new Error("Unexpected Error"));
|
||||
|
||||
await expect(withExponentialBackoff(mockApiCall, 3, 0.001)).rejects.toThrow(
|
||||
"Unexpected Error",
|
||||
);
|
||||
expect(mockApiCall).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
|
@ -2,7 +2,7 @@ interface APIError extends Error {
|
|||
response?: Response;
|
||||
}
|
||||
|
||||
const RETRY_AFTER_HEADER = "Retry-After";
|
||||
export const RETRY_AFTER_HEADER = "Retry-After";
|
||||
|
||||
const withExponentialBackoff = async <T>(
|
||||
apiCall: () => Promise<T>,
|
||||
|
|
@ -18,8 +18,12 @@ const withExponentialBackoff = async <T>(
|
|||
(error as APIError).response?.status === 429 &&
|
||||
attempt < maxRetries - 1
|
||||
) {
|
||||
const retryAfter = (error as APIError).response?.headers.get(RETRY_AFTER_HEADER);
|
||||
const delay = retryAfter ? parseInt(retryAfter, 10) : initialDelaySeconds * 2 ** attempt;
|
||||
const retryAfter = (error as APIError).response?.headers.get(
|
||||
RETRY_AFTER_HEADER,
|
||||
);
|
||||
const delay = retryAfter
|
||||
? parseInt(retryAfter, 10)
|
||||
: initialDelaySeconds * 2 ** attempt;
|
||||
console.log(
|
||||
`Hit rate limit. Retrying in ${delay} seconds (attempt ${
|
||||
attempt + 1
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue