Skip to content

Commit

Permalink
Merge pull request #686 from cibernox/guard-against-undefined-processor
Browse files Browse the repository at this point in the history
Add guards to check that `TaskQueue`s have at least one processor
  • Loading branch information
dgeb authored Aug 20, 2019
2 parents 11a1428 + 066e2bd commit f19060b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/@orbit/core/src/task-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export default class TaskQueue implements Evented {
this._cancel();
this._tasks.shift();
let processor = this._processors.shift();
if (!processor.settled) {
if (processor !== undefined && !processor.settled) {
processor.reject(
e || new Error('Processing cancelled via `TaskQueue#skip`')
);
Expand Down Expand Up @@ -262,7 +262,7 @@ export default class TaskQueue implements Evented {
this._cancel();
task = this._tasks.shift();
let processor = this._processors.shift();
if (!processor.settled) {
if (processor !== undefined && !processor.settled) {
processor.reject(
e || new Error('Processing cancelled via `TaskQueue#shift`')
);
Expand Down
54 changes: 54 additions & 0 deletions packages/@orbit/core/test/task-queue-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,33 @@ module('TaskQueue', function() {
assert.equal(queue.length, 1, 'queue now has one member');
});

test('#skip just restarts processing if there are no tasks in the queue', async function(assert) {
assert.expect(2);

const performer: Performer = {
async perform(task: Task): Promise<void> {}
};

const queue = new TaskQueue(performer, { autoProcess: true });

await queue.skip();

let op1 = { op: 'add', path: ['planets', '123'], value: 'Mercury' };

queue.on('complete', function() {
assert.ok(true, 'queue completed');
});

queue.on('task', function(task) {
assert.strictEqual(task.data, op1, 'op1 processed');
});

await queue.push({
type: 'transform',
data: op1
});
});

test('#shift can remove failed tasks from an inactive queue, allowing processing to be restarted', async function(assert) {
assert.expect(11);

Expand Down Expand Up @@ -961,6 +988,33 @@ module('TaskQueue', function() {
assert.equal(queue.length, 1, 'queue now has one member');
});

test('#shift just restarts processing if there are no tasks in the queue', async function(assert) {
assert.expect(2);

const performer: Performer = {
async perform(task: Task): Promise<void> {}
};

const queue = new TaskQueue(performer, { autoProcess: true });

await queue.shift();

let op1 = { op: 'add', path: ['planets', '123'], value: 'Mercury' };

queue.on('complete', function() {
assert.ok(true, 'queue completed');
});

queue.on('task', function(task) {
assert.strictEqual(task.data, op1, 'op1 processed');
});

await queue.push({
type: 'transform',
data: op1
});
});

test('#unshift can add a new task to the beginning of an inactive queue', function(assert) {
assert.expect(9);

Expand Down

0 comments on commit f19060b

Please sign in to comment.