|
1
|
|
|
/* eslint-env mocha */ |
|
2
|
|
|
|
|
3
|
|
|
var Concurrent = require('../../../../lib').Concurrent |
|
4
|
|
|
var timeout = Concurrent.timeout |
|
5
|
|
|
var TimeoutException = Concurrent.TimeoutException |
|
6
|
|
|
var Sinon = require('sinon') |
|
7
|
|
|
var Chai = require('chai') |
|
8
|
|
|
var expect = Chai.expect |
|
9
|
|
|
|
|
10
|
|
|
Chai.use(require('chai-as-promised')) |
|
11
|
|
|
|
|
12
|
|
|
var branchStopper = function () { |
|
13
|
|
|
throw new Error('Unexpected branch execution') |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
describe('Integration', function () { |
|
17
|
|
|
describe('/concurrent', function () { |
|
18
|
|
|
describe('/timeout.js', function () { |
|
19
|
|
|
describe('.timeout', function () { |
|
20
|
|
|
var clock |
|
21
|
|
|
|
|
22
|
|
|
beforeEach(function () { |
|
23
|
|
|
clock = Sinon.useFakeTimers() |
|
24
|
|
|
}) |
|
25
|
|
|
|
|
26
|
|
|
afterEach(function () { |
|
27
|
|
|
clock.restore() |
|
28
|
|
|
}) |
|
29
|
|
|
|
|
30
|
|
|
it('returns promise if timeout is negative', function () { |
|
31
|
|
|
var promise = new Promise(function () {}) |
|
32
|
|
|
return expect(timeout(promise, -1)).to.equal(promise) |
|
33
|
|
|
}) |
|
34
|
|
|
|
|
35
|
|
|
it('returns promise if timeout is omitted', function () { |
|
36
|
|
|
var promise = new Promise(function () {}) |
|
37
|
|
|
return expect(timeout(promise)).to.equal(promise) |
|
38
|
|
|
}) |
|
39
|
|
|
|
|
40
|
|
|
it('returns promise if not-a-number is supplied', function () { |
|
41
|
|
|
var promise = new Promise(function () {}) |
|
42
|
|
|
return expect(timeout(promise, false)).to.equal(promise) |
|
43
|
|
|
}) |
|
44
|
|
|
|
|
45
|
|
|
it('wraps promise in timed out one', function () { |
|
46
|
|
|
var promise = new Promise(function () {}) |
|
47
|
|
|
var wrapped = timeout(promise, 0) |
|
48
|
|
|
clock.next() |
|
49
|
|
|
return expect(wrapped).to.eventually.be.rejectedWith(TimeoutException) |
|
50
|
|
|
}) |
|
51
|
|
|
|
|
52
|
|
|
it('passes provided message to exception', function () { |
|
53
|
|
|
var message = 'foo' |
|
54
|
|
|
var promise = new Promise(function () {}) |
|
55
|
|
|
var wrapped = timeout(promise, 0, message) |
|
56
|
|
|
clock.next() |
|
57
|
|
|
return wrapped |
|
58
|
|
|
.then(branchStopper, function (error) { |
|
59
|
|
|
expect(error).to.be.instanceOf(TimeoutException) |
|
60
|
|
|
expect(error.message).to.eq(message) |
|
61
|
|
|
}) |
|
62
|
|
|
}) |
|
63
|
|
|
|
|
64
|
|
|
it('doesn\'t timeout resolved promise', function () { |
|
65
|
|
|
var value = 12 |
|
66
|
|
|
var promise = Promise.resolve(value) |
|
67
|
|
|
var wrapped = promise |
|
68
|
|
|
.then(function () { |
|
69
|
|
|
return timeout(promise, 0) |
|
70
|
|
|
}) |
|
71
|
|
|
clock.next() |
|
72
|
|
|
return expect(wrapped).to.eventually.eq(value) |
|
73
|
|
|
}) |
|
74
|
|
|
}) |
|
75
|
|
|
}) |
|
76
|
|
|
}) |
|
77
|
|
|
}) |
|
78
|
|
|
|