CartoDB-SQL-API/test/acceptance/batch.test.js

127 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-12-23 06:13:33 +08:00
var _ = require('underscore');
var queue = require('queue-async');
var Batch = require('../../batch');
// var Batch = require('../../batch/batch');
var JobPublisher = require('../../batch/job_publisher');
var JobQueue = require('../../batch/job_queue');
var UserIndexer = require('../../batch/user_indexer');
var JobBackend = require('../../batch/job_backend');
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
describe('batch module', function() {
var dbInstance = 'localhost';
var username = 'vizzuality';
var jobQueue = new JobQueue(metadataBackend);
var jobPublisher = new JobPublisher();
var userIndexer = new UserIndexer(metadataBackend);
var jobBackend = new JobBackend(metadataBackend, jobQueue, jobPublisher, userIndexer);
var batch = new Batch(metadataBackend);
before(function () {
batch.start();
});
after(function () {
batch.stop();
});
function createJob(sql, done) {
jobBackend.create(username, sql, dbInstance, function (err, job) {
if (err) {
return done(err);
}
done(null, job);
});
}
it('should perform job with select', function (done) {
createJob('select * from private_table', function (err, job) {
if (err) {
return done(err);
}
batch.on('job:done', function (job_id) {
if (job_id === job.job_id) {
done();
}
});
});
});
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);
}
batch.on('job:done', function (job_id) {
if (job_id === job.job_id) {
done();
}
});
});
});
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);
}
batch.on('job:done', function (job_id) {
if (job_id === job.job_id) {
done();
}
});
});
2015-12-09 07:02:08 +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;
batch.on('job:done', function (job_id) {
2015-12-23 06:13:33 +08:00
_.find(jobsCreated, function(job) {
if (job_id === job.job_id) {
2015-12-23 06:13:33 +08:00
jobsDone += 1;
if (jobsDone === jobs.length) {
done();
}
2015-12-23 06:13:33 +08:00
}
});
});
});
});
2015-12-09 07:02:08 +08:00
});