-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/timeout-management' into dev
- Loading branch information
Showing
4 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
var timeout = require('./timeout') | ||
|
||
module.exports = { | ||
Future: require('./Future').Future | ||
Future: require('./Future').Future, | ||
TimeoutException: timeout.TimeoutException, | ||
timeout: timeout.timeout | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @class | ||
* @param {string} message | ||
*/ | ||
function TimeoutException (message) { | ||
this.message = message || 'Timeout has exceeded' | ||
this.stack = (new Error()).stack | ||
} | ||
|
||
TimeoutException.prototype = Object.create(Error.prototype) | ||
TimeoutException.prototype.name = 'TimeoutException' | ||
TimeoutException.prototype.constructor = TimeoutException | ||
|
||
/** | ||
* @param {Promise.<*>|Thenable.<*>} promise Promise to add time bound to | ||
* @param {int} timeout Timeout in milliseconds | ||
* @param {string} message | ||
* @return {Thenable.<*>} | ||
*/ | ||
function timeout (promise, timeout, message) { | ||
if (timeout < 0) { | ||
return promise | ||
} | ||
return new Promise(function (resolve, reject) { | ||
promise.then(resolve, reject) | ||
setTimeout(function () { | ||
message = message || 'Timeout of ' + timeout + ' ms has exceeded' | ||
reject(new TimeoutException(message)) | ||
}, timeout) | ||
}) | ||
} | ||
|
||
module.exports = { | ||
timeout: timeout, | ||
TimeoutException: TimeoutException | ||
} |
18 changes: 18 additions & 0 deletions
18
test/suites/integration/concurrent/timeout.TimeoutException.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* eslint-env mocha */ | ||
|
||
var Concurrent = require('../../../../lib').Concurrent | ||
var TimeoutException = Concurrent.TimeoutException | ||
var Chai = require('chai') | ||
var expect = Chai.expect | ||
|
||
describe('Integration', function () { | ||
describe('/concurrent', function () { | ||
describe('/timeout.js', function () { | ||
describe('.TimeoutException', function () { | ||
it('is a child of error', function () { | ||
expect(new TimeoutException()).to.be.instanceOf(Error) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
53 changes: 53 additions & 0 deletions
53
test/suites/integration/concurrent/timeout.timeout.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* eslint-env mocha */ | ||
|
||
var Concurrent = require('../../../../lib').Concurrent | ||
var timeout = Concurrent.timeout | ||
var TimeoutException = Concurrent.TimeoutException | ||
var Chai = require('chai') | ||
var expect = Chai.expect | ||
|
||
Chai.use(require('chai-as-promised')) | ||
|
||
var branchStopper = function () { | ||
throw new Error('Unexpected branch execution') | ||
} | ||
|
||
describe('Integration', function () { | ||
describe('/concurrent', function () { | ||
describe('/timeout.js', function () { | ||
describe('.timeout', function () { | ||
it('returns promise if timeout is negative', function () { | ||
var promise = new Promise(function () {}) | ||
return expect(timeout(promise, -1)).to.equal(promise) | ||
}) | ||
|
||
it('wraps promise in timed out one', function () { | ||
var promise = new Promise(function () {}) | ||
var wrapped = timeout(promise, 0) | ||
return expect(wrapped).to.eventually.be.rejectedWith(TimeoutException) | ||
}) | ||
|
||
it('passes provided message to exception', function () { | ||
var message = 'foo' | ||
var promise = new Promise(function () {}) | ||
var wrapped = timeout(promise, 0, message) | ||
return wrapped | ||
.then(branchStopper, function (error) { | ||
expect(error).to.be.instanceOf(TimeoutException) | ||
expect(error.message).to.eq(message) | ||
}) | ||
}) | ||
|
||
it('doesn\'t timeout resolved promise', function () { | ||
var value = 12 | ||
var promise = Promise.resolve(value) | ||
var wrapped = promise | ||
.then(function () { | ||
return timeout(promise, 0) | ||
}) | ||
return expect(wrapped).to.eventually.eq(value) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |