mqrpc
Version:
💫 Easy RPC over RabbitMQ
94 lines (93 loc) • 4.92 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ava_1 = require("ava");
const _utils_1 = require("../_utils");
const RpcClient_1 = require("../../lib/RpcClient");
const promises_1 = require("../../lib/promises");
const Timer_1 = require("../../lib/Timer");
const errors_1 = require("../../lib/RpcClient/errors");
const errors_2 = require("../../lib/RpcServer/errors");
ava_1.default.beforeEach(t => {
t.context.client = new RpcClient_1.default({ amqpClient: { amqpUrl: '' } });
const [promise, callbacks] = promises_1.newPromiseAndCallbacks();
t.context.workingPromise = promise;
t.context.client.calls.set('works', callbacks);
});
ava_1.default('[unit] #makeReplyHandler returns a function', t => {
t.true(t.context.client.makeReplyHandler() instanceof Function);
});
ava_1.default('[unit] #makeReplyHandler does not throw if the call is missing', t => {
t.context.client.makeReplyHandler()({ properties: { correlationId: '1234' } });
t.pass();
});
ava_1.default('[unit] #makeReplyHandler rejects the call promise when reply cannot be parsed', t => {
t.plan(1);
const message = { properties: { correlationId: 'works' }, content: new Buffer('gibberish') };
t.context.client.makeReplyHandler()(message);
return t.context.workingPromise.catch(err => {
t.true(err instanceof errors_1.UnparseableContent);
});
});
ava_1.default('[unit] #makeReplyHandler resolves the call with the reply', async (t) => {
const payload = { type: 'reply', reply: 42 };
const message = { properties: { correlationId: 'works' }, content: new Buffer(JSON.stringify(payload)) };
t.context.client.makeReplyHandler()(message);
t.is(await t.context.workingPromise, 42);
});
ava_1.default('[unit] #makeReplyHandler rejects a call on a received error', async (t) => {
const payload = { type: 'error', error: new errors_2.ProcedureFailed(new TypeError('42')).toObject() };
const message = { properties: { correlationId: 'works' }, content: new Buffer(JSON.stringify(payload)) };
t.context.client.makeReplyHandler()(message);
return t.context.workingPromise.catch(err => {
t.true(err instanceof errors_1.ProcedureFailed);
t.is(err.message, 'Remote procedure failed with error - TypeError: 42');
t.is(err.causeStack, payload.error.cause.stack);
});
});
ava_1.default('[unit] #makeReplyHandler clears the ackTimeout when an `ack` message is received', async (t) => {
const message = { properties: { correlationId: 'works' }, content: new Buffer('{"type":"ack"}') };
t.context.client.callTimer.addTimeouts('works', { id: 'ackTimeout', length: 25 });
t.context.client.makeReplyHandler()(message);
await _utils_1.delay(30);
await t.notThrows(Promise.race([t.context.workingPromise, Promise.resolve(42)]));
});
ava_1.default('[unit] #makeReplyHandler sets the idleTimeout on an `ack` message, if defined', async (t) => {
t.plan(1);
t.context.client.idleTimeout = 25;
const timerPromise = t.context.client.callTimer.addTimeouts('works');
const message = { properties: { correlationId: 'works' }, content: new Buffer('{"type":"ack"}') };
t.context.client.makeReplyHandler()(message);
timerPromise.catch(err => {
t.true(err instanceof Timer_1.TimeoutExpired);
});
await _utils_1.delay(35);
});
ava_1.default('[unit] #makeReplyHandler does not set the idleTimeout on an `ack` message, if not defined', async (t) => {
const timerPromise = t.context.client.callTimer.addTimeouts('works');
const message = { properties: { correlationId: 'works' }, content: new Buffer('{"type":"ack"}') };
t.context.client.makeReplyHandler()(message);
await _utils_1.delay(35);
return t.notThrows(Promise.race([timerPromise, Promise.resolve(42)]));
});
ava_1.default('[unit] #makeReplyHandler restarts the idleTimeout when a `wait` message is received', async (t) => {
t.plan(2);
const timerPromise = t.context.client.callTimer.addTimeouts('works', { id: 'idleTimeout', length: 50 });
const message = { properties: { correlationId: 'works' }, content: new Buffer('{"type":"wait"}') };
await _utils_1.delay(20);
t.context.client.makeReplyHandler()(message);
await _utils_1.delay(35);
await t.notThrows(Promise.race([timerPromise, Promise.resolve(42)]));
timerPromise.catch(err => {
t.true(err instanceof Timer_1.TimeoutExpired);
});
await _utils_1.delay(25);
});
ava_1.default('[unit] #makeReplyHandler rejects the call if an unknown message is received', async (t) => {
t.plan(2);
const message = { properties: { correlationId: 'works' }, content: new Buffer('{"type":"quoi"}') };
t.context.client.makeReplyHandler()(message);
return t.context.workingPromise.catch(err => {
t.true(err instanceof errors_1.UnknownReply);
t.regex(err.message, /Cannot handle reply/);
});
});