mqrpc
Version:
💫 Easy RPC over RabbitMQ
50 lines (49 loc) • 2.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ava_1 = require("ava");
const Timer_1 = require("../../lib/Timer");
ava_1.default.beforeEach(t => t.context.timer = new Timer_1.default());
ava_1.default.afterEach(t => t.context.timer.clear());
ava_1.default('[unit] #addTimeouts returns a promise', t => {
t.true(t.context.timer.addTimeouts('an-id', { id: 'ackTo', length: 25 }) instanceof Promise);
});
ava_1.default('[unit] #addTimeouts rejects with an expired timeout', t => {
t.plan(2);
const start = Date.now();
return t.context.timer.addTimeouts('an-id', { id: 'ackTo', length: 25 })
.catch(err => {
t.regex(err.message, /ackTo.*25/);
t.true(Date.now() >= start + 25);
});
});
ava_1.default('[unit] #addTimeouts rejects with the first, when multiple are set', t => {
t.plan(2);
const start = Date.now();
const tFast = { id: 'fastTo', length: 25 };
const tSlow = { id: 'slowTo', length: 50 };
return t.context.timer.addTimeouts('an-id', tSlow, tFast)
.catch(err => {
t.regex(err.message, /fastTo.*25/);
t.true(Date.now() >= start + 25);
});
});
ava_1.default('[unit] #addTimeouts handles multiple entries independently', async (t) => {
const start = Date.now();
const onePromise = t.context.timer.addTimeouts('an-id', { id: 'ackTo', length: 25 });
const twoPromise = t.context.timer.addTimeouts('an-id-too', { id: 'ackTo', length: 25 });
t.not(onePromise, twoPromise);
await t.throws(onePromise);
await t.throws(twoPromise);
t.true(Date.now() >= start + 25);
});
ava_1.default('[unit] #addTimeouts re-uses the same promise for multiple adds on the same entry', async (t) => {
const onePromise = t.context.timer.addTimeouts('an-id', { id: 'ackTo', lenght: 50 });
const twoPromise = t.context.timer.addTimeouts('an-id', { id: 'callTo', lenght: 50 });
t.is(onePromise, twoPromise);
});
ava_1.default('[unit] #addTimeouts throws when the same timeout ID is re-used', t => {
const to = { id: 'ackTo', lenght: 50 };
t.context.timer.addTimeouts('an-id', to);
t.throws(() => t.context.timer.addTimeouts('an-id', to));
t.throws(() => t.context.timer.addTimeouts('an-id-too', to, to));
});