2009-10-12 16:09:15 +02:00
|
|
|
node.mixin(require("common.js"));
|
|
|
|
|
|
|
|
var timeouts = 0;
|
|
|
|
|
|
|
|
var promise = new node.Promise();
|
|
|
|
promise.timeout(250);
|
|
|
|
assertEquals(250, promise.timeout());
|
|
|
|
|
|
|
|
promise.addCallback(function() {
|
|
|
|
assertUnreachable('addCallback should not fire after a promise error');
|
|
|
|
});
|
|
|
|
|
|
|
|
promise.addErrback(function(e) {
|
|
|
|
assertInstanceof(e, Error);
|
|
|
|
assertEquals('timeout', e.message);
|
|
|
|
timeouts++;
|
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
promise.emitSuccess('Am I too late?');
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
var waitPromise = new node.Promise();
|
|
|
|
try {
|
|
|
|
waitPromise.timeout(250).wait()
|
|
|
|
} catch (e) {
|
|
|
|
assertInstanceof(e, Error);
|
|
|
|
assertEquals('timeout', e.message);
|
|
|
|
timeouts++;
|
|
|
|
}
|
|
|
|
|
2009-10-29 11:19:47 +01:00
|
|
|
var successPromise = new node.Promise();
|
|
|
|
successPromise.timeout(500);
|
|
|
|
setTimeout(function() {
|
|
|
|
successPromise.emitSuccess();
|
|
|
|
}, 250);
|
|
|
|
|
|
|
|
successPromise.addErrback(function() {
|
|
|
|
assertUnreachable('addErrback should not fire if there is no timeout');
|
|
|
|
});
|
|
|
|
|
|
|
|
var errorPromise = new node.Promise();
|
|
|
|
errorPromise.timeout(500);
|
|
|
|
setTimeout(function() {
|
|
|
|
errorPromise.emitError(new Error('intentional'));
|
|
|
|
}, 250);
|
|
|
|
|
|
|
|
errorPromise.addErrback(function(e) {
|
|
|
|
assertInstanceof(e, Error);
|
|
|
|
assertEquals('intentional', e.message);
|
|
|
|
});
|
|
|
|
|
|
|
|
var cancelPromise = new node.Promise();
|
|
|
|
cancelPromise.timeout(500);
|
|
|
|
setTimeout(function() {
|
|
|
|
cancelPromise.cancel();
|
|
|
|
}, 250);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
cancelPromise.emitSuccess('should be ignored');
|
|
|
|
}, 400);
|
|
|
|
|
|
|
|
cancelPromise.addCallback(function(e) {
|
|
|
|
assertUnreachable('addCallback should not fire if the promise is canceled');
|
|
|
|
});
|
|
|
|
|
|
|
|
cancelPromise.addErrback(function(e) {
|
|
|
|
assertUnreachable('addErrback should not fire if the promise is canceled');
|
|
|
|
});
|
|
|
|
|
|
|
|
var cancelTimeoutPromise = new node.Promise();
|
|
|
|
cancelTimeoutPromise.timeout(500);
|
|
|
|
setTimeout(function() {
|
|
|
|
cancelPromise.emitCancel();
|
|
|
|
}, 250);
|
|
|
|
|
|
|
|
cancelPromise.addErrback(function(e) {
|
|
|
|
assertUnreachable('addErrback should not fire after a cancel event');
|
|
|
|
});
|
|
|
|
|
2009-10-12 16:09:15 +02:00
|
|
|
process.addListener('exit', function() {
|
|
|
|
assertEquals(2, timeouts);
|
|
|
|
});
|