CartoDB-SQL-API/test/integration/batch/scheduler.js

176 lines
5.0 KiB
JavaScript
Raw Normal View History

'use strict';
require('../../helper');
var assert = require('../../support/assert');
var Scheduler = require('../../../batch/scheduler/scheduler');
var FixedCapacity = require('../../../batch/scheduler/capacity/fixed');
describe('scheduler', function() {
2016-10-21 17:10:17 +08:00
var USER_A = 'userA';
var USER_B = 'userB';
var USER_C = 'userC';
function TaskRunner(userTasks) {
this.results = [];
this.userTasks = userTasks;
}
TaskRunner.prototype.run = function(user, callback) {
this.results.push(user);
this.userTasks[user]--;
2016-10-21 02:16:57 +08:00
setTimeout(function() {
return callback(null, this.userTasks[user] === 0);
}.bind(this), 50);
};
// simulate one by one or infinity capacity
var capacities = [new FixedCapacity(1), new FixedCapacity(2), new FixedCapacity(Infinity)];
capacities.forEach(function(capacity) {
2016-10-21 02:16:57 +08:00
it('regression #1', function (done) {
2016-10-21 02:16:57 +08:00
var taskRunner = new TaskRunner({
userA: 2,
userB: 2
});
var scheduler = new Scheduler(capacity, taskRunner);
2016-10-21 17:10:17 +08:00
scheduler.add(USER_A);
scheduler.add(USER_B);
2016-10-21 02:16:57 +08:00
scheduler.on('done', function() {
var results = taskRunner.results;
assert.equal(results.length, 4);
2016-10-21 17:10:17 +08:00
assert.equal(results[0], USER_A);
assert.equal(results[1], USER_B);
assert.equal(results[2], USER_A);
assert.equal(results[3], USER_B);
2016-10-21 02:16:57 +08:00
return done();
});
scheduler.schedule();
});
it('regression #2', function (done) {
var taskRunner = new TaskRunner({
userA: 2,
userB: 2,
userC: 2,
userD: 1
});
var scheduler = new Scheduler(capacity, taskRunner);
2016-10-21 17:10:17 +08:00
scheduler.add(USER_A);
scheduler.add(USER_B);
scheduler.on('done', function() {
var results = taskRunner.results;
assert.equal(results.length, 7);
2016-10-21 17:10:17 +08:00
assert.equal(results[0], USER_A);
assert.equal(results[1], USER_B);
assert.equal(results[2], USER_C);
assert.equal(results[3], 'userD');
2016-10-21 17:10:17 +08:00
assert.equal(results[4], USER_A);
assert.equal(results[5], USER_B);
assert.equal(results[6], USER_C);
return done();
});
setTimeout(function() {
2016-10-21 17:10:17 +08:00
scheduler.add(USER_C);
}, 10);
setTimeout(function() {
scheduler.add('userD');
}, 20);
scheduler.schedule();
});
it('should run tasks', function (done) {
var taskRunner = new TaskRunner({
userA: 1
});
var scheduler = new Scheduler(capacity, taskRunner);
2016-10-21 17:10:17 +08:00
scheduler.add(USER_A);
scheduler.on('done', function() {
var results = taskRunner.results;
assert.equal(results.length, 1);
2016-10-21 17:10:17 +08:00
assert.equal(results[0], USER_A);
return done();
});
scheduler.schedule();
});
it('should run tasks for different users', function (done) {
var taskRunner = new TaskRunner({
userA: 1,
userB: 1,
userC: 1
});
var scheduler = new Scheduler(capacity, taskRunner);
2016-10-21 17:10:17 +08:00
scheduler.add(USER_A);
scheduler.add(USER_B);
scheduler.add(USER_C);
scheduler.on('done', function() {
var results = taskRunner.results;
assert.equal(results.length, 3);
2016-10-21 17:10:17 +08:00
assert.equal(results[0], USER_A);
assert.equal(results[1], USER_B);
assert.equal(results[2], USER_C);
return done();
});
scheduler.schedule();
});
it('should be fair when scheduling tasks', function (done) {
var taskRunner = new TaskRunner({
userA: 3,
userB: 2,
userC: 1
});
var scheduler = new Scheduler(capacity, taskRunner);
2016-10-21 17:10:17 +08:00
scheduler.add(USER_A);
scheduler.add(USER_A);
scheduler.add(USER_A);
scheduler.add(USER_B);
scheduler.add(USER_B);
scheduler.add(USER_C);
scheduler.on('done', function() {
var results = taskRunner.results;
assert.equal(results.length, 6);
2016-10-21 17:10:17 +08:00
assert.equal(results[0], USER_A);
assert.equal(results[1], USER_B);
assert.equal(results[2], USER_C);
assert.equal(results[3], USER_A);
assert.equal(results[4], USER_B);
assert.equal(results[5], USER_A);
return done();
});
scheduler.schedule();
});
});
});