mqrpc
Version:
💫 Easy RPC over RabbitMQ
47 lines (46 loc) • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ava_1 = require("ava");
const sinon = require("sinon");
const _utils_1 = require("../_utils");
const _config_1 = require("../_config");
const RpcClient_1 = require("../../lib/RpcClient");
const Timer_1 = require("../../lib/Timer");
ava_1.default.beforeEach(async (t) => {
t.context.client = new RpcClient_1.default({
amqpClient: { amqpUrl: _config_1.AMQP_URL },
rpcClient: { callTimeout: 25 }
});
await t.context.client.init();
t.context.sandbox = sinon.sandbox.create();
t.context.publishStub = t.context.sandbox.stub(t.context.client.amqpClient.channel, 'publish').resolves();
});
ava_1.default.afterEach(t => {
t.context.sandbox.restore();
return t.context.client.term();
});
// Most of the tests for this function rely on RabbitMQ and a responding server,
// so they're mostly integration tests.
//
// @see test/clientServerInteraction.ts
ava_1.default('[unit] #call publishes the procedure call', async (t) => {
t.context.client.call('marco', 'polo', 42).catch(err => { });
await _utils_1.delay(1); // let the publish happen
const timeouts = { callTimeout: 25 };
const payload = JSON.stringify({ procedure: 'marco', args: ['polo', 42], timeouts });
sinon.assert.calledWith(t.context.publishStub, 'mqrpc', 'call', sinon.match(buffer => buffer.toString() === payload), { persistent: false, replyTo: 'amq.rabbitmq.reply-to', correlationId: sinon.match.string });
t.pass();
});
ava_1.default('[unit] #call rejects on an expired timeout', async (t) => {
const promise = t.context.client.call('marco');
await _utils_1.delay(25);
await t.throws(promise, Timer_1.TimeoutExpired, 'callTimeout expired after 25ms');
});
ava_1.default('[unit] #call clears all call timeouts on reject', async (t) => {
const spy = t.context.sandbox.spy(t.context.client.callTimer, 'addTimeouts');
t.context.client.call('marco').catch(err => { });
await _utils_1.delay(30);
sinon.assert.calledOnce(spy);
sinon.assert.calledWith(spy, t.context.publishStub.getCall(0).args[3].correlationId);
t.pass();
});