Files
wekan/tests/exporterBackpressure.test.cjs
T
Lauri Ojansivu 52325228f8 Fix 6 pre-existing unit-test failures (stale source-guards + a cleanup race).
These failed before the ESM conversion and are unrelated to it:

- addBoardTileHeight: the .path-right slice window was 1600 chars but
  js-board-search-input is at offset 2517 after the header grew; widen to 3000.
- listWidthDefaults: isValidListWidth(269) is TRUE (269 >= MIN 200, no upper
  bound); drop 269 from the "invalid widths" list.
- uiDensity: the js-open-board-menu cog moved to sidebar.jade; read that file.
- exporterBackpressure: the leaky-pattern guard matched the pattern quoted in
  an explanatory comment; strip // comments before matching.
- adminReportsPagination: a 6th report section was added; expect 6
  .admin-report-controls, not 5.
- fileTypeCorrection (real fix): streamHeaderToTemp unlinked the partial temp
  file fire-and-forget, racing the async write-stream open, so a caller could
  still see the file after the rejection. Remove it only after the write stream
  has closed, then reject.

Remaining known failure: rulesApiTriggerNormalize (#2674 $in-matching) — left
for a maintainer decision on the intended rule-normalization contract.
2026-07-23 19:16:24 +03:00

117 lines
4.7 KiB
JavaScript

'use strict';
// Regression test for #6480: the board/CSV export writers paused on socket
// backpressure with `res.once('drain', resolve); res.once('error', reject)`.
// The 'drain' listener self-removes when it fires, but the paired 'error'
// listener never does — so every backpressured write leaked one 'error' listener
// on the ServerResponse. A large export (an 11 MB board) accumulated dozens,
// tripping Node's "MaxListenersExceededWarning: 11 error listeners added to
// [ServerResponse]" and holding listeners for the life of the socket.
// writeWithBackpressure() must remove BOTH listeners once either settles.
//
// Run: node tests/exporterBackpressure.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { EventEmitter } = require('events');
let passed = 0;
function test(name, fn) {
const r = fn();
if (r && typeof r.then === 'function') return r.then(() => { passed += 1; console.log(' ok -', name); });
passed += 1;
console.log(' ok -', name);
return Promise.resolve();
}
// Mirrors writeWithBackpressure() in models/exporter.js.
function writeWithBackpressure(res, str) {
return new Promise((resolve, reject) => {
if (res.write(str)) { resolve(); return; }
const cleanup = () => {
res.removeListener('drain', onDrain);
res.removeListener('error', onError);
};
const onDrain = () => { cleanup(); resolve(); };
const onError = err => { cleanup(); reject(err); };
res.once('drain', onDrain);
res.once('error', onError);
});
}
// A fake ServerResponse whose write() reports backpressure (returns false) until
// a 'drain' is emitted — like a full socket buffer.
function fakeRes() {
const res = new EventEmitter();
res.setMaxListeners(10); // Node's default; the leak would trip a warning past it
res.write = () => false; // always backpressured, forcing the drain path
return res;
}
console.log('exporterBackpressure:');
(async () => {
await test('no drain/error listeners remain after a backpressured write drains', async () => {
const res = fakeRes();
const p = writeWithBackpressure(res, 'chunk');
assert.strictEqual(res.listenerCount('drain'), 1);
assert.strictEqual(res.listenerCount('error'), 1);
res.emit('drain');
await p;
assert.strictEqual(res.listenerCount('drain'), 0, 'drain listener removed');
assert.strictEqual(res.listenerCount('error'), 0, 'error listener removed (this was the leak)');
});
await test('listeners never accumulate across many backpressured writes', async () => {
const res = fakeRes();
for (let i = 0; i < 100; i++) {
const p = writeWithBackpressure(res, 'x');
res.emit('drain');
await p;
assert.ok(res.listenerCount('error') <= 1, `error listeners leaked at write ${i}`);
assert.ok(res.listenerCount('drain') <= 1, `drain listeners leaked at write ${i}`);
}
assert.strictEqual(res.listenerCount('error'), 0);
assert.strictEqual(res.listenerCount('drain'), 0);
});
await test('a socket error rejects and still removes both listeners', async () => {
const res = fakeRes();
const p = writeWithBackpressure(res, 'x');
res.emit('error', new Error('socket closed'));
await assert.rejects(p, /socket closed/);
assert.strictEqual(res.listenerCount('drain'), 0);
assert.strictEqual(res.listenerCount('error'), 0);
});
await test('a write that does NOT backpressure registers no listeners at all', async () => {
const res = fakeRes();
res.write = () => true; // buffer has room
await writeWithBackpressure(res, 'x');
assert.strictEqual(res.listenerCount('drain'), 0);
assert.strictEqual(res.listenerCount('error'), 0);
});
// Source guard: the real exporter must use the shared helper and must NOT carry
// the leaky inline `once('drain', resolve); once('error', reject)` pattern.
await test('source: exporter.js uses writeWithBackpressure and drops the leaky pattern', () => {
const src = fs.readFileSync(path.join(__dirname, '..', 'models', 'exporter.js'), 'utf8');
// Strip line comments so the guard matches only real code, not the doc comment that
// quotes the old leaky pattern to explain why the helper replaced it.
const code = src.replace(/\/\/[^\n]*/g, '');
assert.ok(/export function writeWithBackpressure/.test(code), 'helper must exist');
assert.ok(/removeListener\('drain'/.test(code) && /removeListener\('error'/.test(code),
'helper must remove both listeners on settle');
assert.ok(!/once\('drain',\s*resolve\)\s*;\s*res\.once\('error',\s*reject\)/.test(code),
'the leaky inline once(drain)/once(error) pattern must be gone');
// both writers delegate to the helper
assert.ok((src.match(/writeWithBackpressure\(res,\s*str\)/g) || []).length >= 2,
'both the JSON and CSV writers must delegate to the helper');
});
console.log(`\n${passed} passed`);
})();