snapshot of https://github.com/ueberdosis/hocuspocus.git @ cf3cfa355b4d3bb61c6b6c80071f71783e32470e (2026-09-15)
Some checks failed
build / lint (22) (push) Failing after 17s
build / test (22) (push) Failing after 8s
build / lint (23) (push) Failing after 2s
build / build (22) (push) Has been skipped
build / build (23) (push) Has been skipped
build / test (23) (push) Failing after 4s
docsearch / docsearch (push) Failing after 1s
Some checks failed
build / lint (22) (push) Failing after 17s
build / test (22) (push) Failing after 8s
build / lint (23) (push) Failing after 2s
build / build (22) (push) Has been skipped
build / build (23) (push) Has been skipped
build / test (23) (push) Failing after 4s
docsearch / docsearch (push) Failing after 1s
This commit is contained in:
31
tests/server/debounce.ts
Normal file
31
tests/server/debounce.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { useDebounce } from "@hocuspocus/server";
|
||||
import test from "ava";
|
||||
import { sleep } from "../utils/index.ts";
|
||||
|
||||
test("maxDebounce does not queue an execution per call while a slow execution is in flight", async (t) => {
|
||||
const { debounce } = useDebounce();
|
||||
|
||||
let executions = 0;
|
||||
const func = async () => {
|
||||
executions += 1;
|
||||
// the first execution is slower than maxDebounce, simulating a
|
||||
// database/S3 latency spike during a store
|
||||
await sleep(executions === 1 ? 500 : 10);
|
||||
};
|
||||
|
||||
// Continuous calls, like a busy collaborative document: each call resets
|
||||
// the debounce timeout, so only the maxDebounce branch triggers executions
|
||||
// (at most one per 200ms).
|
||||
const end = Date.now() + 1_000;
|
||||
while (Date.now() < end) {
|
||||
debounce("doc", func, 50, 200);
|
||||
await sleep(20);
|
||||
}
|
||||
await sleep(600); // let in-flight and queued executions finish
|
||||
|
||||
// ~1s of continuous calls with maxDebounce=200 should execute ~5 times.
|
||||
// If run() suspends on the in-flight execution before deleting the timer
|
||||
// entry, the stale entry keeps satisfying the maxDebounce age check and
|
||||
// every call during the slow execution queues another run (~25 executions).
|
||||
t.true(executions <= 8, `executed ${executions} times, expected ~5`);
|
||||
});
|
||||
Reference in New Issue
Block a user