mqrpc
Version:
💫 Easy RPC over RabbitMQ
69 lines (68 loc) • 3.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ava_1 = require("ava");
const sinon = require("sinon");
const channel_model_1 = require("amqplib/lib/channel_model");
const _utils_1 = require("../../_utils");
const comms_1 = require("../../../lib/RpcServer/comms");
const fakeMessage = { properties: { replyTo: 1234, correlationId: '12345' } };
const expectMessage = (mock, type, times = 1) => {
mock.expects('publish')
.exactly(times)
.withExactArgs('', 1234, new Buffer(JSON.stringify({ type })), { correlationId: '12345', deliveryMode: undefined })
.resolves();
};
ava_1.default.beforeEach(t => {
t.context.sandbox = sinon.sandbox.create();
t.context.fakeChannel = new channel_model_1.Channel({});
t.context.channelMock = sinon.mock(t.context.fakeChannel);
});
ava_1.default.afterEach.always(t => t.context.sandbox.restore());
ava_1.default('[unit] #whileSendingHeartbeats sends nothing when called, returns a function', t => {
t.context.channelMock.expects('publish').never();
const res = comms_1.whileSendingHeartbeats(t.context.fakeChannel, fakeMessage, {});
t.true(res instanceof Function);
t.context.channelMock.verify();
});
ava_1.default('[unit] #whileSendingHeartbeats sends an `ack` message when return function is called', async (t) => {
expectMessage(t.context.channelMock, 'ack');
await comms_1.whileSendingHeartbeats(t.context.fakeChannel, fakeMessage, {})(async () => { });
t.context.channelMock.verify();
t.pass();
});
ava_1.default('[unit] #whileSendingHeartbeats resolves to whatever the inner function resolves', async (t) => {
expectMessage(t.context.channelMock, 'ack');
t.is(await comms_1.whileSendingHeartbeats(t.context.fakeChannel, fakeMessage, {})(async () => 42), 42);
t.context.channelMock.verify();
});
ava_1.default('[unit] #whileSendingHeartbeats does not send `wait` messages if idleTimeout is not set', async (t) => {
expectMessage(t.context.channelMock, 'ack');
await comms_1.whileSendingHeartbeats(t.context.fakeChannel, fakeMessage, {})(async () => { });
await _utils_1.delay(50);
t.context.channelMock.verify();
t.pass();
});
ava_1.default('[unit] #whileSendingHeartbeats sends `wait` messages if idleTimeout is set', async (t) => {
expectMessage(t.context.channelMock, 'ack');
expectMessage(t.context.channelMock, 'wait', 2);
const fn = () => _utils_1.delay(140);
await comms_1.whileSendingHeartbeats(t.context.fakeChannel, fakeMessage, { idleTimeout: 150 })(fn);
t.context.channelMock.verify();
t.pass();
});
ava_1.default('[unit] #whileSendingHeartbeats stops sending `wait` messages after function returns', async (t) => {
expectMessage(t.context.channelMock, 'ack');
expectMessage(t.context.channelMock, 'wait', 2);
const fn = () => _utils_1.delay(140);
await comms_1.whileSendingHeartbeats(t.context.fakeChannel, fakeMessage, { idleTimeout: 150 })(fn);
await _utils_1.delay(50);
t.context.channelMock.verify();
t.pass();
});
ava_1.default('[unit] #whileSendingHeartbeats does not send `wait` messages if function returns quickly', async (t) => {
expectMessage(t.context.channelMock, 'ack');
const fn = () => _utils_1.delay(1);
await comms_1.whileSendingHeartbeats(t.context.fakeChannel, fakeMessage, { idleTimeout: 30 })(fn);
t.context.channelMock.verify();
t.pass();
});