dify/sdks/nodejs-client/src/http/retry.test.js
yyh 4d48791f3c
Some checks are pending
autofix.ci / autofix (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions
Main CI Pipeline / Check Changed Files (push) Waiting to run
Main CI Pipeline / API Tests (push) Blocked by required conditions
Main CI Pipeline / Web Tests (push) Blocked by required conditions
Main CI Pipeline / Style Check (push) Waiting to run
Main CI Pipeline / VDB Tests (push) Blocked by required conditions
Main CI Pipeline / DB Migration Test (push) Blocked by required conditions
refactor: nodejs sdk (#30036)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-12-23 23:24:38 +08:00

39 lines
1.2 KiB
JavaScript

import { describe, expect, it } from "vitest";
import { getRetryDelayMs, shouldRetry } from "./retry";
import { NetworkError, RateLimitError, TimeoutError } from "../errors/dify-error";
const withMockedRandom = (value, fn) => {
const original = Math.random;
Math.random = () => value;
try {
fn();
} finally {
Math.random = original;
}
};
describe("retry helpers", () => {
it("getRetryDelayMs honors retry-after header", () => {
expect(getRetryDelayMs(1, 1, 3)).toBe(3000);
});
it("getRetryDelayMs uses exponential backoff with jitter", () => {
withMockedRandom(0, () => {
expect(getRetryDelayMs(1, 1)).toBe(1000);
expect(getRetryDelayMs(2, 1)).toBe(2000);
expect(getRetryDelayMs(3, 1)).toBe(4000);
});
});
it("shouldRetry respects max retries", () => {
expect(shouldRetry(new TimeoutError("timeout"), 3, 3)).toBe(false);
});
it("shouldRetry retries on network, timeout, and rate limit", () => {
expect(shouldRetry(new TimeoutError("timeout"), 0, 3)).toBe(true);
expect(shouldRetry(new NetworkError("network"), 0, 3)).toBe(true);
expect(shouldRetry(new RateLimitError("limit"), 0, 3)).toBe(true);
expect(shouldRetry(new Error("other"), 0, 3)).toBe(false);
});
});