From ddd5a0bd190af330c53c5f0b15fc6837dce9c6ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Mon, 18 Jul 2016 14:49:57 +0200 Subject: [PATCH 01/16] Added integration folder to tests --- Makefile | 7 ++- test/integration/job_backend.test.js | 74 ++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 test/integration/job_backend.test.js diff --git a/Makefile b/Makefile index acc116a2..bb4aaba1 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,9 @@ jshint: @echo "***jshint***" @./node_modules/.bin/jshint app/ batch/ test/ app.js -TEST_SUITE := $(shell find test/{acceptance,unit} -name "*.js") +TEST_SUITE := $(shell find test/{acceptance,unit,integration} -name "*.js") TEST_SUITE_UNIT := $(shell find test/unit -name "*.js") +TEST_SUITE_INTEGRATION := $(shell find test/integration -name "*.js") TEST_SUITE_ACCEPTANCE := $(shell find test/acceptance -name "*.js") test: @@ -25,6 +26,10 @@ test-unit: @echo "***unit tests***" @$(SHELL) test/run_tests.sh ${RUNTESTFLAGS} $(TEST_SUITE_UNIT) +test-integration: + @echo "***integration tests***" + @$(SHELL) test/run_tests.sh ${RUNTESTFLAGS} $(TEST_SUITE_INTEGRATION) + test-acceptance: @echo "***acceptance tests***" @$(SHELL) test/run_tests.sh ${RUNTESTFLAGS} $(TEST_SUITE_ACCEPTANCE) diff --git a/test/integration/job_backend.test.js b/test/integration/job_backend.test.js new file mode 100644 index 00000000..ba8c6e83 --- /dev/null +++ b/test/integration/job_backend.test.js @@ -0,0 +1,74 @@ +'use strict'; + +require('../helper'); + +var BATCH_SOURCE = '../../batch/'; + + +var assert = require('../support/assert'); + +var _ = require('underscore'); +var RedisPool = require('redis-mpool'); + +var UserIndexer = require(BATCH_SOURCE + 'user_indexer'); +var JobQueue = require(BATCH_SOURCE + 'job_queue'); +var JobBackend = require(BATCH_SOURCE + 'job_backend'); +var JobPublisher = require(BATCH_SOURCE + 'job_publisher'); +var JobFactory = require(BATCH_SOURCE + 'models/job_factory'); +var jobStatus = require(BATCH_SOURCE + 'job_status'); + + +var redisConfig = { + host: global.settings.redis_host, + port: global.settings.redis_port, + max: global.settings.redisPool, + idleTimeoutMillis: global.settings.redisIdleTimeoutMillis, + reapIntervalMillis: global.settings.redisReapIntervalMillis +}; + +var metadataBackend = require('cartodb-redis')(redisConfig); +var redisPoolPublisher = new RedisPool(_.extend(redisConfig, { name: 'batch-publisher'})); +var jobPublisher = new JobPublisher(redisPoolPublisher); +var jobQueue = new JobQueue(metadataBackend, jobPublisher); +var userIndexer = new UserIndexer(metadataBackend); + +var USER = 'vizzuality'; +var QUERY = 'select pg_sleep(0)'; +var HOST = 'localhost'; +var JOB = { + user: USER, + query: QUERY, + host: HOST +}; + +describe('job backend', function() { + var jobBackend = new JobBackend(metadataBackend, jobQueue, userIndexer); + + it('.create() should persist a job', function (done) { + var job = JobFactory.create(JOB); + + jobBackend.create(job.data, function (err, jobCreated) { + if (err) { + return done(err); + } + + assert.ok(jobCreated.job_id); + assert.equal(jobCreated.status, jobStatus.PENDING); + done(); + }); + }); + + it('.create() should throw an error', function (done) { + var job = JobFactory.create(JOB); + + delete job.data.job_id; + + jobBackend.create(job, function (err) { + assert.ok(err); + assert.equal(err.name, 'NotFoundError'); + assert.equal(err.message, 'Job with id undefined not found'); + done(); + }); + }); + +}); From 89c3681be0cad745403e2f84472c84f3f776a39d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 19 Jul 2016 12:34:06 +0200 Subject: [PATCH 02/16] Fix bug when checking if a job is found --- batch/job_backend.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/batch/job_backend.js b/batch/job_backend.js index 8fd61302..2075045f 100644 --- a/batch/job_backend.js +++ b/batch/job_backend.js @@ -64,7 +64,7 @@ function toObject(job_id, redisParams, redisValues) { } function isJobFound(redisValues) { - return redisValues[0] && redisValues[1] && redisValues[2] && redisValues[3] && redisValues[4]; + return !!(redisValues[0] && redisValues[1] && redisValues[2] && redisValues[3] && redisValues[4]); } JobBackend.prototype.get = function (job_id, callback) { @@ -132,6 +132,7 @@ JobBackend.prototype.update = function (job, callback) { var self = this; self.get(job.job_id, function (err) { + if (err) { return callback(err); } From 2a4364142dca0487cc5bef37766819676bc52c13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 19 Jul 2016 13:08:52 +0200 Subject: [PATCH 03/16] Added integration test for job backend --- test/integration/job_backend.test.js | 83 ++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 4 deletions(-) diff --git a/test/integration/job_backend.test.js b/test/integration/job_backend.test.js index ba8c6e83..8c24cadc 100644 --- a/test/integration/job_backend.test.js +++ b/test/integration/job_backend.test.js @@ -41,11 +41,19 @@ var JOB = { host: HOST }; +function createWadusJob() { + return JobFactory.create(JSON.parse(JSON.stringify(JOB))); +} + describe('job backend', function() { - var jobBackend = new JobBackend(metadataBackend, jobQueue, userIndexer); + var jobBackend = {}; + + beforeEach(function () { + jobBackend = new JobBackend(metadataBackend, jobQueue, userIndexer); + }); it('.create() should persist a job', function (done) { - var job = JobFactory.create(JOB); + var job = createWadusJob(); jobBackend.create(job.data, function (err, jobCreated) { if (err) { @@ -58,8 +66,8 @@ describe('job backend', function() { }); }); - it('.create() should throw an error', function (done) { - var job = JobFactory.create(JOB); + it('.create() should return error', function (done) { + var job = createWadusJob(); delete job.data.job_id; @@ -71,4 +79,71 @@ describe('job backend', function() { }); }); + it('.update() should update an existent job', function (done) { + var job = createWadusJob(); + + jobBackend.create(job.data, function (err, jobCreated) { + if (err) { + return done(err); + } + + jobCreated.query = 'select pg_sleep(1)'; + + var job = JobFactory.create(jobCreated); + + jobBackend.update(job.data, function (err, jobUpdated) { + if (err) { + return done(err); + } + + assert.equal(jobUpdated.query, 'select pg_sleep(1)'); + done(); + }); + }); + }); + + it('.update() should return error when updates a nonexistent job', function (done) { + var job = createWadusJob(); + + jobBackend.update(job.data, function (err) { + assert.ok(err, err); + assert.equal(err.name, 'NotFoundError'); + assert.equal(err.message, 'Job with id ' + job.data.job_id + ' not found'); + done(); + }); + }); + + it('.list() should return a list of user\'s jobs', function (done) { + var job = createWadusJob(); + + jobBackend.create(job.data, function (err, jobCreated) { + if (err) { + return done(err); + } + + jobBackend.list(USER, function (err, jobs) { + var found = false; + + assert.ok(!err, err); + assert.ok(jobs.length); + + jobs.forEach(function (job) { + if (job.job_id === jobCreated.job_id) { + found = true; + } + }); + + assert.ok(found, 'Job expeted to be listed not found'); + done(); + }); + }); + }); + + it('.list() should return a empty list for nonexitent user', function (done) { + jobBackend.list('wadus_user', function (err, jobs) { + assert.ok(!err, err); + assert.ok(!jobs.length); + done(); + }); + }); }); From fe11828046466eece0bdf8a19961ce30de3f2b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 19 Jul 2016 16:20:53 +0200 Subject: [PATCH 04/16] Implemented integration test for job service --- test/integration/job_service.test.js | 276 +++++++++++++++++++++++++++ 1 file changed, 276 insertions(+) create mode 100644 test/integration/job_service.test.js diff --git a/test/integration/job_service.test.js b/test/integration/job_service.test.js new file mode 100644 index 00000000..9fa49714 --- /dev/null +++ b/test/integration/job_service.test.js @@ -0,0 +1,276 @@ +'use strict'; + +require('../helper'); + +var BATCH_SOURCE = '../../batch/'; + +var assert = require('../support/assert'); + +var _ = require('underscore'); +var RedisPool = require('redis-mpool'); + +var UserIndexer = require(BATCH_SOURCE + 'user_indexer'); +var JobQueue = require(BATCH_SOURCE + 'job_queue'); +var JobBackend = require(BATCH_SOURCE + 'job_backend'); +var JobPublisher = require(BATCH_SOURCE + 'job_publisher'); +var jobStatus = require(BATCH_SOURCE + 'job_status'); +var UserDatabaseMetadataService = require(BATCH_SOURCE + 'user_database_metadata_service'); +var JobCanceller = require(BATCH_SOURCE + 'job_canceller'); +var JobService = require(BATCH_SOURCE + 'job_service'); +var PSQL = require('cartodb-psql'); + +var redisConfig = { + host: global.settings.redis_host, + port: global.settings.redis_port, + max: global.settings.redisPool, + idleTimeoutMillis: global.settings.redisIdleTimeoutMillis, + reapIntervalMillis: global.settings.redisReapIntervalMillis +}; + +var metadataBackend = require('cartodb-redis')(redisConfig); +var redisPoolPublisher = new RedisPool(_.extend(redisConfig, { name: 'batch-publisher'})); +var jobPublisher = new JobPublisher(redisPoolPublisher); +var jobQueue = new JobQueue(metadataBackend, jobPublisher); +var userIndexer = new UserIndexer(metadataBackend); +var jobBackend = new JobBackend(metadataBackend, jobQueue, userIndexer); +var userDatabaseMetadataService = new UserDatabaseMetadataService(metadataBackend); +var jobCanceller = new JobCanceller(userDatabaseMetadataService); + +var USER = 'vizzuality'; +var QUERY = 'select pg_sleep(0)'; +var HOST = 'localhost'; +var JOB = { + user: USER, + query: QUERY, + host: HOST +}; + +// sets job to running, run its query and returns inmediatly (don't wait for query finishes) +// in order to test query cancelation/draining +function runQueryHelper(job, callback) { + var job_id = job.job_id; + var user = job.user; + var sql = job.query; + + job.status = jobStatus.RUNNING; + + jobBackend.update(job, function (err) { + if (err) { + return callback(err); + } + + userDatabaseMetadataService.getUserMetadata(user, function (err, userDatabaseMetadata) { + if (err) { + return callback(err); + } + + var pg = new PSQL(userDatabaseMetadata, {}, { destroyOnError: true }); + + sql = '/* ' + job_id + ' */ ' + sql; + + pg.eventedQuery(sql, function (err, query) { + if (err) { + return callback(err); + } + + callback(null, query); + }); + }); + }); +} + +describe('job service', function() { + var jobService = new JobService(jobBackend, jobCanceller); + + it('.get() should return a job', function (done) { + jobService.create(JOB, function (err, jobCreated) { + if (err) { + return done(err); + } + + jobService.get(jobCreated.data.job_id, function (err, job) { + if (err) { + return done(err); + } + + assert.equal(job.data.job_id, jobCreated.data.job_id); + done(); + }); + }); + }); + + it('.get() should return a not found error', function (done) { + jobService.get('wadus_job_id', function (err) { + assert.ok(err); + assert.equal(err.message, 'Job with id wadus_job_id not found'); + done(); + }); + }); + + it('.create() should persist a job', function (done) { + jobService.create(JOB, function (err, jobCreated) { + if (err) { + return done(err); + } + + assert.ok(jobCreated.data.job_id); + assert.equal(jobCreated.data.status, jobStatus.PENDING); + done(); + }); + }); + + it('.create() should return error with invalid job data', function (done) { + var job = JSON.parse(JSON.stringify(JOB)); + + delete job.query; + + jobService.create(job, function (err) { + assert.ok(err); + assert.equal(err.message, 'You must indicate a valid SQL'); + done(); + }); + }); + + it('.list() should return a list of user\'s jobs', function (done) { + jobService.create(JOB, function (err, jobCreated) { + if (err) { + return done(err); + } + + jobService.list(USER, function (err, jobs) { + var found = false; + + assert.ok(!err, err); + assert.ok(jobs.length); + + jobs.forEach(function (job) { + if (job.data.job_id === jobCreated.data.job_id) { + found = true; + } + }); + + assert.ok(found, 'Job expeted to be listed not found'); + done(); + }); + }); + }); + + it('.list() should return a empty list for nonexitent user', function (done) { + jobService.list('wadus_user', function (err, jobs) { + assert.ok(!err, err); + assert.ok(!jobs.length); + done(); + }); + }); + + it('.update() should update a job', function (done) { + jobService.create(JOB, function (err, jobCreated) { + if (err) { + return done(err); + } + + jobCreated.data.query = 'select pg_sleep(1)'; + + jobService.update(jobCreated.data, function (err, jobUpdated) { + if (err) { + return done(err); + } + + assert.equal(jobUpdated.data.job_id, jobCreated.data.job_id); + assert.equal(jobUpdated.data.query, 'select pg_sleep(1)'); + done(); + }); + }); + }); + + it('.update() should return error when updates a nonexistent job', function (done) { + var job = JSON.parse(JSON.stringify(JOB)); + job.job_id = 'wadus_job_id'; + + jobService.update(job, function (err) { + assert.ok(err, err); + assert.equal(err.name, 'NotFoundError'); + assert.equal(err.message, 'Job with id ' + job.job_id + ' not found'); + done(); + }); + }); + + it('.cancel() should cancel a running job', function (done) { + var job = { + user: USER, + query: 'select pg_sleep(3)', + host: HOST + }; + + jobService.create(job, function (err, job) { + if (err) { + return done(err); + } + + runQueryHelper(job.data, function (err) { + if (err) { + return done(err); + } + + jobService.cancel(job.data.job_id, function (err, jobCancelled) { + if (err) { + return done(err); + } + + assert.equal(jobCancelled.data.job_id, job.data.job_id); + assert.equal(jobCancelled.data.status, jobStatus.CANCELLED); + done(); + }); + }); + }); + }); + + it('.cancel() should return a job not found error', function (done) { + jobService.cancel('wadus_job_id', function (err) { + assert.ok(err, err); + assert.equal(err.name, 'NotFoundError'); + assert.equal(err.message, 'Job with id wadus_job_id not found'); + done(); + }); + }); + + it('.drain() should draing a running job', function (done) { + var job = { + user: USER, + query: 'select pg_sleep(3)', + host: HOST + }; + + jobService.create(job, function (err, job) { + if (err) { + return done(err); + } + + runQueryHelper(job.data, function (err) { + if (err) { + return done(err); + } + + jobService.drain(job.data.job_id, function (err, jobDrained) { + if (err) { + return done(err); + } + + assert.equal(jobDrained.job_id, job.data.job_id); + assert.equal(jobDrained.status, jobStatus.PENDING); + done(); + }); + }); + }); + }); + + it('.drain() should return a job not found error', function (done) { + jobService.drain('wadus_job_id', function (err) { + assert.ok(err, err); + assert.equal(err.name, 'NotFoundError'); + assert.equal(err.message, 'Job with id wadus_job_id not found'); + done(); + }); + }); + +}); From 8d4dbd85dfa4c27ce9a49595f8f366f31aee8317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 19 Jul 2016 19:42:49 +0200 Subject: [PATCH 05/16] Implmented integration tests for publisher, runner and canceller of batch service. --- test/integration/job_backend.test.js | 7 +- test/integration/job_canceller.test.js | 125 +++++++++++++++++++++++++ test/integration/job_publisher.test.js | 50 ++++++++++ test/integration/job_runner.test.js | 82 ++++++++++++++++ 4 files changed, 258 insertions(+), 6 deletions(-) create mode 100644 test/integration/job_canceller.test.js create mode 100644 test/integration/job_publisher.test.js create mode 100644 test/integration/job_runner.test.js diff --git a/test/integration/job_backend.test.js b/test/integration/job_backend.test.js index 8c24cadc..02e3b779 100644 --- a/test/integration/job_backend.test.js +++ b/test/integration/job_backend.test.js @@ -4,7 +4,6 @@ require('../helper'); var BATCH_SOURCE = '../../batch/'; - var assert = require('../support/assert'); var _ = require('underscore'); @@ -46,11 +45,7 @@ function createWadusJob() { } describe('job backend', function() { - var jobBackend = {}; - - beforeEach(function () { - jobBackend = new JobBackend(metadataBackend, jobQueue, userIndexer); - }); + var jobBackend = new JobBackend(metadataBackend, jobQueue, userIndexer); it('.create() should persist a job', function (done) { var job = createWadusJob(); diff --git a/test/integration/job_canceller.test.js b/test/integration/job_canceller.test.js new file mode 100644 index 00000000..57fb6ca3 --- /dev/null +++ b/test/integration/job_canceller.test.js @@ -0,0 +1,125 @@ +'use strict'; + +require('../helper'); + +var BATCH_SOURCE = '../../batch/'; + +var assert = require('../support/assert'); + +var _ = require('underscore'); +var RedisPool = require('redis-mpool'); + +var UserIndexer = require(BATCH_SOURCE + 'user_indexer'); +var JobQueue = require(BATCH_SOURCE + 'job_queue'); +var JobBackend = require(BATCH_SOURCE + 'job_backend'); +var JobPublisher = require(BATCH_SOURCE + 'job_publisher'); +var jobStatus = require(BATCH_SOURCE + 'job_status'); +var UserDatabaseMetadataService = require(BATCH_SOURCE + 'user_database_metadata_service'); +var JobCanceller = require(BATCH_SOURCE + 'job_canceller'); +var PSQL = require('cartodb-psql'); + +var redisConfig = { + host: global.settings.redis_host, + port: global.settings.redis_port, + max: global.settings.redisPool, + idleTimeoutMillis: global.settings.redisIdleTimeoutMillis, + reapIntervalMillis: global.settings.redisReapIntervalMillis +}; + +var metadataBackend = require('cartodb-redis')(redisConfig); +var redisPoolPublisher = new RedisPool(_.extend(redisConfig, { name: 'batch-publisher'})); +var jobPublisher = new JobPublisher(redisPoolPublisher); +var jobQueue = new JobQueue(metadataBackend, jobPublisher); +var userIndexer = new UserIndexer(metadataBackend); +var jobBackend = new JobBackend(metadataBackend, jobQueue, userIndexer); +var userDatabaseMetadataService = new UserDatabaseMetadataService(metadataBackend); +var JobFactory = require(BATCH_SOURCE + 'models/job_factory'); + +var USER = 'vizzuality'; +var QUERY = 'select pg_sleep(0)'; +var HOST = 'localhost'; +var JOB = { + user: USER, + query: QUERY, + host: HOST +}; + +// sets job to running, run its query and returns inmediatly (don't wait for query finishes) +// in order to test query cancelation/draining +function runQueryHelper(job, callback) { + var job_id = job.job_id; + var user = job.user; + var sql = job.query; + + job.status = jobStatus.RUNNING; + + jobBackend.update(job, function (err) { + if (err) { + return callback(err); + } + + userDatabaseMetadataService.getUserMetadata(user, function (err, userDatabaseMetadata) { + if (err) { + return callback(err); + } + + var pg = new PSQL(userDatabaseMetadata, {}, { destroyOnError: true }); + + sql = '/* ' + job_id + ' */ ' + sql; + + pg.eventedQuery(sql, function (err, query) { + if (err) { + return callback(err); + } + + callback(null, query); + }); + }); + }); +} + +function createWadusJob() { + return JobFactory.create(JSON.parse(JSON.stringify(JOB))); +} + +describe('job canceller', function() { + var jobCanceller = new JobCanceller(userDatabaseMetadataService); + + it('.cancel() should cancel a job', function (done) { + var job = createWadusJob(); + + jobBackend.create(job.data, function (err, jobCreated) { + if (err) { + return done(err); + } + + assert.equal(job.data.job_id, jobCreated.job_id); + + runQueryHelper(job.data, function (err) { + if (err) { + return done(err); + } + + jobCanceller.cancel(job, function (err) { + if (err) { + return done(err); + } + + done(); + }); + }); + }); + }); + + it('.cancel() a non running job should not return an error', function (done) { + var job = createWadusJob(); + + jobCanceller.cancel(job, function (err) { + if (err) { + return done(err); + } + + done(); + }); + }); +}); diff --git a/test/integration/job_publisher.test.js b/test/integration/job_publisher.test.js new file mode 100644 index 00000000..c64c5bcf --- /dev/null +++ b/test/integration/job_publisher.test.js @@ -0,0 +1,50 @@ +'use strict'; + +require('../helper'); + +var BATCH_SOURCE = '../../batch/'; + +var assert = require('../support/assert'); + +var _ = require('underscore'); +var RedisPool = require('redis-mpool'); + +var JobPublisher = require(BATCH_SOURCE + 'job_publisher'); + +var redisConfig = { + host: global.settings.redis_host, + port: global.settings.redis_port, + max: global.settings.redisPool, + idleTimeoutMillis: global.settings.redisIdleTimeoutMillis, + reapIntervalMillis: global.settings.redisReapIntervalMillis +}; + +var redisPoolPublisher = new RedisPool(_.extend(redisConfig, { name: 'batch-publisher'})); +var redisPoolSubscriber = new RedisPool(_.extend(redisConfig, { name: 'batch-subscriber'})); + +var HOST = 'wadus'; +var CHANNEL = 'batch:hosts'; +var DB = 0; + +describe('job publisher', function() { + var jobPublisher = new JobPublisher(redisPoolPublisher); + + it('.publish() should publish in job channel', function (done) { + redisPoolSubscriber.acquire(DB, function (err, client) { + if (err) { + return done(err); + } + + client.subscribe(CHANNEL); + + client.on('message', function (channel, host) { + assert.equal(host, HOST); + assert.equal(channel, CHANNEL) ; + done(); + }); + + jobPublisher.publish(HOST); + }); + }); + +}); diff --git a/test/integration/job_runner.test.js b/test/integration/job_runner.test.js new file mode 100644 index 00000000..23dbc9d7 --- /dev/null +++ b/test/integration/job_runner.test.js @@ -0,0 +1,82 @@ +'use strict'; + +require('../helper'); + +var BATCH_SOURCE = '../../batch/'; + +var assert = require('../support/assert'); + +var _ = require('underscore'); +var RedisPool = require('redis-mpool'); + +var UserIndexer = require(BATCH_SOURCE + 'user_indexer'); +var JobQueue = require(BATCH_SOURCE + 'job_queue'); +var JobBackend = require(BATCH_SOURCE + 'job_backend'); +var JobPublisher = require(BATCH_SOURCE + 'job_publisher'); +var jobStatus = require(BATCH_SOURCE + 'job_status'); +var UserDatabaseMetadataService = require(BATCH_SOURCE + 'user_database_metadata_service'); +var JobCanceller = require(BATCH_SOURCE + 'job_canceller'); +var JobService = require(BATCH_SOURCE + 'job_service'); +var JobRunner = require(BATCH_SOURCE + 'job_runner'); +var QueryRunner = require(BATCH_SOURCE + 'query_runner'); + +var redisConfig = { + host: global.settings.redis_host, + port: global.settings.redis_port, + max: global.settings.redisPool, + idleTimeoutMillis: global.settings.redisIdleTimeoutMillis, + reapIntervalMillis: global.settings.redisReapIntervalMillis +}; + +var metadataBackend = require('cartodb-redis')(redisConfig); +var redisPoolPublisher = new RedisPool(_.extend(redisConfig, { name: 'batch-publisher'})); +var jobPublisher = new JobPublisher(redisPoolPublisher); +var jobQueue = new JobQueue(metadataBackend, jobPublisher); +var userIndexer = new UserIndexer(metadataBackend); +var jobBackend = new JobBackend(metadataBackend, jobQueue, userIndexer); +var userDatabaseMetadataService = new UserDatabaseMetadataService(metadataBackend); +var jobCanceller = new JobCanceller(userDatabaseMetadataService); +var jobService = new JobService(jobBackend, jobCanceller); +var queryRunner = new QueryRunner(userDatabaseMetadataService); +var StatsD = require('node-statsd').StatsD; +var statsdClient = new StatsD(global.settings.statsd); + +var USER = 'vizzuality'; +var QUERY = 'select pg_sleep(0)'; +var HOST = 'localhost'; +var JOB = { + user: USER, + query: QUERY, + host: HOST +}; + +describe('job runner', function() { + var jobRunner = new JobRunner(jobService, jobQueue, queryRunner, statsdClient); + + it('.run() should run a job', function (done) { + jobService.create(JOB, function (err, job) { + if (err) { + return done(err); + } + + jobRunner.run(job.data.job_id, function (err, job) { + if (err) { + return done(err); + } + + assert.equal(job.data.status, jobStatus.DONE); + done(); + }); + }); + }); + + it('.run() should return a job not found error', function (done) { + jobRunner.run('wadus_job_id', function (err) { + assert.ok(err, err); + assert.equal(err.name, 'NotFoundError'); + assert.equal(err.message, 'Job with id wadus_job_id not found'); + done(); + }); + }); + +}); From b33caf61264bd96561572acbb9e1473b32e22a41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 19 Jul 2016 19:56:12 +0200 Subject: [PATCH 06/16] Moved batch integration test to a separated folder --- test/integration/{ => batch}/job_backend.test.js | 6 +++--- test/integration/{ => batch}/job_canceller.test.js | 6 +++--- test/integration/{ => batch}/job_publisher.test.js | 6 +++--- test/integration/{ => batch}/job_runner.test.js | 6 +++--- test/integration/{ => batch}/job_service.test.js | 6 +++--- 5 files changed, 15 insertions(+), 15 deletions(-) rename test/integration/{ => batch}/job_backend.test.js (97%) rename test/integration/{ => batch}/job_canceller.test.js (97%) rename test/integration/{ => batch}/job_publisher.test.js (92%) rename test/integration/{ => batch}/job_runner.test.js (96%) rename test/integration/{ => batch}/job_service.test.js (98%) diff --git a/test/integration/job_backend.test.js b/test/integration/batch/job_backend.test.js similarity index 97% rename from test/integration/job_backend.test.js rename to test/integration/batch/job_backend.test.js index 02e3b779..b34b024a 100644 --- a/test/integration/job_backend.test.js +++ b/test/integration/batch/job_backend.test.js @@ -1,10 +1,10 @@ 'use strict'; -require('../helper'); +require('../../helper'); -var BATCH_SOURCE = '../../batch/'; +var BATCH_SOURCE = '../../../batch/'; -var assert = require('../support/assert'); +var assert = require('../../support/assert'); var _ = require('underscore'); var RedisPool = require('redis-mpool'); diff --git a/test/integration/job_canceller.test.js b/test/integration/batch/job_canceller.test.js similarity index 97% rename from test/integration/job_canceller.test.js rename to test/integration/batch/job_canceller.test.js index 57fb6ca3..8ca1160e 100644 --- a/test/integration/job_canceller.test.js +++ b/test/integration/batch/job_canceller.test.js @@ -1,10 +1,10 @@ 'use strict'; -require('../helper'); +require('../../helper'); -var BATCH_SOURCE = '../../batch/'; +var BATCH_SOURCE = '../../../batch/'; -var assert = require('../support/assert'); +var assert = require('../../support/assert'); var _ = require('underscore'); var RedisPool = require('redis-mpool'); diff --git a/test/integration/job_publisher.test.js b/test/integration/batch/job_publisher.test.js similarity index 92% rename from test/integration/job_publisher.test.js rename to test/integration/batch/job_publisher.test.js index c64c5bcf..d6690247 100644 --- a/test/integration/job_publisher.test.js +++ b/test/integration/batch/job_publisher.test.js @@ -1,10 +1,10 @@ 'use strict'; -require('../helper'); +require('../../helper'); -var BATCH_SOURCE = '../../batch/'; +var BATCH_SOURCE = '../../../batch/'; -var assert = require('../support/assert'); +var assert = require('../../support/assert'); var _ = require('underscore'); var RedisPool = require('redis-mpool'); diff --git a/test/integration/job_runner.test.js b/test/integration/batch/job_runner.test.js similarity index 96% rename from test/integration/job_runner.test.js rename to test/integration/batch/job_runner.test.js index 23dbc9d7..39150b44 100644 --- a/test/integration/job_runner.test.js +++ b/test/integration/batch/job_runner.test.js @@ -1,10 +1,10 @@ 'use strict'; -require('../helper'); +require('../../helper'); -var BATCH_SOURCE = '../../batch/'; +var BATCH_SOURCE = '../../../batch/'; -var assert = require('../support/assert'); +var assert = require('../../support/assert'); var _ = require('underscore'); var RedisPool = require('redis-mpool'); diff --git a/test/integration/job_service.test.js b/test/integration/batch/job_service.test.js similarity index 98% rename from test/integration/job_service.test.js rename to test/integration/batch/job_service.test.js index 9fa49714..44431e92 100644 --- a/test/integration/job_service.test.js +++ b/test/integration/batch/job_service.test.js @@ -1,10 +1,10 @@ 'use strict'; -require('../helper'); +require('../../helper'); -var BATCH_SOURCE = '../../batch/'; +var BATCH_SOURCE = '../../../batch/'; -var assert = require('../support/assert'); +var assert = require('../../support/assert'); var _ = require('underscore'); var RedisPool = require('redis-mpool'); From 39a9f950b098f336ceda43ab7061f24443f2b709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 20 Jul 2016 16:38:57 +0200 Subject: [PATCH 07/16] Added integration test for multiquery jobs --- .../batch/batch.multiquery.test.js | 248 ++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 test/integration/batch/batch.multiquery.test.js diff --git a/test/integration/batch/batch.multiquery.test.js b/test/integration/batch/batch.multiquery.test.js new file mode 100644 index 00000000..cb9b88bf --- /dev/null +++ b/test/integration/batch/batch.multiquery.test.js @@ -0,0 +1,248 @@ + 'use strict'; + +require('../../helper'); +var assert = require('../../support/assert'); +var queue = require('queue-async'); + +var redisConfig = { + host: global.settings.redis_host, + port: global.settings.redis_port, + max: global.settings.redisPool, + idleTimeoutMillis: global.settings.redisIdleTimeoutMillis, + reapIntervalMillis: global.settings.redisReapIntervalMillis +}; + +var metadataBackend = require('cartodb-redis')(redisConfig); +var StatsD = require('node-statsd').StatsD; +var statsdClient = new StatsD(global.settings.statsd); + +var BATCH_SOURCE = '../../../batch/'; +var batchFactory = require(BATCH_SOURCE + 'index'); + + +var _ = require('underscore'); +var RedisPool = require('redis-mpool'); +var jobStatus = require(BATCH_SOURCE + 'job_status'); +var JobPublisher = require(BATCH_SOURCE + 'job_publisher'); +var JobQueue = require(BATCH_SOURCE + 'job_queue'); +var UserIndexer = require(BATCH_SOURCE + 'user_indexer'); +var JobBackend = require(BATCH_SOURCE + 'job_backend'); +var JobService = require(BATCH_SOURCE + 'job_service'); +var UserDatabaseMetadataService = require(BATCH_SOURCE + 'user_database_metadata_service'); +var JobCanceller = require(BATCH_SOURCE + 'job_canceller'); + +var redisPoolPublisher = new RedisPool(_.extend(redisConfig, { name: 'batch-publisher'})); +var jobPublisher = new JobPublisher(redisPoolPublisher); +var jobQueue = new JobQueue(metadataBackend, jobPublisher); +var userIndexer = new UserIndexer(metadataBackend); +var jobBackend = new JobBackend(metadataBackend, jobQueue, userIndexer); +var userDatabaseMetadataService = new UserDatabaseMetadataService(metadataBackend); +var jobCanceller = new JobCanceller(userDatabaseMetadataService); +var jobService = new JobService(jobBackend, jobCanceller); + +var USER = 'vizzuality'; +var HOST = 'localhost'; + +var batch = batchFactory(metadataBackend, redisConfig, statsdClient); + +function createJob(query, done) { + var data = { + user: USER, + query: query, + host: HOST + }; + + jobService.create(data, function (err, job) { + if (err) { + return done(err); + } + + done(null, job.serialize()); + }); +} + +function getJob(job_id, callback) { + jobService.get(job_id, function (err, job) { + if (err) { + return callback(err); + } + + callback(null, job.serialize()); + }); +} + +function assertJob(job, expectedStatus, done) { + return function (job_id) { + if (job.job_id === job_id) { + getJob(job_id, function (err, jobDone) { + if (err) { + return done(err); + } + + assert.equal(jobDone.status, expectedStatus); + done(); + }); + } + }; +} + +describe('batch multiquery', function() { + + beforeEach(function () { + batch.start(); + }); + + afterEach(function (done) { + batch.stop(); + batch.removeAllListeners(); + batch.drain(function () { + metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + }); + }); + + it('should perform one multiquery job with two queries', function (done) { + var queries = [ + 'select pg_sleep(0)', + 'select pg_sleep(0)' + ]; + + createJob(queries, function (err, job) { + if (err) { + return done(err); + } + + batch.on('job:done', assertJob(job, jobStatus.DONE, done)); + }); + }); + + it('should perform one multiquery job with two queries and fail on last one', function (done) { + var queries = [ + 'select pg_sleep(0)', + 'select shouldFail()' + ]; + + createJob(queries, function (err, job) { + if (err) { + return done(err); + } + + batch.on('job:failed', assertJob(job, jobStatus.FAILED, done)); + }); + }); + + it('should perform one multiquery job with three queries and fail on last one', function (done) { + var queries = [ + 'select pg_sleep(0)', + 'select pg_sleep(0)', + 'select shouldFail()' + ]; + + createJob(queries, function (err, job) { + if (err) { + return done(err); + } + + batch.on('job:failed', assertJob(job, jobStatus.FAILED, done)); + }); + }); + + + it('should perform one multiquery job with three queries and fail on second one', function (done) { + var queries = [ + 'select pg_sleep(0)', + 'select shouldFail()', + 'select pg_sleep(0)' + ]; + + createJob(queries, function (err, job) { + if (err) { + return done(err); + } + + batch.on('job:failed', assertJob(job, jobStatus.FAILED, done)); + }); + }); + + it('should perform two multiquery job with two queries for each one', function (done) { + var jobs = [[ + 'select pg_sleep(0)', + 'select pg_sleep(0)' + ], [ + 'select pg_sleep(0)', + 'select pg_sleep(0)' + ]]; + + var jobsQueue = queue(jobs.length); + + jobs.forEach(function(job) { + jobsQueue.defer(createJob, job); + }); + + jobsQueue.awaitAll(function (err, jobs) { + if (err) { + return done(err); + } + + jobs.forEach(function (job) { + batch.on('job:done', assertJob(job, jobStatus.DONE, done)); + }); + }); + }); + + it('should perform two multiquery job with two queries for each one and fail the first one', function (done) { + var jobs = [[ + 'select pg_sleep(0)', + 'select shouldFail()' + ], [ + 'select pg_sleep(0)', + 'select pg_sleep(0)' + ]]; + + queue(jobs.length) + .defer(createJob, jobs[0]) + .defer(createJob, jobs[1]) + .awaitAll(function (err, createdJobs) { + if (err) { + return done(err); + } + + queue(createdJobs.length) + .defer(function (callback) { + batch.on('job:failed', assertJob(createdJobs[0], jobStatus.FAILED, callback)); + }) + .defer(function (callback) { + batch.on('job:done', assertJob(createdJobs[1], jobStatus.DONE, callback)); + }) + .awaitAll(done); + }); + }); + + it('should perform two multiquery job with two queries for each one and fail the second one', function (done) { + var jobs = [[ + 'select pg_sleep(0)', + 'select pg_sleep(0)' + ], [ + 'select pg_sleep(0)', + 'select shouldFail()', + ]]; + + queue(jobs.length) + .defer(createJob, jobs[0]) + .defer(createJob, jobs[1]) + .awaitAll(function (err, createdJobs) { + if (err) { + return done(err); + } + + queue(createdJobs.length) + .defer(function (callback) { + batch.on('job:done', assertJob(createdJobs[0], jobStatus.DONE, callback)); + }) + .defer(function (callback) { + batch.on('job:failed', assertJob(createdJobs[1], jobStatus.FAILED, callback)); + }) + .awaitAll(done); + }); + }); + +}); From 457414c2fe31a19bfb87050085c8d3162c8be579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 20 Jul 2016 18:27:54 +0200 Subject: [PATCH 08/16] Skipping integration test --- .../batch/batch.multiquery.test.js | 190 ++++++++---------- 1 file changed, 82 insertions(+), 108 deletions(-) diff --git a/test/integration/batch/batch.multiquery.test.js b/test/integration/batch/batch.multiquery.test.js index cb9b88bf..f65289c1 100644 --- a/test/integration/batch/batch.multiquery.test.js +++ b/test/integration/batch/batch.multiquery.test.js @@ -27,47 +27,29 @@ var JobPublisher = require(BATCH_SOURCE + 'job_publisher'); var JobQueue = require(BATCH_SOURCE + 'job_queue'); var UserIndexer = require(BATCH_SOURCE + 'user_indexer'); var JobBackend = require(BATCH_SOURCE + 'job_backend'); -var JobService = require(BATCH_SOURCE + 'job_service'); -var UserDatabaseMetadataService = require(BATCH_SOURCE + 'user_database_metadata_service'); -var JobCanceller = require(BATCH_SOURCE + 'job_canceller'); +var JobFactory = require(BATCH_SOURCE + 'models/job_factory'); var redisPoolPublisher = new RedisPool(_.extend(redisConfig, { name: 'batch-publisher'})); var jobPublisher = new JobPublisher(redisPoolPublisher); var jobQueue = new JobQueue(metadataBackend, jobPublisher); var userIndexer = new UserIndexer(metadataBackend); var jobBackend = new JobBackend(metadataBackend, jobQueue, userIndexer); -var userDatabaseMetadataService = new UserDatabaseMetadataService(metadataBackend); -var jobCanceller = new JobCanceller(userDatabaseMetadataService); -var jobService = new JobService(jobBackend, jobCanceller); + var USER = 'vizzuality'; var HOST = 'localhost'; -var batch = batchFactory(metadataBackend, redisConfig, statsdClient); - -function createJob(query, done) { - var data = { - user: USER, - query: query, - host: HOST - }; - - jobService.create(data, function (err, job) { - if (err) { - return done(err); - } - - done(null, job.serialize()); - }); +function createJob(job) { + jobBackend.create(job, function () {}); } function getJob(job_id, callback) { - jobService.get(job_id, function (err, job) { + jobBackend.get(job_id, function (err, job) { if (err) { return callback(err); } - callback(null, job.serialize()); + callback(null, job); }); } @@ -86,18 +68,16 @@ function assertJob(job, expectedStatus, done) { }; } -describe('batch multiquery', function() { +describe.skip('batch multiquery', function() { + var batch = batchFactory(metadataBackend, redisConfig, statsdClient); beforeEach(function () { batch.start(); }); - afterEach(function (done) { - batch.stop(); + afterEach(function () { batch.removeAllListeners(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); - }); + batch.stop(); }); it('should perform one multiquery job with two queries', function (done) { @@ -106,13 +86,12 @@ describe('batch multiquery', function() { 'select pg_sleep(0)' ]; - createJob(queries, function (err, job) { - if (err) { - return done(err); - } + var job = JobFactory.create({ user: USER, host: HOST, query: queries}); + var assertCallback = assertJob(job.data, jobStatus.DONE, done); - batch.on('job:done', assertJob(job, jobStatus.DONE, done)); - }); + batch.on('job:done', assertCallback); + + createJob(job.data); }); it('should perform one multiquery job with two queries and fail on last one', function (done) { @@ -121,13 +100,12 @@ describe('batch multiquery', function() { 'select shouldFail()' ]; - createJob(queries, function (err, job) { - if (err) { - return done(err); - } + var job = JobFactory.create({ user: USER, host: HOST, query: queries}); + var assertCallback = assertJob(job.data, jobStatus.FAILED, done); - batch.on('job:failed', assertJob(job, jobStatus.FAILED, done)); - }); + batch.on('job:failed', assertCallback); + + createJob(job.data); }); it('should perform one multiquery job with three queries and fail on last one', function (done) { @@ -137,13 +115,12 @@ describe('batch multiquery', function() { 'select shouldFail()' ]; - createJob(queries, function (err, job) { - if (err) { - return done(err); - } + var job = JobFactory.create({ user: USER, host: HOST, query: queries}); + var assertCallback = assertJob(job.data, jobStatus.FAILED, done); - batch.on('job:failed', assertJob(job, jobStatus.FAILED, done)); - }); + batch.on('job:failed', assertCallback); + + createJob(job.data); }); @@ -154,95 +131,92 @@ describe('batch multiquery', function() { 'select pg_sleep(0)' ]; - createJob(queries, function (err, job) { - if (err) { - return done(err); - } + var job = JobFactory.create({ user: USER, host: HOST, query: queries}); + var assertCallback = assertJob(job.data, jobStatus.FAILED, done); - batch.on('job:failed', assertJob(job, jobStatus.FAILED, done)); - }); + batch.on('job:failed', assertCallback); + + createJob(job.data); }); it('should perform two multiquery job with two queries for each one', function (done) { - var jobs = [[ + var jobs = []; + + jobs.push(JobFactory.create({ user: USER, host: HOST, query: [ 'select pg_sleep(0)', 'select pg_sleep(0)' - ], [ + ]})); + + jobs.push(JobFactory.create({ user: USER, host: HOST, query: [ 'select pg_sleep(0)', 'select pg_sleep(0)' - ]]; + ]})); var jobsQueue = queue(jobs.length); - jobs.forEach(function(job) { - jobsQueue.defer(createJob, job); - }); - - jobsQueue.awaitAll(function (err, jobs) { - if (err) { - return done(err); - } - - jobs.forEach(function (job) { - batch.on('job:done', assertJob(job, jobStatus.DONE, done)); + jobs.forEach(function (job) { + jobsQueue.defer(function (callback) { + batch.on('job:done', assertJob(job.data, jobStatus.DONE, callback)); + createJob(job.data); }); }); + + jobsQueue.awaitAll(done); }); it('should perform two multiquery job with two queries for each one and fail the first one', function (done) { - var jobs = [[ + var jobs = []; + + jobs.push(JobFactory.create({ user: USER, host: HOST, query: [ 'select pg_sleep(0)', 'select shouldFail()' - ], [ + ]})); + + jobs.push(JobFactory.create({ user: USER, host: HOST, query: [ 'select pg_sleep(0)', 'select pg_sleep(0)' - ]]; + ]})); - queue(jobs.length) - .defer(createJob, jobs[0]) - .defer(createJob, jobs[1]) - .awaitAll(function (err, createdJobs) { - if (err) { - return done(err); - } + var jobsQueue = queue(jobs.length); - queue(createdJobs.length) - .defer(function (callback) { - batch.on('job:failed', assertJob(createdJobs[0], jobStatus.FAILED, callback)); - }) - .defer(function (callback) { - batch.on('job:done', assertJob(createdJobs[1], jobStatus.DONE, callback)); - }) - .awaitAll(done); - }); + jobsQueue.defer(function (callback) { + batch.on('job:failed', assertJob(jobs[0].data, jobStatus.FAILED, callback)); + createJob(jobs[0].data); + }); + + jobsQueue.defer(function (callback) { + batch.on('job:done', assertJob(jobs[1].data, jobStatus.DONE, callback)); + createJob(jobs[1].data); + }); + + jobsQueue.awaitAll(done); }); it('should perform two multiquery job with two queries for each one and fail the second one', function (done) { - var jobs = [[ + var jobs = []; + + jobs.push(JobFactory.create({ user: USER, host: HOST, query: [ 'select pg_sleep(0)', 'select pg_sleep(0)' - ], [ + ]})); + + jobs.push(JobFactory.create({ user: USER, host: HOST, query: [ 'select pg_sleep(0)', - 'select shouldFail()', - ]]; + 'select shouldFail()' + ]})); - queue(jobs.length) - .defer(createJob, jobs[0]) - .defer(createJob, jobs[1]) - .awaitAll(function (err, createdJobs) { - if (err) { - return done(err); - } + var jobsQueue = queue(jobs.length); - queue(createdJobs.length) - .defer(function (callback) { - batch.on('job:done', assertJob(createdJobs[0], jobStatus.DONE, callback)); - }) - .defer(function (callback) { - batch.on('job:failed', assertJob(createdJobs[1], jobStatus.FAILED, callback)); - }) - .awaitAll(done); - }); + jobsQueue.defer(function (callback) { + batch.on('job:done', assertJob(jobs[0].data, jobStatus.DONE, callback)); + createJob(jobs[0].data); + }); + + jobsQueue.defer(function (callback) { + batch.on('job:failed', assertJob(jobs[1].data, jobStatus.FAILED, callback)); + createJob(jobs[1].data); + }); + + jobsQueue.awaitAll(done); }); - }); From 1625d8a556329341a9da8c22f6a7ec26ba1d5c74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 20 Jul 2016 19:41:55 +0200 Subject: [PATCH 09/16] Fixed timeout in job canceller test --- test/integration/batch/job_canceller.test.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/test/integration/batch/job_canceller.test.js b/test/integration/batch/job_canceller.test.js index 8ca1160e..2dfb2029 100644 --- a/test/integration/batch/job_canceller.test.js +++ b/test/integration/batch/job_canceller.test.js @@ -6,6 +6,8 @@ var BATCH_SOURCE = '../../../batch/'; var assert = require('../../support/assert'); +var errorCodes = require('../../../app/postgresql/error_codes').codeToCondition; + var _ = require('underscore'); var RedisPool = require('redis-mpool'); @@ -38,11 +40,6 @@ var JobFactory = require(BATCH_SOURCE + 'models/job_factory'); var USER = 'vizzuality'; var QUERY = 'select pg_sleep(0)'; var HOST = 'localhost'; -var JOB = { - user: USER, - query: QUERY, - host: HOST -}; // sets job to running, run its query and returns inmediatly (don't wait for query finishes) // in order to test query cancelation/draining @@ -78,15 +75,20 @@ function runQueryHelper(job, callback) { }); } -function createWadusJob() { - return JobFactory.create(JSON.parse(JSON.stringify(JOB))); +function createWadusJob(query) { + query = query || QUERY; + return JobFactory.create(JSON.parse(JSON.stringify({ + user: USER, + query: query, + host: HOST + }))); } describe('job canceller', function() { var jobCanceller = new JobCanceller(userDatabaseMetadataService); it('.cancel() should cancel a job', function (done) { - var job = createWadusJob(); + var job = createWadusJob('select pg_sleep(1)'); jobBackend.create(job.data, function (err, jobCreated) { if (err) { From 027e7b8768835165af14106031c7c21f13ebde17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 20 Jul 2016 19:42:33 +0200 Subject: [PATCH 10/16] Removed unused dependency --- test/integration/batch/job_canceller.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/integration/batch/job_canceller.test.js b/test/integration/batch/job_canceller.test.js index 2dfb2029..f7c9be33 100644 --- a/test/integration/batch/job_canceller.test.js +++ b/test/integration/batch/job_canceller.test.js @@ -6,8 +6,6 @@ var BATCH_SOURCE = '../../../batch/'; var assert = require('../../support/assert'); -var errorCodes = require('../../../app/postgresql/error_codes').codeToCondition; - var _ = require('underscore'); var RedisPool = require('redis-mpool'); From 438d61fb70b01c41898be112ec1f603aa5595f6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 20 Jul 2016 19:50:28 +0200 Subject: [PATCH 11/16] Set timeout to 30s --- test/acceptance/job.use-case-1.test.js | 1 + test/acceptance/job.use-case-10.test.js | 1 + test/acceptance/job.use-case-2.test.js | 1 + test/acceptance/job.use-case-3.test.js | 1 + test/acceptance/job.use-case-4.test.js | 1 + test/acceptance/job.use-case-5.test.js | 2 ++ test/acceptance/job.use-case-6.test.js | 1 + test/acceptance/job.use-case-7.test.js | 1 + test/acceptance/job.use-case-8.test.js | 1 + test/acceptance/job.use-case-9.test.js | 1 + 10 files changed, 11 insertions(+) diff --git a/test/acceptance/job.use-case-1.test.js b/test/acceptance/job.use-case-1.test.js index e8960679..7fe1d58f 100644 --- a/test/acceptance/job.use-case-1.test.js +++ b/test/acceptance/job.use-case-1.test.js @@ -28,6 +28,7 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 1: cancel and modify a done job', function () { + this.timeout(30000); var batch = batchFactory(metadataBackend, redisConfig); diff --git a/test/acceptance/job.use-case-10.test.js b/test/acceptance/job.use-case-10.test.js index f4d1a5ab..9ed4ec15 100644 --- a/test/acceptance/job.use-case-10.test.js +++ b/test/acceptance/job.use-case-10.test.js @@ -28,6 +28,7 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 10: cancel and modify a done multiquery job', function () { + this.timeout(30000); var batch = batchFactory(metadataBackend, redisConfig); diff --git a/test/acceptance/job.use-case-2.test.js b/test/acceptance/job.use-case-2.test.js index 016f064d..f397d445 100644 --- a/test/acceptance/job.use-case-2.test.js +++ b/test/acceptance/job.use-case-2.test.js @@ -28,6 +28,7 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 2: cancel a running job', function() { + this.timeout(30000); var batch = batchFactory(metadataBackend, redisConfig); diff --git a/test/acceptance/job.use-case-3.test.js b/test/acceptance/job.use-case-3.test.js index 3cbd372d..bad66fe1 100644 --- a/test/acceptance/job.use-case-3.test.js +++ b/test/acceptance/job.use-case-3.test.js @@ -28,6 +28,7 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 3: cancel a pending job', function() { + this.timeout(30000); var batch = batchFactory(metadataBackend, redisConfig); diff --git a/test/acceptance/job.use-case-4.test.js b/test/acceptance/job.use-case-4.test.js index 4adb0ff3..3d61fda0 100644 --- a/test/acceptance/job.use-case-4.test.js +++ b/test/acceptance/job.use-case-4.test.js @@ -28,6 +28,7 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 4: modify a pending job', function() { + this.timeout(30000); var batch = batchFactory(metadataBackend, redisConfig); diff --git a/test/acceptance/job.use-case-5.test.js b/test/acceptance/job.use-case-5.test.js index dbddd688..6a073687 100644 --- a/test/acceptance/job.use-case-5.test.js +++ b/test/acceptance/job.use-case-5.test.js @@ -28,6 +28,8 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 5: modify a running job', function() { + this.timeout(30000); + var batch = batchFactory(metadataBackend, redisConfig); before(function () { diff --git a/test/acceptance/job.use-case-6.test.js b/test/acceptance/job.use-case-6.test.js index 18a99d4f..0294198f 100644 --- a/test/acceptance/job.use-case-6.test.js +++ b/test/acceptance/job.use-case-6.test.js @@ -28,6 +28,7 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 6: modify a done job', function() { + this.timeout(30000); var batch = batchFactory(metadataBackend, redisConfig); diff --git a/test/acceptance/job.use-case-7.test.js b/test/acceptance/job.use-case-7.test.js index 84896c3d..c1821fec 100644 --- a/test/acceptance/job.use-case-7.test.js +++ b/test/acceptance/job.use-case-7.test.js @@ -28,6 +28,7 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 7: cancel a job with quotes', function() { + this.timeout(30000); var batch = batchFactory(metadataBackend, redisConfig); diff --git a/test/acceptance/job.use-case-8.test.js b/test/acceptance/job.use-case-8.test.js index b9787403..a7cf321a 100644 --- a/test/acceptance/job.use-case-8.test.js +++ b/test/acceptance/job.use-case-8.test.js @@ -28,6 +28,7 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 8: cancel a running multiquery job', function() { + this.timeout(30000); var batch = batchFactory(metadataBackend, redisConfig); diff --git a/test/acceptance/job.use-case-9.test.js b/test/acceptance/job.use-case-9.test.js index 3e995a97..1eb390ac 100644 --- a/test/acceptance/job.use-case-9.test.js +++ b/test/acceptance/job.use-case-9.test.js @@ -28,6 +28,7 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 9: modify a pending multiquery job', function() { + this.timeout(30000); var batch = batchFactory(metadataBackend, redisConfig); From 0586f454133e23bb29258af7da9d0055343a3616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 22 Jul 2016 13:47:14 +0200 Subject: [PATCH 12/16] Added callback to job subscriber to allow to batch service emit ready event --- batch/batch.js | 8 +++++++- batch/job_subscriber.js | 36 +++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/batch/batch.js b/batch/batch.js index 66b158c7..3edcec0f 100644 --- a/batch/batch.js +++ b/batch/batch.js @@ -25,7 +25,7 @@ Batch.prototype.start = function () { Batch.prototype._subscribe = function () { var self = this; - this.jobSubscriber.subscribe(function (channel, host) { + this.jobSubscriber.subscribe(function onMessage(channel, host) { var queue = self.jobQueuePool.getQueue(host); // there is nothing to do. It is already running jobs @@ -46,6 +46,12 @@ Batch.prototype._subscribe = function () { debug(err); }); + }, function (err) { + if (err) { + return self.emit('error', err); + } + + self.emit('ready'); }); }; diff --git a/batch/job_subscriber.js b/batch/job_subscriber.js index 3aab46db..33b66925 100644 --- a/batch/job_subscriber.js +++ b/batch/job_subscriber.js @@ -6,21 +6,31 @@ var error = require('./util/debug')('pubsub:subscriber:error'); var DB = 0; var SUBSCRIBE_INTERVAL_IN_MILLISECONDS = 10 * 60 * 1000; // 10 minutes -function _subscribe(client, channel, queueSeeker, onMessage) { +function _subscribe(client, channel, queueSeeker, onMessage, callback) { + + client.removeAllListeners('message'); + client.unsubscribe(channel); + client.subscribe(channel); + + client.on('message', function (channel, host) { + debug('message received from: ' + channel + ':' + host); + onMessage(channel, host); + }); queueSeeker.seek(onMessage, function (err) { if (err) { error(err); + + if (callback) { + callback(err); + } + } else { + debug('queues found successfully'); + + if (callback) { + callback(); + } } - - client.removeAllListeners('message'); - client.unsubscribe(channel); - client.subscribe(channel); - - client.on('message', function (channel, host) { - debug('message received from: ' + channel + ':' + host); - onMessage(channel, host); - }); }); } @@ -32,7 +42,7 @@ function JobSubscriber(pool, queueSeeker) { module.exports = JobSubscriber; -JobSubscriber.prototype.subscribe = function (onMessage) { +JobSubscriber.prototype.subscribe = function (onMessage, callback) { var self = this; this.pool.acquire(DB, function (err, client) { @@ -42,8 +52,6 @@ JobSubscriber.prototype.subscribe = function (onMessage) { self.client = client; - _subscribe(self.client, self.channel, self.queueSeeker, onMessage); - self.seekerInterval = setInterval( _subscribe, SUBSCRIBE_INTERVAL_IN_MILLISECONDS, @@ -52,6 +60,8 @@ JobSubscriber.prototype.subscribe = function (onMessage) { self.queueSeeker, onMessage ); + + _subscribe(self.client, self.channel, self.queueSeeker, onMessage, callback); }); }; From 2e8382eeffc9982dea8c8cb8eab7ebfd2dae5f62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 22 Jul 2016 14:27:18 +0200 Subject: [PATCH 13/16] Fixed issues in batch tests: - Wait for batch is ready before testing it - Remove batch keys in redis after testing every suit - Fixed issue creating jobs with same id in job service integration test --- test/acceptance/batch.test.js | 8 ++-- test/acceptance/job.callback-template.test.js | 10 +++-- test/acceptance/job.fallback.test.js | 8 ++-- test/acceptance/job.query.limit.test.js | 5 ++- test/acceptance/job.test.js | 5 ++- test/acceptance/job.timing.test.js | 8 ++-- test/acceptance/job.use-case-1.test.js | 9 +++-- test/acceptance/job.use-case-10.test.js | 8 ++-- test/acceptance/job.use-case-2.test.js | 8 ++-- test/acceptance/job.use-case-3.test.js | 8 ++-- test/acceptance/job.use-case-4.test.js | 8 ++-- test/acceptance/job.use-case-5.test.js | 8 ++-- test/acceptance/job.use-case-6.test.js | 8 ++-- test/acceptance/job.use-case-7.test.js | 8 ++-- test/acceptance/job.use-case-8.test.js | 8 ++-- test/acceptance/job.use-case-9.test.js | 8 ++-- .../batch/batch.multiquery.test.js | 12 ++++-- test/integration/batch/job_backend.test.js | 7 ++++ test/integration/batch/job_canceller.test.js | 8 ++++ test/integration/batch/job_runner.test.js | 7 ++++ test/integration/batch/job_service.test.js | 38 +++++++++++-------- 21 files changed, 132 insertions(+), 65 deletions(-) diff --git a/test/acceptance/batch.test.js b/test/acceptance/batch.test.js index abf3584c..e17e5af0 100644 --- a/test/acceptance/batch.test.js +++ b/test/acceptance/batch.test.js @@ -34,14 +34,16 @@ describe('batch module', function() { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); diff --git a/test/acceptance/job.callback-template.test.js b/test/acceptance/job.callback-template.test.js index e1671f00..365949b4 100644 --- a/test/acceptance/job.callback-template.test.js +++ b/test/acceptance/job.callback-template.test.js @@ -96,18 +96,20 @@ describe('Batch API callback templates', function () { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); - describe.skip('should use templates for error_message and job_id onerror callback', function () { + describe('should use templates for error_message and job_id onerror callback', function () { var jobResponse; before(function(done) { getQueryResult('create table test_batch_errors (job_id text, error_message text)', function(err) { diff --git a/test/acceptance/job.fallback.test.js b/test/acceptance/job.fallback.test.js index 46904df5..93c65bf8 100644 --- a/test/acceptance/job.fallback.test.js +++ b/test/acceptance/job.fallback.test.js @@ -37,14 +37,16 @@ describe('Batch API fallback job', function () { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); diff --git a/test/acceptance/job.query.limit.test.js b/test/acceptance/job.query.limit.test.js index c9cbceab..dfb96adc 100644 --- a/test/acceptance/job.query.limit.test.js +++ b/test/acceptance/job.query.limit.test.js @@ -46,7 +46,10 @@ describe('job query limit', function() { after(function (done) { // batch services is not activate, so we need empty the queue to avoid unexpected // behaviour in further tests - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); + }); }); it('POST /api/v2/sql/job with a invalid query size should respond with 400 query too long', function (done){ diff --git a/test/acceptance/job.test.js b/test/acceptance/job.test.js index d81a6cca..c5aaa461 100644 --- a/test/acceptance/job.test.js +++ b/test/acceptance/job.test.js @@ -31,7 +31,10 @@ describe('job module', function() { after(function (done) { // batch services is not activate, so we need empty the queue to avoid unexpected // behaviour in further tests - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); + }); }); it('POST /api/v2/sql/job should respond with 200 and the created job', function (done){ diff --git a/test/acceptance/job.timing.test.js b/test/acceptance/job.timing.test.js index be89fead..da54c7de 100644 --- a/test/acceptance/job.timing.test.js +++ b/test/acceptance/job.timing.test.js @@ -73,14 +73,16 @@ describe('Batch API query timing', function () { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); diff --git a/test/acceptance/job.use-case-1.test.js b/test/acceptance/job.use-case-1.test.js index 7fe1d58f..1ede0933 100644 --- a/test/acceptance/job.use-case-1.test.js +++ b/test/acceptance/job.use-case-1.test.js @@ -32,14 +32,17 @@ describe('Use case 1: cancel and modify a done job', function () { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); diff --git a/test/acceptance/job.use-case-10.test.js b/test/acceptance/job.use-case-10.test.js index 9ed4ec15..591e040c 100644 --- a/test/acceptance/job.use-case-10.test.js +++ b/test/acceptance/job.use-case-10.test.js @@ -32,14 +32,16 @@ describe('Use case 10: cancel and modify a done multiquery job', function () { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); diff --git a/test/acceptance/job.use-case-2.test.js b/test/acceptance/job.use-case-2.test.js index f397d445..11872e80 100644 --- a/test/acceptance/job.use-case-2.test.js +++ b/test/acceptance/job.use-case-2.test.js @@ -32,14 +32,16 @@ describe('Use case 2: cancel a running job', function() { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); diff --git a/test/acceptance/job.use-case-3.test.js b/test/acceptance/job.use-case-3.test.js index bad66fe1..7b0eebaf 100644 --- a/test/acceptance/job.use-case-3.test.js +++ b/test/acceptance/job.use-case-3.test.js @@ -32,14 +32,16 @@ describe('Use case 3: cancel a pending job', function() { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); diff --git a/test/acceptance/job.use-case-4.test.js b/test/acceptance/job.use-case-4.test.js index 3d61fda0..5538c29d 100644 --- a/test/acceptance/job.use-case-4.test.js +++ b/test/acceptance/job.use-case-4.test.js @@ -32,14 +32,16 @@ describe('Use case 4: modify a pending job', function() { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); diff --git a/test/acceptance/job.use-case-5.test.js b/test/acceptance/job.use-case-5.test.js index 6a073687..a9763b47 100644 --- a/test/acceptance/job.use-case-5.test.js +++ b/test/acceptance/job.use-case-5.test.js @@ -32,14 +32,16 @@ describe('Use case 5: modify a running job', function() { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); diff --git a/test/acceptance/job.use-case-6.test.js b/test/acceptance/job.use-case-6.test.js index 0294198f..7580972e 100644 --- a/test/acceptance/job.use-case-6.test.js +++ b/test/acceptance/job.use-case-6.test.js @@ -32,14 +32,16 @@ describe('Use case 6: modify a done job', function() { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); diff --git a/test/acceptance/job.use-case-7.test.js b/test/acceptance/job.use-case-7.test.js index c1821fec..b5564b43 100644 --- a/test/acceptance/job.use-case-7.test.js +++ b/test/acceptance/job.use-case-7.test.js @@ -32,14 +32,16 @@ describe('Use case 7: cancel a job with quotes', function() { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); diff --git a/test/acceptance/job.use-case-8.test.js b/test/acceptance/job.use-case-8.test.js index a7cf321a..021959fa 100644 --- a/test/acceptance/job.use-case-8.test.js +++ b/test/acceptance/job.use-case-8.test.js @@ -32,14 +32,16 @@ describe('Use case 8: cancel a running multiquery job', function() { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); diff --git a/test/acceptance/job.use-case-9.test.js b/test/acceptance/job.use-case-9.test.js index 1eb390ac..4ab511c1 100644 --- a/test/acceptance/job.use-case-9.test.js +++ b/test/acceptance/job.use-case-9.test.js @@ -32,14 +32,16 @@ describe('Use case 9: modify a pending multiquery job', function() { var batch = batchFactory(metadataBackend, redisConfig); - before(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); after(function (done) { batch.stop(); - batch.drain(function () { - metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); }); }); diff --git a/test/integration/batch/batch.multiquery.test.js b/test/integration/batch/batch.multiquery.test.js index f65289c1..84fdb2fc 100644 --- a/test/integration/batch/batch.multiquery.test.js +++ b/test/integration/batch/batch.multiquery.test.js @@ -35,7 +35,6 @@ var jobQueue = new JobQueue(metadataBackend, jobPublisher); var userIndexer = new UserIndexer(metadataBackend); var jobBackend = new JobBackend(metadataBackend, jobQueue, userIndexer); - var USER = 'vizzuality'; var HOST = 'localhost'; @@ -68,16 +67,21 @@ function assertJob(job, expectedStatus, done) { }; } -describe.skip('batch multiquery', function() { +describe('batch multiquery', function() { var batch = batchFactory(metadataBackend, redisConfig, statsdClient); - beforeEach(function () { + before(function (done) { batch.start(); + batch.on('ready', done); }); - afterEach(function () { + after(function (done) { batch.removeAllListeners(); batch.stop(); + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); + }); }); it('should perform one multiquery job with two queries', function (done) { diff --git a/test/integration/batch/job_backend.test.js b/test/integration/batch/job_backend.test.js index b34b024a..9f81c41c 100644 --- a/test/integration/batch/job_backend.test.js +++ b/test/integration/batch/job_backend.test.js @@ -47,6 +47,13 @@ function createWadusJob() { describe('job backend', function() { var jobBackend = new JobBackend(metadataBackend, jobQueue, userIndexer); + after(function (done) { + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); + }); + }); + it('.create() should persist a job', function (done) { var job = createWadusJob(); diff --git a/test/integration/batch/job_canceller.test.js b/test/integration/batch/job_canceller.test.js index f7c9be33..9a9bf873 100644 --- a/test/integration/batch/job_canceller.test.js +++ b/test/integration/batch/job_canceller.test.js @@ -85,6 +85,14 @@ function createWadusJob(query) { describe('job canceller', function() { var jobCanceller = new JobCanceller(userDatabaseMetadataService); + after(function (done) { + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); + }); + }); + + it('.cancel() should cancel a job', function (done) { var job = createWadusJob('select pg_sleep(1)'); diff --git a/test/integration/batch/job_runner.test.js b/test/integration/batch/job_runner.test.js index 39150b44..a6435db8 100644 --- a/test/integration/batch/job_runner.test.js +++ b/test/integration/batch/job_runner.test.js @@ -53,6 +53,13 @@ var JOB = { describe('job runner', function() { var jobRunner = new JobRunner(jobService, jobQueue, queryRunner, statsdClient); + after(function (done) { + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); + }); + }); + it('.run() should run a job', function (done) { jobService.create(JOB, function (err, job) { if (err) { diff --git a/test/integration/batch/job_service.test.js b/test/integration/batch/job_service.test.js index 44431e92..cd48e4d0 100644 --- a/test/integration/batch/job_service.test.js +++ b/test/integration/batch/job_service.test.js @@ -45,6 +45,10 @@ var JOB = { host: HOST }; +function createWadusDataJob() { + return JSON.parse(JSON.stringify(JOB)); +} + // sets job to running, run its query and returns inmediatly (don't wait for query finishes) // in order to test query cancelation/draining function runQueryHelper(job, callback) { @@ -82,8 +86,15 @@ function runQueryHelper(job, callback) { describe('job service', function() { var jobService = new JobService(jobBackend, jobCanceller); + after(function (done) { + metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { + if (err) { return done(err); } + metadataBackend.redisCmd(5, 'DEL', keys, done); + }); + }); + it('.get() should return a job', function (done) { - jobService.create(JOB, function (err, jobCreated) { + jobService.create(createWadusDataJob(), function (err, jobCreated) { if (err) { return done(err); } @@ -108,7 +119,7 @@ describe('job service', function() { }); it('.create() should persist a job', function (done) { - jobService.create(JOB, function (err, jobCreated) { + jobService.create(createWadusDataJob(), function (err, jobCreated) { if (err) { return done(err); } @@ -120,7 +131,7 @@ describe('job service', function() { }); it('.create() should return error with invalid job data', function (done) { - var job = JSON.parse(JSON.stringify(JOB)); + var job = createWadusDataJob(); delete job.query; @@ -132,7 +143,7 @@ describe('job service', function() { }); it('.list() should return a list of user\'s jobs', function (done) { - jobService.create(JOB, function (err, jobCreated) { + jobService.create(createWadusDataJob(), function (err, jobCreated) { if (err) { return done(err); } @@ -164,7 +175,7 @@ describe('job service', function() { }); it('.update() should update a job', function (done) { - jobService.create(JOB, function (err, jobCreated) { + jobService.create(createWadusDataJob(), function (err, jobCreated) { if (err) { return done(err); } @@ -184,7 +195,8 @@ describe('job service', function() { }); it('.update() should return error when updates a nonexistent job', function (done) { - var job = JSON.parse(JSON.stringify(JOB)); + var job = createWadusDataJob(); + job.job_id = 'wadus_job_id'; jobService.update(job, function (err) { @@ -196,11 +208,8 @@ describe('job service', function() { }); it('.cancel() should cancel a running job', function (done) { - var job = { - user: USER, - query: 'select pg_sleep(3)', - host: HOST - }; + var job = createWadusDataJob(); + job.query = 'select pg_sleep(3)'; jobService.create(job, function (err, job) { if (err) { @@ -235,11 +244,8 @@ describe('job service', function() { }); it('.drain() should draing a running job', function (done) { - var job = { - user: USER, - query: 'select pg_sleep(3)', - host: HOST - }; + var job = createWadusDataJob(); + job.query = 'select pg_sleep(3)'; jobService.create(job, function (err, job) { if (err) { From 7fd5e4221330ad87a2ed7302403b9d229d379560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 22 Jul 2016 14:41:26 +0200 Subject: [PATCH 14/16] Removed timeouts for batch tests and skipped again a failing one in job callback template --- test/acceptance/job.callback-template.test.js | 2 +- test/acceptance/job.use-case-1.test.js | 2 -- test/acceptance/job.use-case-10.test.js | 2 -- test/acceptance/job.use-case-2.test.js | 2 -- test/acceptance/job.use-case-3.test.js | 2 -- test/acceptance/job.use-case-4.test.js | 2 -- test/acceptance/job.use-case-5.test.js | 2 -- test/acceptance/job.use-case-6.test.js | 2 -- test/acceptance/job.use-case-7.test.js | 2 -- test/acceptance/job.use-case-8.test.js | 2 -- test/acceptance/job.use-case-9.test.js | 2 -- 11 files changed, 1 insertion(+), 21 deletions(-) diff --git a/test/acceptance/job.callback-template.test.js b/test/acceptance/job.callback-template.test.js index 365949b4..f0a5dfd1 100644 --- a/test/acceptance/job.callback-template.test.js +++ b/test/acceptance/job.callback-template.test.js @@ -109,7 +109,7 @@ describe('Batch API callback templates', function () { }); }); - describe('should use templates for error_message and job_id onerror callback', function () { + describe.skip('should use templates for error_message and job_id onerror callback', function () { var jobResponse; before(function(done) { getQueryResult('create table test_batch_errors (job_id text, error_message text)', function(err) { diff --git a/test/acceptance/job.use-case-1.test.js b/test/acceptance/job.use-case-1.test.js index 1ede0933..e8940e36 100644 --- a/test/acceptance/job.use-case-1.test.js +++ b/test/acceptance/job.use-case-1.test.js @@ -28,8 +28,6 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 1: cancel and modify a done job', function () { - this.timeout(30000); - var batch = batchFactory(metadataBackend, redisConfig); before(function (done) { diff --git a/test/acceptance/job.use-case-10.test.js b/test/acceptance/job.use-case-10.test.js index 591e040c..85f9f29e 100644 --- a/test/acceptance/job.use-case-10.test.js +++ b/test/acceptance/job.use-case-10.test.js @@ -28,8 +28,6 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 10: cancel and modify a done multiquery job', function () { - this.timeout(30000); - var batch = batchFactory(metadataBackend, redisConfig); before(function (done) { diff --git a/test/acceptance/job.use-case-2.test.js b/test/acceptance/job.use-case-2.test.js index 11872e80..7c3c2112 100644 --- a/test/acceptance/job.use-case-2.test.js +++ b/test/acceptance/job.use-case-2.test.js @@ -28,8 +28,6 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 2: cancel a running job', function() { - this.timeout(30000); - var batch = batchFactory(metadataBackend, redisConfig); before(function (done) { diff --git a/test/acceptance/job.use-case-3.test.js b/test/acceptance/job.use-case-3.test.js index 7b0eebaf..604d1c2f 100644 --- a/test/acceptance/job.use-case-3.test.js +++ b/test/acceptance/job.use-case-3.test.js @@ -28,8 +28,6 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 3: cancel a pending job', function() { - this.timeout(30000); - var batch = batchFactory(metadataBackend, redisConfig); before(function (done) { diff --git a/test/acceptance/job.use-case-4.test.js b/test/acceptance/job.use-case-4.test.js index 5538c29d..dd387ed3 100644 --- a/test/acceptance/job.use-case-4.test.js +++ b/test/acceptance/job.use-case-4.test.js @@ -28,8 +28,6 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 4: modify a pending job', function() { - this.timeout(30000); - var batch = batchFactory(metadataBackend, redisConfig); before(function (done) { diff --git a/test/acceptance/job.use-case-5.test.js b/test/acceptance/job.use-case-5.test.js index a9763b47..43bcc100 100644 --- a/test/acceptance/job.use-case-5.test.js +++ b/test/acceptance/job.use-case-5.test.js @@ -28,8 +28,6 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 5: modify a running job', function() { - this.timeout(30000); - var batch = batchFactory(metadataBackend, redisConfig); before(function (done) { diff --git a/test/acceptance/job.use-case-6.test.js b/test/acceptance/job.use-case-6.test.js index 7580972e..a9439ef7 100644 --- a/test/acceptance/job.use-case-6.test.js +++ b/test/acceptance/job.use-case-6.test.js @@ -28,8 +28,6 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 6: modify a done job', function() { - this.timeout(30000); - var batch = batchFactory(metadataBackend, redisConfig); before(function (done) { diff --git a/test/acceptance/job.use-case-7.test.js b/test/acceptance/job.use-case-7.test.js index b5564b43..fb12925d 100644 --- a/test/acceptance/job.use-case-7.test.js +++ b/test/acceptance/job.use-case-7.test.js @@ -28,8 +28,6 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 7: cancel a job with quotes', function() { - this.timeout(30000); - var batch = batchFactory(metadataBackend, redisConfig); before(function (done) { diff --git a/test/acceptance/job.use-case-8.test.js b/test/acceptance/job.use-case-8.test.js index 021959fa..8e03cee7 100644 --- a/test/acceptance/job.use-case-8.test.js +++ b/test/acceptance/job.use-case-8.test.js @@ -28,8 +28,6 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 8: cancel a running multiquery job', function() { - this.timeout(30000); - var batch = batchFactory(metadataBackend, redisConfig); before(function (done) { diff --git a/test/acceptance/job.use-case-9.test.js b/test/acceptance/job.use-case-9.test.js index 4ab511c1..e5f0c043 100644 --- a/test/acceptance/job.use-case-9.test.js +++ b/test/acceptance/job.use-case-9.test.js @@ -28,8 +28,6 @@ var metadataBackend = require('cartodb-redis')(redisConfig); var batchFactory = require('../../batch'); describe('Use case 9: modify a pending multiquery job', function() { - this.timeout(30000); - var batch = batchFactory(metadataBackend, redisConfig); before(function (done) { From 587ab6a5d9426ce9c9fcf260ef04e36e684bbac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 22 Jul 2016 17:05:01 +0200 Subject: [PATCH 15/16] Extratcs redis clean method for batch keys --- test/acceptance/batch.test.js | 6 ++---- test/acceptance/job.callback-template.test.js | 6 ++---- test/acceptance/job.fallback.test.js | 6 ++---- test/acceptance/job.query.limit.test.js | 9 ++------- test/acceptance/job.test.js | 8 ++------ test/acceptance/job.timing.test.js | 6 ++---- test/acceptance/job.use-case-1.test.js | 7 ++----- test/acceptance/job.use-case-10.test.js | 6 ++---- test/acceptance/job.use-case-2.test.js | 6 ++---- test/acceptance/job.use-case-3.test.js | 6 ++---- test/acceptance/job.use-case-4.test.js | 6 ++---- test/acceptance/job.use-case-5.test.js | 6 ++---- test/acceptance/job.use-case-6.test.js | 6 ++---- test/acceptance/job.use-case-7.test.js | 6 ++---- test/acceptance/job.use-case-8.test.js | 6 ++---- test/acceptance/job.use-case-9.test.js | 6 ++---- .../batch/batch.multiquery.test.js | 6 ++---- test/integration/batch/job_backend.test.js | 8 ++------ test/integration/batch/job_canceller.test.js | 8 ++------ test/integration/batch/job_runner.test.js | 7 ++----- test/integration/batch/job_service.test.js | 7 ++----- test/support/redis_utils.js | 20 +++++++++++++++++++ 22 files changed, 62 insertions(+), 96 deletions(-) create mode 100644 test/support/redis_utils.js diff --git a/test/acceptance/batch.test.js b/test/acceptance/batch.test.js index e17e5af0..d1cffa68 100644 --- a/test/acceptance/batch.test.js +++ b/test/acceptance/batch.test.js @@ -1,4 +1,5 @@ var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var _ = require('underscore'); var RedisPool = require('redis-mpool'); var queue = require('queue-async'); @@ -41,10 +42,7 @@ describe('batch module', function() { after(function (done) { batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); function createJob(sql, done) { diff --git a/test/acceptance/job.callback-template.test.js b/test/acceptance/job.callback-template.test.js index f0a5dfd1..39cfc593 100644 --- a/test/acceptance/job.callback-template.test.js +++ b/test/acceptance/job.callback-template.test.js @@ -1,6 +1,7 @@ require('../helper'); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var app = require(global.settings.app_root + '/app/app')(); var querystring = require('qs'); var redisConfig = { @@ -103,10 +104,7 @@ describe('Batch API callback templates', function () { after(function (done) { batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); describe.skip('should use templates for error_message and job_id onerror callback', function () { diff --git a/test/acceptance/job.fallback.test.js b/test/acceptance/job.fallback.test.js index 93c65bf8..71e6e8e6 100644 --- a/test/acceptance/job.fallback.test.js +++ b/test/acceptance/job.fallback.test.js @@ -1,6 +1,7 @@ require('../helper'); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var app = require(global.settings.app_root + '/app/app')(); var querystring = require('qs'); var redisConfig = { @@ -44,10 +45,7 @@ describe('Batch API fallback job', function () { after(function (done) { batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); describe('"onsuccess" on first query should be triggered', function () { diff --git a/test/acceptance/job.query.limit.test.js b/test/acceptance/job.query.limit.test.js index dfb96adc..8c34a293 100644 --- a/test/acceptance/job.query.limit.test.js +++ b/test/acceptance/job.query.limit.test.js @@ -14,7 +14,7 @@ */ require('../helper'); var JobController = require('../../app/controllers/job_controller'); - +var redisUtils = require('../support/redis_utils'); var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); var querystring = require('qs'); @@ -44,12 +44,7 @@ describe('job query limit', function() { } after(function (done) { - // batch services is not activate, so we need empty the queue to avoid unexpected - // behaviour in further tests - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); it('POST /api/v2/sql/job with a invalid query size should respond with 400 query too long', function (done){ diff --git a/test/acceptance/job.test.js b/test/acceptance/job.test.js index c5aaa461..7ee71c00 100644 --- a/test/acceptance/job.test.js +++ b/test/acceptance/job.test.js @@ -16,6 +16,7 @@ require('../helper'); var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var querystring = require('querystring'); var metadataBackend = require('cartodb-redis')({ host: global.settings.redis_host, @@ -29,12 +30,7 @@ describe('job module', function() { var job = {}; after(function (done) { - // batch services is not activate, so we need empty the queue to avoid unexpected - // behaviour in further tests - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); it('POST /api/v2/sql/job should respond with 200 and the created job', function (done){ diff --git a/test/acceptance/job.timing.test.js b/test/acceptance/job.timing.test.js index da54c7de..610ae446 100644 --- a/test/acceptance/job.timing.test.js +++ b/test/acceptance/job.timing.test.js @@ -1,6 +1,7 @@ require('../helper'); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var app = require(global.settings.app_root + '/app/app')(); var querystring = require('qs'); var redisConfig = { @@ -80,10 +81,7 @@ describe('Batch API query timing', function () { after(function (done) { batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); describe('should report start and end time for each query with fallback queries', function () { diff --git a/test/acceptance/job.use-case-1.test.js b/test/acceptance/job.use-case-1.test.js index e8940e36..53ee9596 100644 --- a/test/acceptance/job.use-case-1.test.js +++ b/test/acceptance/job.use-case-1.test.js @@ -16,6 +16,7 @@ require('../helper'); var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var querystring = require('querystring'); var redisConfig = { host: global.settings.redis_host, @@ -37,11 +38,7 @@ describe('Use case 1: cancel and modify a done job', function () { after(function (done) { batch.stop(); - - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); var doneJob = {}; diff --git a/test/acceptance/job.use-case-10.test.js b/test/acceptance/job.use-case-10.test.js index 85f9f29e..df80dff8 100644 --- a/test/acceptance/job.use-case-10.test.js +++ b/test/acceptance/job.use-case-10.test.js @@ -16,6 +16,7 @@ require('../helper'); var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var querystring = require('querystring'); var redisConfig = { host: global.settings.redis_host, @@ -37,10 +38,7 @@ describe('Use case 10: cancel and modify a done multiquery job', function () { after(function (done) { batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); var doneJob = {}; diff --git a/test/acceptance/job.use-case-2.test.js b/test/acceptance/job.use-case-2.test.js index 7c3c2112..8896bbaa 100644 --- a/test/acceptance/job.use-case-2.test.js +++ b/test/acceptance/job.use-case-2.test.js @@ -16,6 +16,7 @@ require('../helper'); var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var querystring = require('querystring'); var redisConfig = { host: global.settings.redis_host, @@ -37,10 +38,7 @@ describe('Use case 2: cancel a running job', function() { after(function (done) { batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); var runningJob = {}; diff --git a/test/acceptance/job.use-case-3.test.js b/test/acceptance/job.use-case-3.test.js index 604d1c2f..dc35346c 100644 --- a/test/acceptance/job.use-case-3.test.js +++ b/test/acceptance/job.use-case-3.test.js @@ -16,6 +16,7 @@ require('../helper'); var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var querystring = require('querystring'); var redisConfig = { host: global.settings.redis_host, @@ -37,10 +38,7 @@ describe('Use case 3: cancel a pending job', function() { after(function (done) { batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); var runningJob = {}; diff --git a/test/acceptance/job.use-case-4.test.js b/test/acceptance/job.use-case-4.test.js index dd387ed3..570e5358 100644 --- a/test/acceptance/job.use-case-4.test.js +++ b/test/acceptance/job.use-case-4.test.js @@ -16,6 +16,7 @@ require('../helper'); var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var querystring = require('querystring'); var redisConfig = { host: global.settings.redis_host, @@ -37,10 +38,7 @@ describe('Use case 4: modify a pending job', function() { after(function (done) { batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); var runningJob = {}; diff --git a/test/acceptance/job.use-case-5.test.js b/test/acceptance/job.use-case-5.test.js index 43bcc100..31c55874 100644 --- a/test/acceptance/job.use-case-5.test.js +++ b/test/acceptance/job.use-case-5.test.js @@ -16,6 +16,7 @@ require('../helper'); var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var querystring = require('querystring'); var redisConfig = { host: global.settings.redis_host, @@ -37,10 +38,7 @@ describe('Use case 5: modify a running job', function() { after(function (done) { batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); var runningJob = {}; diff --git a/test/acceptance/job.use-case-6.test.js b/test/acceptance/job.use-case-6.test.js index a9439ef7..b6116d4b 100644 --- a/test/acceptance/job.use-case-6.test.js +++ b/test/acceptance/job.use-case-6.test.js @@ -16,6 +16,7 @@ require('../helper'); var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var querystring = require('querystring'); var redisConfig = { host: global.settings.redis_host, @@ -37,10 +38,7 @@ describe('Use case 6: modify a done job', function() { after(function (done) { batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); var doneJob = {}; diff --git a/test/acceptance/job.use-case-7.test.js b/test/acceptance/job.use-case-7.test.js index fb12925d..e9364ea4 100644 --- a/test/acceptance/job.use-case-7.test.js +++ b/test/acceptance/job.use-case-7.test.js @@ -16,6 +16,7 @@ require('../helper'); var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var querystring = require('querystring'); var redisConfig = { host: global.settings.redis_host, @@ -37,10 +38,7 @@ describe('Use case 7: cancel a job with quotes', function() { after(function (done) { batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); var runningJob = {}; diff --git a/test/acceptance/job.use-case-8.test.js b/test/acceptance/job.use-case-8.test.js index 8e03cee7..109f9f73 100644 --- a/test/acceptance/job.use-case-8.test.js +++ b/test/acceptance/job.use-case-8.test.js @@ -16,6 +16,7 @@ require('../helper'); var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var querystring = require('querystring'); var redisConfig = { host: global.settings.redis_host, @@ -37,10 +38,7 @@ describe('Use case 8: cancel a running multiquery job', function() { after(function (done) { batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); var runningJob = {}; diff --git a/test/acceptance/job.use-case-9.test.js b/test/acceptance/job.use-case-9.test.js index e5f0c043..8e3212df 100644 --- a/test/acceptance/job.use-case-9.test.js +++ b/test/acceptance/job.use-case-9.test.js @@ -16,6 +16,7 @@ require('../helper'); var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); +var redisUtils = require('../support/redis_utils'); var querystring = require('querystring'); var redisConfig = { host: global.settings.redis_host, @@ -37,10 +38,7 @@ describe('Use case 9: modify a pending multiquery job', function() { after(function (done) { batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); var runningJob = {}; diff --git a/test/integration/batch/batch.multiquery.test.js b/test/integration/batch/batch.multiquery.test.js index 84fdb2fc..11583035 100644 --- a/test/integration/batch/batch.multiquery.test.js +++ b/test/integration/batch/batch.multiquery.test.js @@ -2,6 +2,7 @@ require('../../helper'); var assert = require('../../support/assert'); +var redisUtils = require('../../support/redis_utils'); var queue = require('queue-async'); var redisConfig = { @@ -78,10 +79,7 @@ describe('batch multiquery', function() { after(function (done) { batch.removeAllListeners(); batch.stop(); - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); it('should perform one multiquery job with two queries', function (done) { diff --git a/test/integration/batch/job_backend.test.js b/test/integration/batch/job_backend.test.js index 9f81c41c..bdd0b1f2 100644 --- a/test/integration/batch/job_backend.test.js +++ b/test/integration/batch/job_backend.test.js @@ -5,7 +5,7 @@ require('../../helper'); var BATCH_SOURCE = '../../../batch/'; var assert = require('../../support/assert'); - +var redisUtils = require('../../support/redis_utils'); var _ = require('underscore'); var RedisPool = require('redis-mpool'); @@ -16,7 +16,6 @@ var JobPublisher = require(BATCH_SOURCE + 'job_publisher'); var JobFactory = require(BATCH_SOURCE + 'models/job_factory'); var jobStatus = require(BATCH_SOURCE + 'job_status'); - var redisConfig = { host: global.settings.redis_host, port: global.settings.redis_port, @@ -48,10 +47,7 @@ describe('job backend', function() { var jobBackend = new JobBackend(metadataBackend, jobQueue, userIndexer); after(function (done) { - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); it('.create() should persist a job', function (done) { diff --git a/test/integration/batch/job_canceller.test.js b/test/integration/batch/job_canceller.test.js index 9a9bf873..5d704567 100644 --- a/test/integration/batch/job_canceller.test.js +++ b/test/integration/batch/job_canceller.test.js @@ -5,7 +5,7 @@ require('../../helper'); var BATCH_SOURCE = '../../../batch/'; var assert = require('../../support/assert'); - +var redisUtils = require('../../support/redis_utils'); var _ = require('underscore'); var RedisPool = require('redis-mpool'); @@ -86,13 +86,9 @@ describe('job canceller', function() { var jobCanceller = new JobCanceller(userDatabaseMetadataService); after(function (done) { - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); - it('.cancel() should cancel a job', function (done) { var job = createWadusJob('select pg_sleep(1)'); diff --git a/test/integration/batch/job_runner.test.js b/test/integration/batch/job_runner.test.js index a6435db8..0fbbee4c 100644 --- a/test/integration/batch/job_runner.test.js +++ b/test/integration/batch/job_runner.test.js @@ -5,7 +5,7 @@ require('../../helper'); var BATCH_SOURCE = '../../../batch/'; var assert = require('../../support/assert'); - +var redisUtils = require('../../support/redis_utils'); var _ = require('underscore'); var RedisPool = require('redis-mpool'); @@ -54,10 +54,7 @@ describe('job runner', function() { var jobRunner = new JobRunner(jobService, jobQueue, queryRunner, statsdClient); after(function (done) { - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); it('.run() should run a job', function (done) { diff --git a/test/integration/batch/job_service.test.js b/test/integration/batch/job_service.test.js index cd48e4d0..60b131e0 100644 --- a/test/integration/batch/job_service.test.js +++ b/test/integration/batch/job_service.test.js @@ -5,7 +5,7 @@ require('../../helper'); var BATCH_SOURCE = '../../../batch/'; var assert = require('../../support/assert'); - +var redisUtils = require('../../support/redis_utils'); var _ = require('underscore'); var RedisPool = require('redis-mpool'); @@ -87,10 +87,7 @@ describe('job service', function() { var jobService = new JobService(jobBackend, jobCanceller); after(function (done) { - metadataBackend.redisCmd(5, 'KEYS', [ 'batch:*'], function (err, keys) { - if (err) { return done(err); } - metadataBackend.redisCmd(5, 'DEL', keys, done); - }); + redisUtils.clean('batch:*', done); }); it('.get() should return a job', function (done) { diff --git a/test/support/redis_utils.js b/test/support/redis_utils.js new file mode 100644 index 00000000..ec0a8043 --- /dev/null +++ b/test/support/redis_utils.js @@ -0,0 +1,20 @@ +'use strict'; + +var redisConfig = { + host: global.settings.redis_host, + port: global.settings.redis_port, + max: global.settings.redisPool, + idleTimeoutMillis: global.settings.redisIdleTimeoutMillis, + reapIntervalMillis: global.settings.redisReapIntervalMillis +}; +var metadataBackend = require('cartodb-redis')(redisConfig); + +module.exports.clean = function clean(pattern, callback) { + metadataBackend.redisCmd(5, 'KEYS', [ pattern ], function (err, keys) { + if (err) { + return callback(err); + } + + metadataBackend.redisCmd(5, 'DEL', keys, callback); + }); +}; From f3f45bb4e2e2e8f5ecd94563f281b0edf73c9d0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 22 Jul 2016 17:11:21 +0200 Subject: [PATCH 16/16] Fixed lint issues --- test/acceptance/job.query.limit.test.js | 7 ------- test/acceptance/job.test.js | 7 ------- 2 files changed, 14 deletions(-) diff --git a/test/acceptance/job.query.limit.test.js b/test/acceptance/job.query.limit.test.js index 8c34a293..5b24fde4 100644 --- a/test/acceptance/job.query.limit.test.js +++ b/test/acceptance/job.query.limit.test.js @@ -18,13 +18,6 @@ var redisUtils = require('../support/redis_utils'); var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); var querystring = require('qs'); -var metadataBackend = require('cartodb-redis')({ - host: global.settings.redis_host, - port: global.settings.redis_port, - max: global.settings.redisPool, - idleTimeoutMillis: global.settings.redisIdleTimeoutMillis, - reapIntervalMillis: global.settings.redisReapIntervalMillis -}); function payload(query) { return JSON.stringify({query: query}); diff --git a/test/acceptance/job.test.js b/test/acceptance/job.test.js index 7ee71c00..39754a71 100644 --- a/test/acceptance/job.test.js +++ b/test/acceptance/job.test.js @@ -18,13 +18,6 @@ var app = require(global.settings.app_root + '/app/app')(); var assert = require('../support/assert'); var redisUtils = require('../support/redis_utils'); var querystring = require('querystring'); -var metadataBackend = require('cartodb-redis')({ - host: global.settings.redis_host, - port: global.settings.redis_port, - max: global.settings.redisPool, - idleTimeoutMillis: global.settings.redisIdleTimeoutMillis, - reapIntervalMillis: global.settings.redisReapIntervalMillis -}); describe('job module', function() { var job = {};