35 lines
1.4 KiB
Bash
Executable File
35 lines
1.4 KiB
Bash
Executable File
#!/bin/bash
|
|
set -e
|
|
node -e '
|
|
const assert = require("assert");
|
|
const { retry, timeout, pLimit, sleep } = require("./index.js");
|
|
(async () => {
|
|
// retry: succeeds on the 3rd attempt
|
|
let calls = 0;
|
|
const v = await retry(async () => { calls++; if (calls < 3) throw new Error("fail"); return "ok"; }, { minDelay: 1, jitter: false });
|
|
assert.strictEqual(v, "ok"); assert.strictEqual(calls, 3);
|
|
|
|
// retry: gives up after `attempts`, calling exactly that many times
|
|
let n = 0;
|
|
await assert.rejects(retry(async () => { n++; throw new Error("boom"); }, { attempts: 4, minDelay: 1 }));
|
|
assert.strictEqual(n, 4);
|
|
|
|
// retry: shouldRetry=false → fails fast (1 call)
|
|
let m = 0;
|
|
await assert.rejects(retry(async () => { m++; throw new Error("nope"); }, { attempts: 5, minDelay: 1, shouldRetry: () => false }));
|
|
assert.strictEqual(m, 1);
|
|
|
|
// timeout: fast resolves, slow rejects
|
|
assert.strictEqual(await timeout(Promise.resolve(42), 50), 42);
|
|
await assert.rejects(timeout(sleep(100), 10), /timed out/);
|
|
|
|
// pLimit: never exceeds the concurrency cap
|
|
const limit = pLimit(2);
|
|
let active = 0, peak = 0;
|
|
await Promise.all(Array.from({ length: 6 }, () => limit(async () => { active++; peak = Math.max(peak, active); await sleep(15); active--; })));
|
|
assert.ok(peak <= 2, "peak concurrency " + peak + " exceeded 2");
|
|
|
|
console.log("all tests passed (retry, timeout, pLimit)");
|
|
})().catch((e) => { console.error("TEST FAILED:", e.message); process.exit(1); });
|
|
'
|