Skip to content

Commit

Permalink
Merge branch 'feature/timeout-management' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
etki committed Aug 14, 2017
2 parents 8148613 + 9f7ab1a commit 45aefa4
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/concurrent/index.js
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
}
36 changes: 36 additions & 0 deletions lib/concurrent/timeout.js
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
}
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 test/suites/integration/concurrent/timeout.timeout.spec.js
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)
})
})
})
})
})

0 comments on commit 45aefa4

Please sign in to comment.