Skip to content

Commit

Permalink
bug fix: default job priorities were mutated at every push (+ test)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienjoly committed Oct 6, 2017
1 parent 8c43c4d commit 5a63329
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions qyu.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const LOWEST_PRIO = 10;
const DEFAULT_QUEUE_OPTIONS = {
rateLimit: 1, // process no more than 1 job per second
statsInterval: 1000, // emit `stats` every second
};
}; // TODO: use or remove this constant

const DEFAULT_JOB_OPTIONS = {
priority: LOWEST_PRIO, // low job priority by default, when calling push()
Expand Down Expand Up @@ -154,7 +154,7 @@ class Qyu extends EventEmitter {
this.log.trace('Qyu:_processJob() ', { started: this.started, running: this.running });
if (this.started && this.running === 0) {
if (this.jobs.length) {
const priority = this.jobs.reduce((lowest, job) => Math.min(lowest, job.opts.priority), LOWEST_PRIO);
const priority = Math.min.apply(Math, this.jobs.map(job => job.opts.priority));
this.runningJob = this.jobs.find(job => job.opts.priority === priority);
this.running = 1;
this.log.debug('Qyu.runningJob = ', this.runningJob);
Expand All @@ -180,7 +180,7 @@ class Qyu extends EventEmitter {
this.jobs.push({
id: nextJobId++,
job,
opts: Object.assign(DEFAULT_JOB_OPTIONS, opts)
opts: Object.assign({}, DEFAULT_JOB_OPTIONS, opts)
});
this._processJob();
}
Expand Down
24 changes: 24 additions & 0 deletions test/priorities.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const assert = require('assert');
const helpers = require('./_helpers');
const qyu = require('../qyu');

describe('qyu job priorities', function() {

it('jobs must be run in order of priority', async function() {
const q = qyu(/*{ log: helpers.createLogger() }*/);
const jobs = [ helpers.makeSpyJob(), helpers.makeSpyJob(), helpers.makeSpyJob() ];
q.push(jobs[0], { priority: 8 }); // should be run third
q.push(jobs[1], { priority: 1 }); // should be run first
q.push(jobs[2], { priority: 7 }); // should be run second
await q.start();
await q.pause();
assert.deepEqual(jobs.map(job => job.done), [ false, true, false ]);
await q.start();
await q.pause();
assert.deepEqual(jobs.map(job => job.done), [ false, true, true ]);
await q.start();
await q.pause();
assert.deepEqual(jobs.map(job => job.done), [ true, true, true ]);
});

});

0 comments on commit 5a63329

Please sign in to comment.