mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Current activity delivery already launches the outgoing webhook without awaiting it and isolates both synchronous and asynchronous failures. Add explicit regressions showing a receiver that holds its response yields control immediately, a rejected delivery resolves safely, and the activity hook retains its non-awaited callback wiring. Thanks to n8ores, RyanHecht and xet7 ! Fixes #3575,
65 lines
2.0 KiB
JavaScript
65 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
// Regression coverage for #3575 / #1402. A slow or failing outgoing webhook
|
|
// must never delay the database mutation that emitted its activity.
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const vm = require('vm');
|
|
|
|
const root = path.resolve(__dirname, '..');
|
|
const read = relative => fs.readFileSync(path.join(root, relative), 'utf8');
|
|
const activities = read('server/models/activities.js');
|
|
const guardSource = read('server/lib/webhookGuard.js');
|
|
|
|
const sandbox = {
|
|
module: { exports: {} },
|
|
console: { error() {} },
|
|
TypeError,
|
|
};
|
|
vm.runInNewContext(
|
|
`${guardSource.replace('export async function safeDeliver', 'async function safeDeliver')}\n` +
|
|
'module.exports = { safeDeliver };',
|
|
sandbox,
|
|
);
|
|
const { safeDeliver } = sandbox.module.exports;
|
|
|
|
async function run() {
|
|
const deliveryBlock = activities.slice(
|
|
activities.indexOf(' if (integrations.length > 0)'),
|
|
activities.indexOf('\n});', activities.indexOf(' if (integrations.length > 0)')),
|
|
);
|
|
assert.match(deliveryBlock, /safeDeliver\(/);
|
|
assert.doesNotMatch(deliveryBlock, /await\s+safeDeliver\(/,
|
|
'activity insertion must not await webhook delivery');
|
|
assert.match(deliveryBlock, /Meteor\.call\('outgoingWebhooks'/);
|
|
|
|
let release;
|
|
const hanging = safeDeliver(() => new Promise(resolve => {
|
|
release = resolve;
|
|
}));
|
|
const callerContinued = await Promise.race([
|
|
Promise.resolve('continued'),
|
|
hanging.then(() => 'blocked'),
|
|
]);
|
|
assert.strictEqual(callerContinued, 'continued',
|
|
'starting a hanging delivery must return control to the caller');
|
|
release();
|
|
assert.strictEqual((await hanging).ok, true);
|
|
|
|
const failure = new Error('receiver closed connection');
|
|
const failed = await safeDeliver(async () => {
|
|
throw failure;
|
|
});
|
|
assert.strictEqual(failed.ok, false);
|
|
assert.strictEqual(failed.error, failure);
|
|
|
|
console.log('webhookNonBlockingActivity: 3 tests passed');
|
|
}
|
|
|
|
run().catch(error => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|