From 5c4d2394671cefab923400c43503348c6498b2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 22 Jan 2016 12:43:41 +0100 Subject: [PATCH] Fixed unit test for job queue and moved isJobFound function as private method in job backend --- HOWTO_RELEASE | 2 +- batch/job_backend.js | 11 ++++++----- batch/job_runner.js | 4 +++- test/unit/batch/job_queue.js | 33 +++++++++++++++++++++++++++++++ test/unit/batch/username_queue.js | 33 ------------------------------- 5 files changed, 43 insertions(+), 40 deletions(-) create mode 100644 test/unit/batch/job_queue.js delete mode 100644 test/unit/batch/username_queue.js diff --git a/HOWTO_RELEASE b/HOWTO_RELEASE index 1d65b876..f147f500 100644 --- a/HOWTO_RELEASE +++ b/HOWTO_RELEASE @@ -1,5 +1,5 @@ 1. Test (make clean all check), fix if broken before proceeding -2. Ensure proper version in package.json +2. Ensure proper version in package.json 3. Ensure NEWS section exists for the new version, review it, add release date 4. Drop npm-shrinkwrap.json 5. Run npm shrinkwrap to recreate npm-shrinkwrap.json diff --git a/batch/job_backend.js b/batch/job_backend.js index c661cedf..4e592263 100644 --- a/batch/job_backend.js +++ b/batch/job_backend.js @@ -146,7 +146,12 @@ JobBackend.prototype._getIndexedJob = function (job_id, username, callback) { }); }; +JobBackend.prototype._isJobFound = function (jobValues) { + return jobValues[0] && jobValues[1] && jobValues[2] && jobValues[3] && jobValues[4]; +}; + JobBackend.prototype.get = function (job_id, callback) { + var self = this; var redisParams = [ this.redisPrefix + job_id, 'user', @@ -162,11 +167,7 @@ JobBackend.prototype.get = function (job_id, callback) { return callback(err); } - function isJobFound(jobValues) { - return jobValues[0] && jobValues[1] && jobValues[2] && jobValues[3] && jobValues[4]; - } - - if (!isJobFound(jobValues)) { + if (!self._isJobFound(jobValues)) { var notFoundError = new Error('Job with id ' + job_id + ' not found'); notFoundError.name = 'NotFoundError'; return callback(notFoundError); diff --git a/batch/job_runner.js b/batch/job_runner.js index b8e16262..6692041f 100644 --- a/batch/job_runner.js +++ b/batch/job_runner.js @@ -1,8 +1,10 @@ 'use strict'; +var errorCodes = require('../app/postgresql/error_codes'); var PSQL = require('cartodb-psql'); var QUERY_CANCELED = '57014'; + function JobRunner(jobBackend, userDatabaseMetadataService) { this.jobBackend = jobBackend; this.userDatabaseMetadataService = userDatabaseMetadataService; @@ -58,7 +60,7 @@ JobRunner.prototype._query = function (job, userDatabaseMetadata, callback) { query.on('error', function (err) { // if query has been cancelled then it's going to get the current job status saved by query_canceller - if (err.code === QUERY_CANCELED) { + if (errorCodes[err.code] === 'query_canceled') { return self.jobBackend.get(job.job_id, callback); } diff --git a/test/unit/batch/job_queue.js b/test/unit/batch/job_queue.js new file mode 100644 index 00000000..e9b9e836 --- /dev/null +++ b/test/unit/batch/job_queue.js @@ -0,0 +1,33 @@ +var JobQueue = require('../../../batch/job_queue'); +var assert = require('assert'); + +describe('batch API job queue', function () { + beforeEach(function () { + this.metadataBackend = { + redisCmd: function () { + var callback = arguments[arguments.length -1]; + process.nextTick(function () { + callback(null, 'irrelevantJob'); + }); + } + }; + this.jobQueue = new JobQueue(this.metadataBackend); + }); + + it('.enqueue(username) should enqueue the provided job', function (done) { + this.jobQueue.enqueue('irrelevantJob', 'irrelevantHost', function (err, username) { + assert.ok(!err); + assert.equal(username, 'irrelevantJob'); + done(); + }); + }); + + it('.dequeue(username) should dequeue the next job', function (done) { + this.jobQueue.dequeue('irrelevantHost', function (err, username) { + assert.ok(!err); + assert.equal(username, 'irrelevantJob'); + done(); + }); + }); + +}); diff --git a/test/unit/batch/username_queue.js b/test/unit/batch/username_queue.js deleted file mode 100644 index 3a856764..00000000 --- a/test/unit/batch/username_queue.js +++ /dev/null @@ -1,33 +0,0 @@ -var UsernameQueue = require('../../../batch/username_queue'); -var assert = require('assert'); - -describe('batch API username queue', function () { - beforeEach(function () { - this.metadataBackend = { - redisCmd: function () { - var callback = arguments[arguments.length -1]; - process.nextTick(function () { - callback(null, 'irrelevantUsername'); - }); - } - }; - this.usernameQueue = new UsernameQueue(this.metadataBackend); - }); - - it('.enqueue(username) should enqueue the provided username', function (done) { - this.usernameQueue.enqueue('irrelevantUsername', function (err, username) { - assert.ok(!err); - assert.equal(username, 'irrelevantUsername'); - done(); - }); - }); - - it('.dequeue(username) should dequeue the next username', function (done) { - this.usernameQueue.dequeue(function (err, username) { - assert.ok(!err); - assert.equal(username, 'irrelevantUsername'); - done(); - }); - }); - -});