Formatting

This commit is contained in:
2026-08-06 13:32:16 +00:00
parent f1bdb1c9ed
commit 6306cc3380
3 changed files with 32 additions and 15 deletions
+7 -1
View File
@@ -76,7 +76,7 @@ export class ExponentialBackoff {
* @param options.baseDelay - Delay used as the basis for the first retry. Default: `1_000` ms. * @param options.baseDelay - Delay used as the basis for the first retry. Default: `1_000` ms.
* @param options.growthRate - Multiplier applied to the delay after each attempt. Default: `2`. * @param options.growthRate - Multiplier applied to the delay after each attempt. Default: `2`.
* @param options.jitter - Maximum proportional reduction subtracted from each delay (01). Default: `0.1`. * @param options.jitter - Maximum proportional reduction subtracted from each delay (01). Default: `0.1`.
* *
* @throws An {@link ExponentialBackoffNumberNotFiniteError} if a provided option is not a finite number * @throws An {@link ExponentialBackoffNumberNotFiniteError} if a provided option is not a finite number
* @throws An {@link ExponentialBackoffNumberOutOfBoundsError} if a provided option is out of bounds * @throws An {@link ExponentialBackoffNumberOutOfBoundsError} if a provided option is out of bounds
* @throws An {@link ExponentialBackoffNumberTooSmallError} if a provided option is too small * @throws An {@link ExponentialBackoffNumberTooSmallError} if a provided option is too small
@@ -99,6 +99,12 @@ export class ExponentialBackoff {
* Create a new ExponentialBackoff instance * Create a new ExponentialBackoff instance
* *
* @param config - The configuration for the exponential backoff * @param config - The configuration for the exponential backoff
*
* @throws An {@link ExponentialBackoffNumberNotFiniteError} if a provided option is not a finite number
* @throws An {@link ExponentialBackoffNumberOutOfBoundsError} if a provided option is out of bounds
* @throws An {@link ExponentialBackoffNumberTooSmallError} if a provided option is too small
* @throws An {@link ExponentialBackoffNonIntegerError} if a provided option is not an integer
*
* @returns The ExponentialBackoff instance * @returns The ExponentialBackoff instance
*/ */
public static from(config?: Partial<ExponentialBackoffOptions>): ExponentialBackoff { public static from(config?: Partial<ExponentialBackoffOptions>): ExponentialBackoff {
+5 -5
View File
@@ -8,9 +8,9 @@
* @returns True if the value is within the bounds, false otherwise * @returns True if the value is within the bounds, false otherwise
*/ */
export const isWithinBounds = (value: number, min: number, max: number): boolean => { export const isWithinBounds = (value: number, min: number, max: number): boolean => {
if (value < min || value > max) { if (value < min || value > max) {
return false; return false;
} }
return true; return true;
}; };
+20 -9
View File
@@ -1,6 +1,10 @@
import { expect, test, vi } from 'vitest'; import { expect, test, vi } from 'vitest';
import { ExponentialBackoff } from '../source/exponential-backoff.ts'; import { ExponentialBackoff } from '../source/exponential-backoff.ts';
import { ExponentialBackoffMaxRetriesHitError, ExponentialBackoffNumberNotFiniteError, ExponentialBackoffStoppedRetriesError } from '../source/errors.ts'; import {
ExponentialBackoffMaxRetriesHitError,
ExponentialBackoffNumberNotFiniteError,
ExponentialBackoffStoppedRetriesError,
} from '../source/errors.ts';
/** /**
* A valid options object that satisfies {@link ExponentialBackoff.validateOptions}. * A valid options object that satisfies {@link ExponentialBackoff.validateOptions}.
@@ -589,13 +593,16 @@ const testExponentialBackoffCalculateDelayDoesNotResultInNaN = (): void => {
const largeNumber = 1_000_000_000_000; const largeNumber = 1_000_000_000_000;
// Test the calculateDelay function // Test the calculateDelay function
const result = ExponentialBackoff.calculateDelay({ const result = ExponentialBackoff.calculateDelay(
baseDelay: 10000, {
growthRate: largeNumber, baseDelay: 10000,
jitter: 0, growthRate: largeNumber,
maxDelay: 10_000, jitter: 0,
maxAttempts: largeNumber, maxDelay: 10_000,
}, largeNumber); maxAttempts: largeNumber,
},
largeNumber,
);
// Test to ensure it was bounded to the max delay // Test to ensure it was bounded to the max delay
expect(result).toBe(10_000); expect(result).toBe(10_000);
@@ -615,6 +622,7 @@ const testExponentialBackoffConstructorDoesNotCauseErrorDuringSpread = async ():
// @ts-expect-error - Passing undefined is allowed if the exactOptionalPropertyTypes option is set to false in TS Compiler options. // @ts-expect-error - Passing undefined is allowed if the exactOptionalPropertyTypes option is set to false in TS Compiler options.
expect(() => new ExponentialBackoff(options)).toThrow(ExponentialBackoffNumberNotFiniteError); expect(() => new ExponentialBackoff(options)).toThrow(ExponentialBackoffNumberNotFiniteError);
}; };
const runTests = async (): Promise<void> => { const runTests = async (): Promise<void> => {
test('ExponentialBackoff.run: delegates to a new instance using default options', testExponentialBackoffRunUsesDefaultOptions); test('ExponentialBackoff.run: delegates to a new instance using default options', testExponentialBackoffRunUsesDefaultOptions);
test('ExponentialBackoff.run: retries and succeeds with partial options', testExponentialBackoffRunWithPartialOptions); test('ExponentialBackoff.run: retries and succeeds with partial options', testExponentialBackoffRunWithPartialOptions);
@@ -641,7 +649,10 @@ const runTests = async (): Promise<void> => {
test('ExponentialBackoff.validateOptions: rejects Infinity', testExponentialBackoffValidateOptionsRejectsNonFiniteValues); test('ExponentialBackoff.validateOptions: rejects Infinity', testExponentialBackoffValidateOptionsRejectsNonFiniteValues);
test('ExponentialBackoff.validateOptions: rejects non-integer values', testExponentialBackoffValidateOptionsRejectsNonIntegerValues); test('ExponentialBackoff.validateOptions: rejects non-integer values', testExponentialBackoffValidateOptionsRejectsNonIntegerValues);
test('ExponentialBackoff.validateOptions: rejects NaN', testExponentialBackoffValidateOptionsRejectsNaN); test('ExponentialBackoff.validateOptions: rejects NaN', testExponentialBackoffValidateOptionsRejectsNaN);
test('ExponentialBackoff: calculateDelay does not result in NaN from extremely large growth rates and attempts', testExponentialBackoffCalculateDelayDoesNotResultInNaN); test(
'ExponentialBackoff: calculateDelay does not result in NaN from extremely large growth rates and attempts',
testExponentialBackoffCalculateDelayDoesNotResultInNaN,
);
test('ExponentialBackoff: constructor does not cause an error during spread', testExponentialBackoffConstructorDoesNotCauseErrorDuringSpread); test('ExponentialBackoff: constructor does not cause an error during spread', testExponentialBackoffConstructorDoesNotCauseErrorDuringSpread);
}; };