UNPKG

mqrpc

Version:

💫 Easy RPC over RabbitMQ

64 lines (63 loc) • 3.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ava_1 = require("ava"); const sinon = require("sinon"); const _config_1 = require("../_config"); const RpcServer_1 = require("../../lib/RpcServer"); const AmqpClient_1 = require("../../lib/AmqpClient"); const amqpClient = new AmqpClient_1.default({ amqpUrl: _config_1.AMQP_URL }); ava_1.default.before(() => amqpClient.init()); ava_1.default.after(() => amqpClient.term()); ava_1.default.beforeEach(t => t.context.sandbox = sinon.sandbox.create()); ava_1.default.afterEach(async (t) => { t.context.sandbox.restore(); if (t.context.server) await t.context.server.term(); await Promise.all([ amqpClient.channel.deleteQueue('mqrpc.call'), amqpClient.channel.deleteQueue('conversio.mqrpc.call'), amqpClient.channel.deleteExchange('mqrpc'), amqpClient.channel.deleteExchange('conversio.mqrpc') ]); }); ava_1.default.serial('[unit] #init instances an AmqpClient', async (t) => { t.context.server = new RpcServer_1.default({ amqpClient: { amqpUrl: _config_1.AMQP_URL } }); await t.context.server.init(); t.true(t.context.server.amqpClient instanceof AmqpClient_1.default); }); ava_1.default.serial('[unit] #init asserts the client\'s exchange', async (t) => { t.context.server = new RpcServer_1.default({ amqpClient: { amqpUrl: _config_1.AMQP_URL } }); await t.context.server.init(); t.notThrows(() => amqpClient.channel.checkExchange(t.context.server.rpcExchangeName)); }); ava_1.default.serial('[unit] #init asserts the call queue and binds it to the exchange', async (t) => { const channel = await amqpClient.connection.createChannel(); const bindSpy = t.context.sandbox.spy(channel, 'bindQueue'); // There's no way to check if the queue is bound t.context.sandbox.stub(amqpClient.connection, 'createChannel').resolves(channel); t.context.server = new RpcServer_1.default({ amqpClient: { connection: amqpClient.connection } }); await t.context.server.init(); t.notThrows(() => amqpClient.channel.checkQueue(t.context.server.rpcExchangeName + '.call')); sinon.assert.calledWith(bindSpy, 'mqrpc.call', 'mqrpc', 'call'); }); ava_1.default.serial('[unit] #init changes queue & exchange namespaces as configured', async (t) => { const channel = await amqpClient.connection.createChannel(); const bindSpy = t.context.sandbox.spy(channel, 'bindQueue'); // There's no way to check if the queue is bound t.context.sandbox.stub(amqpClient.connection, 'createChannel').resolves(channel); t.context.server = new RpcServer_1.default({ amqpClient: { connection: amqpClient.connection }, rpcServer: { rpcExchangeName: 'conversio.mqrpc' } }); await t.context.server.init(); t.notThrows(() => amqpClient.channel.checkExchange('conversio.mqrpc')); t.notThrows(() => amqpClient.channel.checkQueue('conversio.mqrpc.call')); sinon.assert.calledWith(bindSpy, 'conversio.mqrpc.call', 'conversio.mqrpc', 'call'); }); ava_1.default.serial('[unit] #init starts listening for calls', async (t) => { const channel = await amqpClient.connection.createChannel(); const consumeSpy = t.context.sandbox.spy(channel, 'consume'); t.context.sandbox.stub(amqpClient.connection, 'createChannel').resolves(channel); t.context.server = new RpcServer_1.default({ amqpClient: { connection: amqpClient.connection } }); await t.context.server.init(); sinon.assert.calledWith(consumeSpy, 'mqrpc.call', sinon.match.func); t.pass(); });