Fix condition to pick next candidate

This commit is contained in:
Raul Ochoa 2016-10-20 20:16:57 +02:00
parent aa69bcf34c
commit e4d54e9ab7
2 changed files with 35 additions and 3 deletions

View File

@ -142,13 +142,17 @@ Scheduler.prototype.acquire = function(callback) {
self.once('add', addListener); self.once('add', addListener);
} }
var isRunningAny = self.tasks.some(is(STATUS.RUNNING)); if (running.length >= capacity) {
if (isRunningAny || running.length >= capacity) {
debug('Waiting for slot'); debug('Waiting for slot');
return self.once('release', releaseListener); return self.once('release', releaseListener);
} }
var isRunningAny = self.tasks.some(is(STATUS.RUNNING));
var candidate = self.tasksTree.min(); var candidate = self.tasksTree.min();
if (isRunningAny && candidate === null) {
debug('Waiting for last task to finish');
return self.once('release', releaseListener);
}
return callback(null, candidate); return callback(null, candidate);
}); });

View File

@ -15,13 +15,41 @@ describe('scheduler', function() {
TaskRunner.prototype.run = function(user, callback) { TaskRunner.prototype.run = function(user, callback) {
this.results.push(user); this.results.push(user);
this.userTasks[user]--; this.userTasks[user]--;
return callback(null, this.userTasks[user] === 0); setTimeout(function() {
return callback(null, this.userTasks[user] === 0);
}.bind(this), 50);
}; };
// simulate one by one or infinity capacity // simulate one by one or infinity capacity
var capacities = [new FixedCapacity(1), new FixedCapacity(Infinity)]; var capacities = [new FixedCapacity(1), new FixedCapacity(Infinity)];
capacities.forEach(function(capacity) { capacities.forEach(function(capacity) {
it('regression', function (done) {
var taskRunner = new TaskRunner({
userA: 2,
userB: 2
});
var scheduler = new Scheduler(capacity, taskRunner);
scheduler.add('userA');
scheduler.add('userB');
scheduler.on('done', function() {
var results = taskRunner.results;
assert.equal(results.length, 4);
assert.equal(results[0], 'userA');
assert.equal(results[1], 'userB');
assert.equal(results[2], 'userA');
assert.equal(results[3], 'userB');
return done();
});
scheduler.schedule();
});
it('should run tasks', function (done) { it('should run tasks', function (done) {
var taskRunner = new TaskRunner({ var taskRunner = new TaskRunner({
userA: 1 userA: 1