Lock TTL is now passed in seconds, matching what the core lock RPC methods expect and what the 3.x SDK documents as ttlSeconds. The presta-api timeout test no longer drains the event loop: it drives the unref-ed timeout timer through node:test mock timers instead of real ones. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LpoFvfAbnMmkSJFRc867XA
55 lines
2.2 KiB
JavaScript
55 lines
2.2 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { createJobRuntime } = require('../src/job-runtime');
|
|
|
|
test('lock is released after success and after failure', async () => {
|
|
const released = [];
|
|
const Core = {
|
|
getLock: async name => name,
|
|
extendLockTime: async () => true,
|
|
releaseLock: async id => released.push(id),
|
|
};
|
|
const runtime = createJobRuntime(Core, { error() {}, warn() {} });
|
|
|
|
assert.equal(await runtime.withRenewedLock('ok', 5, async valid => valid() && 7), 7);
|
|
await assert.rejects(() => runtime.withRenewedLock('fail', 5, async () => { throw new Error('boom'); }), /boom/);
|
|
assert.deepEqual(released, ['ok', 'fail']);
|
|
});
|
|
|
|
test('stop waits for tracked work and blocks subsequent runs until start', async () => {
|
|
const runtime = createJobRuntime({}, console);
|
|
let finish;
|
|
const active = runtime.track(() => new Promise(resolve => { finish = resolve; }));
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
const stopping = runtime.stop();
|
|
finish('done');
|
|
await stopping;
|
|
assert.equal(await active, 'done');
|
|
assert.equal(await runtime.track(async () => 'blocked'), null);
|
|
runtime.start();
|
|
assert.equal(await runtime.track(async () => 'running'), 'running');
|
|
});
|
|
|
|
test('lock TTL is forwarded to Core in seconds, without conversion', async t => {
|
|
t.mock.timers.enable({ apis: ['setInterval'] });
|
|
const getLockCalls = [];
|
|
const extendCalls = [];
|
|
const Core = {
|
|
getLock: async (name, ttl) => { getLockCalls.push([name, ttl]); return 'lock-1'; },
|
|
extendLockTime: async (id, ttl) => { extendCalls.push([id, ttl]); return true; },
|
|
releaseLock: async () => true,
|
|
};
|
|
const runtime = createJobRuntime(Core, { error() {}, warn() {} });
|
|
|
|
let finish;
|
|
const run = runtime.withRenewedLock('ttl', 5, () => new Promise(resolve => { finish = resolve; }));
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
// Interwał odnowienia to ttlSeconds * 1000 / 3 = ~1666 ms, więc jeden tick wystarczy.
|
|
t.mock.timers.tick(1700);
|
|
finish('done');
|
|
assert.equal(await run, 'done');
|
|
|
|
assert.deepEqual(getLockCalls, [['ttl', 5]]);
|
|
assert.deepEqual(extendCalls, [['lock-1', 5]]);
|
|
});
|