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