From 2452c1521f0eb8728989f9e24bd91ad298af6aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aitor=20Mag=C3=A1n=20Garc=C3=ADa?= Date: Fri, 31 May 2013 12:31:32 +0200 Subject: [PATCH 1/2] Fix #152. Improve code and test --- lib/groupService.js | 35 +++++++++---------- test/unit/groupsTest.js | 76 ++++++++++++++++++++++++++++++----------- 2 files changed, 72 insertions(+), 39 deletions(-) diff --git a/lib/groupService.js b/lib/groupService.js index ab3fb5e..d579a94 100644 --- a/lib/groupService.js +++ b/lib/groupService.js @@ -84,34 +84,31 @@ function getGroup(req, res) { }); } -function getElementsHandler(db) { - return function (name, callback) { - db.smembers(name, callback); - } +function getElementsHandler(name, callback) { + var db = dbCluster.getTransactionDb(name); + db.smembers(name, callback); } function addQueuesFromGroup(req, res, next) { - var db = dbCluster.getTransactionDb(req.params.groupName); if (req.body && req.body.groups) { - async.map(req.body.groups, getElementsHandler(db), + async.map(req.body.groups, getElementsHandler, function(error, results) { - //[[q,q,q],[q],[q,q]] - var processedResults = []; - for (var j = 0; j < results.length; j++) { - for (var i = 0; i < results[j].length; i++) { - processedResults.push({ - id: results[j][i] - }); - } + + if (!req.body.queue) { + req.body.queue = []; + } + + var processedResults = []; + for (var j = 0; j < results.length; j++) { + for (var i = 0; i < results[j].length; i++) { + req.body.queue.push({ + id: results[j][i] + }); } - if (req.body.queue) { - req.body.queue = req.body.queue.concat(processedResults); - } else { - req.body.queue = processedResults; } - next(); + next(); }); } else { next(); diff --git a/test/unit/groupsTest.js b/test/unit/groupsTest.js index d3d0288..9a33a5f 100644 --- a/test/unit/groupsTest.js +++ b/test/unit/groupsTest.js @@ -133,12 +133,37 @@ describe('Groups', function () { describe('When a message is posted to a group', function () { var publish; + var groupName = 'group2'; + var queues = ['A2', 'B2']; + + function checkQueue(transId, queue, done) { + var checkQueue = { + url: 'http://' + HOST + ':' + PORT + '/queue/' + queue + '/pop', + method: 'POST' + } + + request(checkQueue, function (error, response, body) { + response.statusCode.should.equal(200); + + var parsedBody = JSON.parse(body); + should.exist(parsedBody.data); + parsedBody.data.length.should.equal(1); + parsedBody.data.should.include(publish.json.payload); + parsedBody.transactions.length.should.equal(1); + parsedBody.transactions.should.include(transId); + + done(); + }); + } beforeEach(function (done) { var createGroup = { url: 'http://' + HOST + ':' + PORT + '/group', method: 'POST', - json: {} + json: { + name: groupName, + queues: queues + } }; publish = { @@ -148,42 +173,53 @@ describe('Groups', function () { 'payload': 'Published message', 'priority': 'H', 'callback': 'http://foo.bar', - 'queue': [ - ], 'groups': [ 'group2' ] } }; - createGroup.json.name = 'group1'; - createGroup.json.queues = ['A1', 'B1'] request(createGroup, function (error, response, body) { - createGroup.json.name = 'group2'; - createGroup.json.queues = ['A2', 'B2'] - request(createGroup, function (error, response, body) { - done(); - }); + done(); + }); + }); + + it('should publish to all the inboxes associated to the group', function (done) { + request(publish, function (error, response, body) { + + response.statusCode.should.equal(200); + + var transId = body.data; + var tests = []; + + for (var i = 0; i < queues.length; i++) { + tests.push(checkQueue.bind({}, transId, queues[i])); + } + + async.parallel(tests, done); + }); }); + it('should publish to all the inboxes associated to the group', function (done) { + var newQueue = 'C2'; + + publish.json.queue = [ { id: newQueue } ]; request(publish, function (error, response, body) { + response.statusCode.should.equal(200); - var checkQueue = { - url: 'http://' + HOST + ':' + PORT + '/queue/B2/pop', - method: 'POST' + var transId = body.data; + var tests = []; + + tests.push(checkQueue.bind({}, transId, newQueue)); + for (var i = 0; i < queues.length; i++) { + tests.push(checkQueue.bind({}, transId, queues[i])); } - request(checkQueue, function (error, response, body) { - response.statusCode.should.equal(200); + async.parallel(tests, done); - var parsedBody = JSON.parse(body); - should.exist(parsedBody.data); - parsedBody.data.length.should.equal(1); - done(); - }); }); }); }); From aa0e5d22861c76e4a3adbdb1dd0f1e7bd81804a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aitor=20Mag=C3=A1n=20Garc=C3=ADa?= Date: Fri, 31 May 2013 12:33:46 +0200 Subject: [PATCH 2/2] Modify test name --- test/unit/groupsTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/groupsTest.js b/test/unit/groupsTest.js index 9a33a5f..b1de0b5 100644 --- a/test/unit/groupsTest.js +++ b/test/unit/groupsTest.js @@ -202,7 +202,7 @@ describe('Groups', function () { }); - it('should publish to all the inboxes associated to the group', function (done) { + it('should publish to all the inboxes associated to the group and in the selected queues', function (done) { var newQueue = 'C2'; publish.json.queue = [ { id: newQueue } ];