UNPKG

nibi

Version:

Extremely easy management of queues with RabbitMQ and/or AmazonSQS

185 lines (146 loc) 4.4 kB
var exec = require('exec'), AWS = require('aws-sdk'), Nibi = require('../lib'), config = require('../lib/config'), myAwsConfigFile = require('../aws-config.json'), listQueues = require('../lib/listQueues'), queueSuffix = (Math.random() * (60 - 1) + 1).toFixed(0), queuesSystem = "test-SQS", queueName = queuesSystem + "-" + queueSuffix; var myAwsConfigFile = {}; if(config.service === 'rabbit') { queuesSystem = "test-rabbit"; queueName = queuesSystem + "-" + queueSuffix; }; describe('Send from '+ queuesSystem, function(){ this.timeout(10000); var queue = queueName, objectSend = {"huba": "LIN"}, objectReceived = {}; before(function(done){ var nibi = new Nibi({ awsConfigFile: myAwsConfigFile }); nibi.sender(queue, objectSend); var callback = function(message){ objectReceived = message done(); }; nibi.receiver(queue, callback); }); //delete the queue after the test. after(function(done){ if(queuesSystem == "test-SQS"){ AWS.config.update(myAwsConfigFile); var sqs = new AWS.SQS(); listQueues(queueName).then(function(url){ sqs.deleteQueue({ QueueUrl: url }, function(err, data){ if(!err){ done(); }; }); }); }else{ done(); }; }); it('an array', function(done){ if(Array.isArray(objectReceived)){ done(); }; }); it('an array containing a string', function(done){ if(typeof objectReceived[0] === "string"){ done(); }; }); it('an array containing a string with JSON syntax', function(done){ if(typeof JSON.parse(objectReceived[0]) === "object"){ done(); }; }); }); describe('Send from '+ queuesSystem + " using two queues", function(){ this.timeout(10000); var queue2 = queueName + "-2", objectSend2 = {"wild": "BOAR"}, objectReceived2 = {}, queue3 = queueName + "-3", objectSend3 = {"furcifer": "PARDALIS"}, objectReceived3 = {}; before(function(done){ var nibi = new Nibi({ awsConfigFile: myAwsConfigFile }); nibi.sender(queue2, objectSend2); var callback2 = function(message){ objectReceived2 = message done(); }; nibi.receiver(queue2, callback2); }); beforeEach(function(done){ var nibi = new Nibi({ awsConfigFile: myAwsConfigFile }); nibi.sender(queue3, objectSend3); var callback3 = function(message){ objectReceived3 = message done(); }; nibi.receiver(queue3, callback3); }); //delete the queue after the test. after(function(done){ if(queuesSystem == "test-SQS"){ AWS.config.update(myAwsConfigFile); var sqs = new AWS.SQS(); //REFACTOR !!!!!! listQueues(queueName+"-2").then(function(url){ sqs.deleteQueue({ QueueUrl: url }, function(err, data){ }); }); listQueues(queueName+"-3").then(function(url){ sqs.deleteQueue({ QueueUrl: url }, function(err, data){ if(!err){ done(); }; }); }); }else{ done(); }; }); it('messages come to their respective queues', function(done){ if(JSON.stringify(JSON.parse(objectReceived2)) === '{"wild":"BOAR"}' && JSON.stringify(JSON.parse(objectReceived3)) === '{"furcifer":"PARDALIS"}'){ done(); } }); }); describe('Use credentials on rabbitmq', function () { this.timeout(10000) var queue = queueName + ' -4' var objectSend = { huba: 'llin' } var objectReceived = {} before(function (done) { var nibi = new Nibi({ awsConfigFile: myAwsConfigFile, credentials: { username: 'tryharder', password: 'tryhard15' } }) nibi.sender(queue, objectSend) nibi.receiver(queue, function (message) { objectReceived = message done() }) }) it('does not blow up', function (done) { done() }) it('is the same object sent and received', function (done) { var receivedChain = JSON.parse(objectReceived.shift()) var eqProps = Object.keys(objectSend).every(function (key) { return receivedChain.hasOwnProperty(key) }) var eqVal = objectSend.huba === receivedChain.huba if (eqProps && eqVal) done() }) })