From e4d54e9ab70d54540b29a01e6c246ae9f639a34a Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 20 Oct 2016 20:16:57 +0200 Subject: [PATCH] Fix condition to pick next candidate --- batch/scheduler/scheduler.js | 8 ++++++-- test/integration/batch/scheduler.js | 30 ++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/batch/scheduler/scheduler.js b/batch/scheduler/scheduler.js index 7ee10560..a7c8ae17 100644 --- a/batch/scheduler/scheduler.js +++ b/batch/scheduler/scheduler.js @@ -142,13 +142,17 @@ Scheduler.prototype.acquire = function(callback) { self.once('add', addListener); } - var isRunningAny = self.tasks.some(is(STATUS.RUNNING)); - if (isRunningAny || running.length >= capacity) { + if (running.length >= capacity) { debug('Waiting for slot'); return self.once('release', releaseListener); } + var isRunningAny = self.tasks.some(is(STATUS.RUNNING)); 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); }); diff --git a/test/integration/batch/scheduler.js b/test/integration/batch/scheduler.js index 4ec5a8f0..3a8658e8 100644 --- a/test/integration/batch/scheduler.js +++ b/test/integration/batch/scheduler.js @@ -15,13 +15,41 @@ describe('scheduler', function() { TaskRunner.prototype.run = function(user, callback) { this.results.push(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 var capacities = [new FixedCapacity(1), new FixedCapacity(Infinity)]; 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) { var taskRunner = new TaskRunner({ userA: 1