Files
wekan/tests/syncGuardWindow.test.cjs
T
Lauri Ojansivu f06ecdee09 Share guard results within one check, and make rule archive a durable Sync action.
Stored Sync stages nest - a rule archive's delivery inside the rule stage,
inside activity delivery, inside the Sync journal - and every layer's guard
checks before and after by calling the guard below it twice. One innermost
check therefore re-ran every outer guard 2^depth times, and one Sync-created
card archived by a rule took 36 s, which is why archive was held back from
durable Sync (1e40e7e06).

reuseWithinEvaluation (server/lib/syncGuardWindow.js) runs each guard at most
once per evaluation: calls made while another guard's check is running share
the results of guards already checked in it. A call from ordinary work -
between stages, after an adapter or a write - starts a new evaluation and
checks everything afresh, so a policy change or a lost lease between stages
still stops the next one. A first attempt with a 100 ms reuse window missed
exactly that and was dropped. Failures never carry over, a guard reached
again through its own check runs fresh instead of waiting on itself, and the
journal's ownership read is left as it was.

With that, archive and unarchive are registered as durable rule adapters
through the stored archive runner, and boards whose rules archive or
unarchive now take durable Sync. The full-app durable Sync test runs an
"archive every created card" rule end to end: 1.1 s for the whole test. The
rule selection test's example of an action without an adapter is now
moveCardToTop. 565 server tests pass.

Thanks to xet7 !
2026-09-30 11:50:04 +03:00

56 lines
2.8 KiB
JavaScript

'use strict';
// Guards of nested stored Sync stages share results within one evaluation
// (server/lib/syncGuardWindow.js). Run: node tests/syncGuardWindow.test.cjs
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { reuseWithinEvaluation } = require('../server/lib/syncGuardWindow');
test('nested before-and-after guards no longer multiply the base check', async () => {
let base = 0;
const layer = inner => reuseWithinEvaluation(async () => { await inner(); await inner(); });
let guard = reuseWithinEvaluation(async () => { base++; });
for (let depth = 0; depth < 8; depth++) guard = layer(guard);
await guard();
assert.equal(base, 1, 'without sharing this is 2^8 = 256 base checks');
});
test('every call from ordinary work checks afresh, so a change between stages is seen (negative)', async () => {
let allowed = true, base = 0;
const lease = reuseWithinEvaluation(async () => { base++; if (!allowed) throw new Error('lease lost'); });
const stage = reuseWithinEvaluation(async () => { await lease(); await lease(); });
await stage();
allowed = false; // what an adapter or another server did in between
await assert.rejects(stage(), /lease lost/);
allowed = true;
await stage();
assert.equal(base, 3, 'one base check per evaluation, none reused across them');
});
test('a failure fails its evaluation and is not carried into the next (negative)', async () => {
let fail = true;
const base = reuseWithinEvaluation(async () => { if (fail) throw new Error('denied'); });
const outer = reuseWithinEvaluation(async () => { await base(); });
await assert.rejects(outer(), /denied/);
fail = false;
await outer();
});
test('a guard reached again through its own check does not wait on itself', async () => {
let depth = 0, inner;
const outer = reuseWithinEvaluation(async () => { if (depth++ < 2) await inner(); });
inner = reuseWithinEvaluation(async () => { await outer(); });
await Promise.race([outer(), new Promise((_, reject) => setTimeout(() => reject(new Error('deadlock')), 500))]);
});
test('the stored stages use it, and the journal ownership read stays fresh', () => {
assert.throws(() => reuseWithinEvaluation(null), /evaluation-invalid/);
const root = path.join(__dirname, '..');
for (const file of ['server/lib/listSyncOperations.js', 'server/notifications/storedRulePlans.js', 'server/lib/syncActivityDelivery.js',
'server/lib/syncEffects.js', 'server/lib/syncRuleArchiveEffects.js', 'server/notifications/storedDelivery.js']) {
assert.match(fs.readFileSync(path.join(root, file), 'utf8'), /reuseWithinEvaluation\(async/, file);
}
assert.doesNotMatch(fs.readFileSync(path.join(root, 'server/lib/syncOperationJournal.js'), 'utf8'), /reuseWithinEvaluation/);
});