Skip to content

Commit

Permalink
Support alternate close error on Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Feb 5, 2013
1 parent d55e6f8 commit ea9aae2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
12 changes: 6 additions & 6 deletions queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ function Queue() {
get: function () {
var result = ends.promise.get("head");
ends.promise = ends.promise.get("tail");
return result.fail(function (reason) {
closed.resolve();
throw reason;
return result.fail(function (error) {
closed.resolve(error);
throw error;
});
},
closed: closed.promise,
close: function (reason) {
reason = reason || new Error("Can't get value from closed queue");
var end = {head: Q.reject(reason)};
close: function (error) {
error = error || new Error("Can't get value from closed queue");
var end = {head: Q.reject(error)};
end.tail = end;
ends.resolve(end);
return closed.promise;
Expand Down
33 changes: 32 additions & 1 deletion spec/queue-spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

var Q = require("../q");
var Queue = require("../queue");

global.Q = Q;
require("./lib/jasmine-promise");

describe("queue", function () {
Expand Down Expand Up @@ -142,7 +144,36 @@ describe("queue", function () {
})
.catch(function (error) {
expect(error.message).toBe("Can't get value from closed queue");
});
})
.then(function () {
return queue.closed;
})
.then(function (error) {
expect(error.message).toBe("Can't get value from closed queue");
})
});

it("should close with alternate error", function () {

var queue = Queue();
queue.close(new Error("Alternate reason"));

return Q.try(function () {
return queue.get();
})
.catch(function (error) {
expect(error.message).toBe("Alternate reason");
return queue.get();
})
.catch(function (error) {
expect(error.message).toBe("Alternate reason");
})
.then(function () {
return queue.closed;
})
.then(function (error) {
expect(error.message).toBe("Alternate reason");
})
});

});
Expand Down

0 comments on commit ea9aae2

Please sign in to comment.