Commit 29822afe authored by Romain Courteaud's avatar Romain Courteaud

Queue: cancelling stop the .push result propagation

parent a6b294f3
...@@ -25,8 +25,8 @@ var Queue = function() { ...@@ -25,8 +25,8 @@ var Queue = function() {
} }
function canceller() { function canceller() {
for (var i = 0; i < 2; i++) { for (var i = promise_list.length; i > 0; i--) {
promise_list[i].cancel(); promise_list[i - 1].cancel();
} }
} }
......
...@@ -2211,8 +2211,10 @@ describe("`RSVP.Queue`", function () { ...@@ -2211,8 +2211,10 @@ describe("`RSVP.Queue`", function () {
}); });
it('should throw if the queue is rejected', function (done) { it('should throw if the queue is rejected', function (done) {
var queue = new RSVP.Queue(); var queue = new RSVP.Queue()
queue.cancel(); .push(undefined, function () {
return RSVP.reject(1);
});
setTimeout(function() { setTimeout(function() {
assert.throws(function () { assert.throws(function () {
...@@ -2221,6 +2223,15 @@ describe("`RSVP.Queue`", function () { ...@@ -2221,6 +2223,15 @@ describe("`RSVP.Queue`", function () {
done(); done();
}, 20); }, 20);
}); });
it('should throw if the queue is cancelled', function () {
var queue = new RSVP.Queue();
queue.cancel();
assert.throws(function () {
queue.push();
}, RSVP.ResolvedQueueError);
});
}); });
describe("`Queue` instance", function () { describe("`Queue` instance", function () {
...@@ -2251,7 +2262,7 @@ describe("`RSVP.Queue`", function () { ...@@ -2251,7 +2262,7 @@ describe("`RSVP.Queue`", function () {
it('get the fulfillmentValue of the last queue entry', function (done) { it('get the fulfillmentValue of the last queue entry', function (done) {
var queue = new RSVP.Queue(); var queue = new RSVP.Queue();
queue.push(function () { queue.push(function () {
return "foo" return "foo";
}); });
setTimeout(function() { setTimeout(function() {
...@@ -2326,25 +2337,180 @@ describe("`RSVP.Queue`", function () { ...@@ -2326,25 +2337,180 @@ describe("`RSVP.Queue`", function () {
done(); done();
}, 20); }, 20);
}); });
});
describe("`cancel`", function () {
it('cancel also cancels the remaining promise', function (done) { it('should be a function', function () {
var queue = new RSVP.Queue();
assert.equal(typeof queue.cancel, "function");
});
it('should reject with a CancellationError', function (done) {
var queue = new RSVP.Queue(), var queue = new RSVP.Queue(),
pushed_result; error;
queue.push(function () { queue.then(null, function(e) {
return RSVP.timeout(1); error = e;
});
queue.push(undefined, function (value) {
pushed_result = value;
throw value;
}); });
queue.cancel(); queue.cancel();
setTimeout(function() { setTimeout(function() {
assert(pushed_result instanceof RSVP.CancellationError); assert(error instanceof RSVP.CancellationError);
assert.equal(queue.isRejected, true); assert.equal(queue.isRejected, true);
assert(queue.rejectedReason instanceof RSVP.CancellationError); assert(queue.rejectedReason instanceof RSVP.CancellationError);
done(); done();
}, 20); }, 20);
}); });
it('does nothing on a `fulfilled` queue', function (done) {
var queue = new RSVP.Queue()
.push(function () {
return 'ok';
}),
result = 'MARKER1',
error = 'MARKER2';
queue.then(function(e) {
result = e;
});
queue.fail(function(e) {
error = e;
});
setTimeout(function() {
queue.cancel();
setTimeout(function() {
assert.equal(queue.isRejected, undefined);
assert.equal(queue.isFulfilled, true);
assert.equal(queue.fulfillmentValue, 'ok');
assert.equal(result, 'ok');
assert.equal(error, 'MARKER2');
done();
}, 20);
}, 20);
});
it('does nothing on a `rejected` queue', function (done) {
var queue = new RSVP.Queue()
.push(function () {
throw 'ko';
}),
result = 'MARKER1',
error = 'MARKER2';
queue.then(function(e) {
result = e;
});
queue.fail(function(e) {
error = e;
});
setTimeout(function() {
queue.cancel();
setTimeout(function() {
assert.equal(queue.isRejected, true);
assert.equal(queue.isFulfilled, undefined);
assert.equal(queue.rejectedReason, 'ko');
assert.equal(result, 'MARKER1');
assert.equal(error, 'ko');
done();
}, 20);
}, 20);
});
it('should `cancel` pending success `thenable`', function (done) {
var thenable_cancel_called = false,
thenable_ongoing = false,
later_success_thenable_called = false,
later_error_thenable_called = false,
queue = new RSVP.Queue()
.push(function () {
return new RSVP.Promise(
function () {
thenable_ongoing = true;
},
function () {
thenable_cancel_called = true;
});
})
.push(
// Should be skipped
function () {later_success_thenable_called = true;},
function () {later_error_thenable_called = true;}
),
result = 'MARKER1',
error = 'MARKER2';
queue.then(function(e) {
result = e;
});
queue.fail(function(e) {
error = e;
});
setTimeout(function() {
assert.equal(queue.isRejected, undefined);
assert.equal(queue.isFulfilled, undefined);
assert.equal(thenable_ongoing, true);
queue.cancel();
assert.equal(thenable_cancel_called, true);
setTimeout(function() {
assert.equal(queue.isRejected, true);
assert.equal(queue.isFulfilled, undefined);
assert(queue.rejectedReason instanceof RSVP.CancellationError);
assert.equal(result, 'MARKER1');
assert(error instanceof RSVP.CancellationError);
assert.equal(later_success_thenable_called, false);
assert.equal(later_error_thenable_called, false);
done();
}, 20);
}, 20);
});
it('should `cancel` pending error `thenable`', function (done) {
var thenable_cancel_called = false,
thenable_ongoing = false,
later_success_thenable_called = false,
later_error_thenable_called = false,
queue = new RSVP.Queue()
.push(function () {
return RSVP.reject(1);
})
.push(undefined, function () {
return new RSVP.Promise(
function () {
thenable_ongoing = true;
},
function () {
thenable_cancel_called = true;
});
})
.push(
// Should be skipped
function () {later_success_thenable_called = true;},
function () {later_error_thenable_called = true;}
),
result = 'MARKER1',
error = 'MARKER2';
queue.then(function(e) {
result = e;
});
queue.fail(function(e) {
error = e;
});
setTimeout(function() {
assert.equal(queue.isRejected, undefined);
assert.equal(queue.isFulfilled, undefined);
assert.equal(thenable_ongoing, true);
queue.cancel();
assert.equal(thenable_cancel_called, true);
setTimeout(function() {
assert.equal(queue.isRejected, true);
assert.equal(queue.isFulfilled, undefined);
assert(queue.rejectedReason instanceof RSVP.CancellationError);
assert.equal(result, 'MARKER1');
assert(error instanceof RSVP.CancellationError);
assert.equal(later_success_thenable_called, false);
assert.equal(later_error_thenable_called, false);
done();
}, 20);
}, 20);
});
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment