35 行
854 B
TypeScript
35 行
854 B
TypeScript
|
|
import assert from 'node:assert/strict'
|
||
|
|
import test from 'node:test'
|
||
|
|
|
||
|
|
import { retryWithBackoff } from '../src/retry'
|
||
|
|
|
||
|
|
test('retryWithBackoff succeeds within the bounded three-attempt policy', async () => {
|
||
|
|
let attempts = 0
|
||
|
|
const value = await retryWithBackoff(
|
||
|
|
async () => {
|
||
|
|
attempts += 1
|
||
|
|
if (attempts < 3) throw new Error('temporary')
|
||
|
|
return 'ok'
|
||
|
|
},
|
||
|
|
{ baseDelayMs: 0, jitterRatio: 0 },
|
||
|
|
)
|
||
|
|
assert.equal(value, 'ok')
|
||
|
|
assert.equal(attempts, 3)
|
||
|
|
})
|
||
|
|
|
||
|
|
test('retryWithBackoff stops immediately for non-retryable errors', async () => {
|
||
|
|
let attempts = 0
|
||
|
|
await assert.rejects(
|
||
|
|
() =>
|
||
|
|
retryWithBackoff(
|
||
|
|
async () => {
|
||
|
|
attempts += 1
|
||
|
|
throw new Error('disabled')
|
||
|
|
},
|
||
|
|
{ baseDelayMs: 0, shouldRetry: () => false },
|
||
|
|
),
|
||
|
|
/disabled/,
|
||
|
|
)
|
||
|
|
assert.equal(attempts, 1)
|
||
|
|
})
|