Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,187 @@
|
||||
"use strict";
|
||||
|
||||
// adpanel:login-bcrypt-admission — unit pins for the login-path bcrypt
|
||||
// admission semaphore (utils/bcrypt-admission.js). The finding this module
|
||||
// closes: withTimeout is Promise.race, so a timed-out compare keeps its
|
||||
// libuv thread to completion — an unbounded number of concurrent login
|
||||
// compares saturated the default 4-thread pool and pushed legitimate logins
|
||||
// past their 8 s windows. These tests pin the CONCURRENCY cap (no more than
|
||||
// maxConcurrent fns run at once), the FIFO hand-off (no starvation), the
|
||||
// release-on-anything guarantee (a throwing fn frees its slot), and the
|
||||
// env-int-clamp contract on BCRYPT_LOGIN_MAX_CONCURRENT (floor 1 — the cap
|
||||
// can never be disabled; documented default 2; a ceiling).
|
||||
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
|
||||
const { createBcryptLoginAdmission } = require("./bcrypt-admission");
|
||||
|
||||
// Drain the gate queue one slot turn at a time: releasing a gate resolves
|
||||
// one task, whose run() finally hands the slot to the next waiter, whose fn
|
||||
// pushes the next gate — the setImmediate awaits let each hand-off land
|
||||
// before the next release.
|
||||
async function drainGates(gates) {
|
||||
while (gates.length > 0) {
|
||||
const release = gates.shift();
|
||||
release();
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
}
|
||||
}
|
||||
|
||||
test("the concurrency cap is honored: no more than maxConcurrent tasks run at once", async () => {
|
||||
const admission = createBcryptLoginAdmission({ env: {} });
|
||||
assert.strictEqual(admission.maxConcurrent, 2, "unset env must give the documented default of 2");
|
||||
let concurrent = 0;
|
||||
let maxConcurrentObserved = 0;
|
||||
const gates = [];
|
||||
const resultsPromise = Promise.all(
|
||||
Array.from({ length: 6 }, (_, i) =>
|
||||
admission.run(() => {
|
||||
concurrent += 1;
|
||||
maxConcurrentObserved = Math.max(maxConcurrentObserved, concurrent);
|
||||
return new Promise((resolve) => gates.push(() => { concurrent -= 1; resolve(i); }));
|
||||
})
|
||||
)
|
||||
);
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
assert.strictEqual(gates.length, 2, "only maxConcurrent fns may start before any release");
|
||||
await drainGates(gates);
|
||||
const results = await resultsPromise;
|
||||
assert.deepStrictEqual(results, [0, 1, 2, 3, 4, 5], "every queued task eventually runs and its verdict propagates");
|
||||
assert.ok(maxConcurrentObserved <= 2, `at most 2 tasks may hold a slot at once (observed ${maxConcurrentObserved})`);
|
||||
});
|
||||
|
||||
test("slots hand off FIFO: the earliest waiter goes first and no task starves", async () => {
|
||||
const admission = createBcryptLoginAdmission({ env: { BCRYPT_LOGIN_MAX_CONCURRENT: "2" } });
|
||||
const order = [];
|
||||
const gates = [];
|
||||
const done = Promise.all(
|
||||
Array.from({ length: 5 }, (_, i) =>
|
||||
admission.run(() => new Promise((resolve) => gates.push(() => { order.push(i); resolve(i); })))
|
||||
)
|
||||
);
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
assert.deepStrictEqual(order, [], "nothing may complete before a slot is released");
|
||||
assert.strictEqual(gates.length, 2, "only maxConcurrent fns may start before any release");
|
||||
await drainGates(gates);
|
||||
await done;
|
||||
assert.deepStrictEqual(order, [0, 1, 2, 3, 4], "slots must hand off in FIFO order — no starvation, no reordering");
|
||||
});
|
||||
|
||||
test("run(fn) releases its slot when fn throws or rejects; the verdict/rejection propagates unchanged", async () => {
|
||||
const admission = createBcryptLoginAdmission({ env: { BCRYPT_LOGIN_MAX_CONCURRENT: "1" } });
|
||||
await assert.rejects(() => admission.run(async () => { throw new Error("compare failed"); }), /compare failed/, "fn rejections propagate unchanged");
|
||||
await assert.rejects(() => admission.run(() => { throw new Error("sync boom"); }), /sync boom/, "synchronous throws also release (finally)");
|
||||
// The slot must be free again: a new task runs immediately.
|
||||
let ran = false;
|
||||
const verdict = await admission.run(async () => { ran = true; return "match"; });
|
||||
assert.strictEqual(ran, true, "the freed slot is reusable");
|
||||
assert.strictEqual(verdict, "match", "fn verdicts propagate unchanged");
|
||||
await assert.rejects(() => admission.run("not a function"), TypeError, "run(fn) requires a function");
|
||||
});
|
||||
|
||||
test("acquire() hands out a release function; the slot transfers to exactly one waiter", async () => {
|
||||
const admission = createBcryptLoginAdmission({ env: { BCRYPT_LOGIN_MAX_CONCURRENT: "1" } });
|
||||
const releaseA = await admission.acquire();
|
||||
assert.strictEqual(typeof releaseA, "function", "acquire resolves with a release function");
|
||||
let secondResolved = false;
|
||||
const second = admission.acquire().then((releaseB) => { secondResolved = true; return releaseB; });
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
assert.strictEqual(secondResolved, false, "the second acquire must wait while the slot is held");
|
||||
releaseA();
|
||||
const releaseB = await second;
|
||||
assert.strictEqual(secondResolved, true, "the released slot transfers to the waiter");
|
||||
releaseB();
|
||||
const releaseC = await admission.acquire();
|
||||
assert.strictEqual(typeof releaseC, "function", "the slot is usable again after the hand-off");
|
||||
releaseC();
|
||||
});
|
||||
|
||||
test("expired queued login work never starts when a busy slot becomes free", async () => {
|
||||
const timers = new Set();
|
||||
const admission = createBcryptLoginAdmission({
|
||||
env: { BCRYPT_LOGIN_MAX_CONCURRENT: "1" },
|
||||
setTimeout: (callback, timeoutMs) => {
|
||||
assert.strictEqual(timeoutMs, 8000, "queue expiry must match the login comparison deadline");
|
||||
timers.add(callback);
|
||||
return callback;
|
||||
},
|
||||
clearTimeout: (callback) => timers.delete(callback),
|
||||
});
|
||||
const releaseBusySlot = await admission.acquire();
|
||||
let compareStarted = false;
|
||||
const queued = admission.run(() => {
|
||||
compareStarted = true;
|
||||
return "matched";
|
||||
}).then((result) => ({ result }), (error) => ({ error: error.message }));
|
||||
|
||||
for (const expire of Array.from(timers)) expire();
|
||||
releaseBusySlot();
|
||||
const outcome = await queued;
|
||||
|
||||
assert.strictEqual(compareStarted, false, "a timed-out request must never enqueue bcrypt work later");
|
||||
assert.strictEqual(outcome.error, "bcrypt-admission-wait-timeout");
|
||||
assert.strictEqual(timers.size, 0, "expired waiters must release their timers");
|
||||
assert.strictEqual(await admission.run(() => "fresh login"), "fresh login", "an expired waiter must not retain a slot");
|
||||
});
|
||||
|
||||
test("slot hand-off rejects expired work even before its timer callback is delivered", async () => {
|
||||
const timers = new Set();
|
||||
let clock = 0;
|
||||
const admission = createBcryptLoginAdmission({
|
||||
env: { BCRYPT_LOGIN_MAX_CONCURRENT: "1" },
|
||||
now: () => clock,
|
||||
setTimeout: (callback) => { timers.add(callback); return callback; },
|
||||
clearTimeout: (callback) => timers.delete(callback),
|
||||
});
|
||||
const releaseBusySlot = await admission.acquire();
|
||||
let expiredWorkStarted = false;
|
||||
const expired = admission.run(() => { expiredWorkStarted = true; })
|
||||
.then(() => null, (error) => error.message);
|
||||
clock = 8000;
|
||||
const fresh = admission.run(() => "fresh login");
|
||||
|
||||
releaseBusySlot();
|
||||
|
||||
assert.strictEqual(await expired, "bcrypt-admission-wait-timeout");
|
||||
assert.strictEqual(expiredWorkStarted, false, "a delayed timer must never allow expired work to start");
|
||||
assert.strictEqual(await fresh, "fresh login", "the next live waiter inherits the slot");
|
||||
assert.strictEqual(timers.size, 0, "expired and admitted waiters both clear their timers");
|
||||
});
|
||||
|
||||
test("expiring a waiter never releases the slot held by a running comparison", async () => {
|
||||
const timers = new Set();
|
||||
const admission = createBcryptLoginAdmission({
|
||||
env: { BCRYPT_LOGIN_MAX_CONCURRENT: "1" },
|
||||
setTimeout: (callback) => { timers.add(callback); return callback; },
|
||||
clearTimeout: (callback) => timers.delete(callback),
|
||||
});
|
||||
const releaseBusySlot = await admission.acquire();
|
||||
const expired = admission.run(() => assert.fail("expired work started"))
|
||||
.then(() => null, (error) => error.message);
|
||||
for (const expire of Array.from(timers)) expire();
|
||||
assert.strictEqual(await expired, "bcrypt-admission-wait-timeout");
|
||||
|
||||
let freshWorkStarted = false;
|
||||
const fresh = admission.run(() => { freshWorkStarted = true; return "fresh login"; });
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
assert.strictEqual(freshWorkStarted, false, "the active comparison still owns the only slot");
|
||||
|
||||
releaseBusySlot();
|
||||
assert.strictEqual(await fresh, "fresh login");
|
||||
assert.strictEqual(timers.size, 0);
|
||||
});
|
||||
|
||||
test("BCRYPT_LOGIN_MAX_CONCURRENT resolves through the shared clamped-int helper (floor 1, documented default 2, ceiling 32)", () => {
|
||||
const maxOf = (env) => createBcryptLoginAdmission({ env }).maxConcurrent;
|
||||
assert.strictEqual(maxOf({}), 2, "unset env must give the documented default of 2");
|
||||
assert.strictEqual(maxOf({ BCRYPT_LOGIN_MAX_CONCURRENT: "4" }), 4);
|
||||
// adpanel:env-int-clamp — a parsed "0" or negative is nonsense for a
|
||||
// concurrency cap and clamps to the floor of 1: it can never disable the
|
||||
// control (the fail-closed direction).
|
||||
assert.strictEqual(maxOf({ BCRYPT_LOGIN_MAX_CONCURRENT: "0" }), 1, "zero clamps to the floor of 1 — the cap cannot be disabled");
|
||||
assert.strictEqual(maxOf({ BCRYPT_LOGIN_MAX_CONCURRENT: "-3" }), 1, "negative clamps to the floor of 1");
|
||||
assert.strictEqual(maxOf({ BCRYPT_LOGIN_MAX_CONCURRENT: "junk" }), 2, "garbage falls back to the documented default");
|
||||
assert.strictEqual(maxOf({ BCRYPT_LOGIN_MAX_CONCURRENT: "12.7" }), 12, "decimal truncation (lenient parseInt semantics)");
|
||||
assert.strictEqual(maxOf({ BCRYPT_LOGIN_MAX_CONCURRENT: "99999" }), 32, "above-ceiling values clamp to 32 — a mistyped value cannot re-open the unbounded-concurrency hole");
|
||||
});
|
||||
Reference in new issue
Block a user