2015-12-23 06:13:33 +08:00
|
|
|
var _ = require('underscore');
|
|
|
|
var queue = require('queue-async');
|
2016-01-08 18:32:01 +08:00
|
|
|
var Batch = require('../../batch');
|
|
|
|
// var Batch = require('../../batch/batch');
|
2015-12-16 22:57:58 +08:00
|
|
|
var JobPublisher = require('../../batch/job_publisher');
|
2015-12-29 17:19:10 +08:00
|
|
|
var JobQueue = require('../../batch/job_queue');
|
2015-12-25 00:42:49 +08:00
|
|
|
var UserIndexer = require('../../batch/user_indexer');
|
2015-12-22 02:57:10 +08:00
|
|
|
var JobBackend = require('../../batch/job_backend');
|
2015-12-17 01:13:48 +08:00
|
|
|
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
|
|
|
|
});
|
2015-12-09 07:02:08 +08:00
|
|
|
|
2015-12-29 22:46:04 +08:00
|
|
|
describe('batch module', function() {
|
2015-12-23 03:12:10 +08:00
|
|
|
var dbInstance = 'localhost';
|
|
|
|
var username = 'vizzuality';
|
2015-12-29 17:19:10 +08:00
|
|
|
var jobQueue = new JobQueue(metadataBackend);
|
2015-12-23 03:12:10 +08:00
|
|
|
var jobPublisher = new JobPublisher();
|
2015-12-25 00:42:49 +08:00
|
|
|
var userIndexer = new UserIndexer(metadataBackend);
|
2015-12-29 17:19:10 +08:00
|
|
|
var jobBackend = new JobBackend(metadataBackend, jobQueue, jobPublisher, userIndexer);
|
2015-12-23 03:12:10 +08:00
|
|
|
var batch = new Batch(metadataBackend);
|
2015-12-14 17:36:16 +08:00
|
|
|
|
2015-12-23 03:12:10 +08:00
|
|
|
before(function () {
|
|
|
|
batch.start();
|
|
|
|
});
|
2015-12-16 22:57:58 +08:00
|
|
|
|
2016-01-06 00:42:28 +08:00
|
|
|
after(function () {
|
|
|
|
batch.stop();
|
|
|
|
});
|
|
|
|
|
2015-12-23 03:12:10 +08:00
|
|
|
function createJob(sql, done) {
|
2015-12-25 00:42:49 +08:00
|
|
|
jobBackend.create(username, sql, dbInstance, function (err, job) {
|
2015-12-16 22:57:58 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2015-12-17 01:13:48 +08:00
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
done(null, job);
|
2015-12-23 03:12:10 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should perform job with select', function (done) {
|
|
|
|
createJob('select * from private_table', function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2015-12-24 00:29:11 +08:00
|
|
|
batch.on('job:done', function (job_id) {
|
|
|
|
if (job_id === job.job_id) {
|
2015-12-23 03:12:10 +08:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-12-17 01:13:48 +08:00
|
|
|
|
2015-12-23 03:12:10 +08:00
|
|
|
it('should perform job with select into', function (done) {
|
|
|
|
createJob('select * into batch_test_table from (select * from private_table) as job', function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2015-12-17 01:13:48 +08:00
|
|
|
|
2015-12-24 00:29:11 +08:00
|
|
|
batch.on('job:done', function (job_id) {
|
|
|
|
if (job_id === job.job_id) {
|
2015-12-23 03:12:10 +08:00
|
|
|
done();
|
|
|
|
}
|
2015-12-17 01:13:48 +08:00
|
|
|
});
|
|
|
|
});
|
2015-12-23 03:12:10 +08:00
|
|
|
});
|
2015-12-17 01:13:48 +08:00
|
|
|
|
2015-12-23 03:12:10 +08:00
|
|
|
it('should perform job swith select from result table', function (done) {
|
|
|
|
createJob('select * from batch_test_table', function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2015-12-24 00:29:11 +08:00
|
|
|
batch.on('job:done', function (job_id) {
|
|
|
|
if (job_id === job.job_id) {
|
2015-12-22 18:02:16 +08:00
|
|
|
done();
|
|
|
|
}
|
2015-12-22 02:57:10 +08:00
|
|
|
});
|
2015-12-23 03:12:10 +08:00
|
|
|
});
|
2015-12-09 07:02:08 +08:00
|
|
|
});
|
2015-12-23 03:12:10 +08:00
|
|
|
|
2015-12-23 06:13:33 +08:00
|
|
|
it('should perform all job enqueued', function (done) {
|
|
|
|
var jobs = [
|
|
|
|
'select * from private_table',
|
|
|
|
'select * from private_table',
|
|
|
|
'select * from private_table',
|
|
|
|
'select * from private_table',
|
|
|
|
'select * from private_table',
|
|
|
|
'select * from private_table',
|
|
|
|
'select * from private_table',
|
|
|
|
'select * from private_table',
|
|
|
|
'select * from private_table',
|
|
|
|
'select * from private_table'
|
|
|
|
];
|
|
|
|
|
|
|
|
var jobsQueue = queue(jobs.length);
|
|
|
|
|
|
|
|
jobs.forEach(function(job) {
|
|
|
|
jobsQueue.defer(createJob, job);
|
|
|
|
});
|
|
|
|
|
|
|
|
jobsQueue.awaitAll(function (err, jobsCreated) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var jobsDone = 0;
|
|
|
|
|
2015-12-24 00:29:11 +08:00
|
|
|
batch.on('job:done', function (job_id) {
|
2015-12-23 06:13:33 +08:00
|
|
|
_.find(jobsCreated, function(job) {
|
2015-12-24 00:29:11 +08:00
|
|
|
if (job_id === job.job_id) {
|
2015-12-23 06:13:33 +08:00
|
|
|
jobsDone += 1;
|
2015-12-23 21:54:54 +08:00
|
|
|
if (jobsDone === jobs.length) {
|
|
|
|
done();
|
|
|
|
}
|
2015-12-23 06:13:33 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-12-09 07:02:08 +08:00
|
|
|
});
|