-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.js
195 lines (173 loc) · 7.54 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
'use strict';
const bluebird = require('bluebird');
const assert = require('assert');
const util = require('util');
const blue_ext = require('./bluebird.retry');
const attempts = 3;
const calc_default_duration_sec = function(attempts, attempt = 0) {
if (attempt <= 0)
return calc_default_duration_sec(attempts, attempt + 1);
else if (attempt == attempts)
return 1;
else
return 1 + calc_default_duration_sec(attempts, attempt + 1);
}
const expected_duration_all_attempts_failed_ms = calc_default_duration_sec(attempts) * 1000;
const expected_duration_last_attempt_succeeds_ms = calc_default_duration_sec(attempts - 1) * 1000;
describe('retry() a function that throws an exception after the default number of attempts', () => {
it('should reject with RetryAttemptsExceeded error', () => {
return blue_ext.retry(() => { throw new Error("foo")})
.catch((err) => assert.equal(true, err instanceof blue_ext.exceptions.RetryAttemptsExceeded))
});
it('should reject after only 1 attempt', () => {
return blue_ext.retry(() => { throw new Error("foo")})
.catch((err) => {
assert.equal(1, err.attempts);
assert.equal(1, err.nested.length);
});
});
});
describe('retry() a function that throws an exception after the default number of attempts', () => {
it('should reject with PredicateViolation error', () => {
return blue_ext.predicatedRetry(() => { throw new Error("foo")})
.catch((err) => assert.equal(true, err instanceof blue_ext.exceptions.PredicateViolation))
});
it('should reject after only 1 attempt', () => {
return blue_ext.predicatedRetry(() => { throw new Error("foo")})
.catch((err) => {
assert.equal(1, err.attempts);
assert.equal(1, err.nested.length);
});
});
});
describe('retry() a function that ' + util.format('fails all %d attempts', attempts), () => {
it(util.format('should reject after %d attempts and ~%d milliseconds', attempts, expected_duration_all_attempts_failed_ms), () => {
var start_time = Date.now();
return blue_ext.retry(() => { throw new Error("foo")}, attempts)
.catch((err) => {
assert.equal(attempts, err.attempts);
assert.equal(attempts, err.nested.length);
}).finally(() => {
var duration = Date.now() - start_time;
assert.equal(true, (duration > (expected_duration_all_attempts_failed_ms - 100) && duration < (expected_duration_all_attempts_failed_ms + 100)))
});
});
});
describe('retry() a function that', () => {
describe(util.format('throws exceptions on the first %d attempts but resolves on attempt %d', attempts - 1, attempts), () => {
it(util.format('should resolve in ~%d ms', expected_duration_last_attempt_succeeds_ms), () => {
var start_time = Date.now();
var attempt = 0;
return blue_ext.retry(() => {
attempt++;
if (attempt < attempts) {
throw new Error("foo");
} else {
return 'success'
}
}, attempts)
.then((result) => assert.equal(result, 'success'))
.finally(() => {
var duration = Date.now() - start_time;
assert.equal(true, (duration > (expected_duration_last_attempt_succeeds_ms - 100) && duration < (expected_duration_last_attempt_succeeds_ms + 100)))
});
});
})
describe(util.format('rejects promises on the first %d attempts but resolves on attempt %d', attempts - 1, attempts), () => {
it(util.format('should resolve in ~%d ms', expected_duration_last_attempt_succeeds_ms), () => {
var start_time = Date.now();
var attempt = 0;
var fn = () => {
attempt++;
var result = (attempt == attempts) ? bluebird.resolve('success') : bluebird.reject('fail');
return result;
};
return blue_ext.retry(fn, attempts)
.then((result) => assert.equal(result, 'success'))
.finally(() => {
var duration = Date.now() - start_time;
assert.equal(true, (duration > (expected_duration_last_attempt_succeeds_ms - 100) && duration < (expected_duration_last_attempt_succeeds_ms + 100)))
});
});
});
});
describe('retry() a promise that rejects after the default number of attempts', () => {
it('should reject with RetryAttemptsExceeded error', () => {
return blue_ext.retry(bluebird.reject('Foo'))
.catch((err) => assert.equal(true, err instanceof blue_ext.exceptions.RetryAttemptsExceeded))
});
it('should reject after only 1 attempt', () => {
return blue_ext.retry(bluebird.reject('Foo'))
.catch((err) => {
assert.equal(1, err.attempts);
assert.equal(1, err.nested.length);
});
});
});
describe('retry() a promise that ' + util.format('fails all %d attempts', attempts), () => {
it(util.format('should reject after %d attempts and ~%d milliseconds', attempts, expected_duration_all_attempts_failed_ms), () => {
var start_time = Date.now();
return blue_ext.retry(bluebird.reject('Foo'), attempts)
.catch((err) => {
assert.equal(attempts, err.attempts);
assert.equal(attempts, err.nested.length);
}).finally(() => {
var duration = Date.now() - start_time;
assert.equal(true, (duration > (expected_duration_all_attempts_failed_ms - 100) && duration < (expected_duration_all_attempts_failed_ms + 100)))
});
});
})
describe('retry() a promise that resolves', () => {
it('should succeed', () => {
return blue_ext.retry(bluebird.resolve('success'), attempts)
.then((result) => assert.equal(result, 'success'))
})
});
describe('predicatedRetry() a promise that resolves', () => {
it('should succeed', () => {
return blue_ext.predicatedRetry(bluebird.resolve('success'), (attempts) => attempts > 3)
.then((result) => assert.equal(result, 'success'))
})
});
describe('ensure the 1st attempt (0th try) always executes', () => {
it('should succeed when the max retry attempts is 0', () => {
return blue_ext.retry(bluebird.resolve('success'), 0)
.then((result) => assert.equal(result, 'success'))
})
});
describe('ensure the 1st attempt (0th try) always executes', () => {
it('should succeed even though the predicate evaluates to true', () => {
return blue_ext.predicatedRetry(bluebird.resolve('success'), () => true)
.then((result) => assert.equal(result, 'success'))
})
});
describe('retry() a parameterless function that returns a value', () => {
it('should succeed', () => {
return blue_ext.retry(() => 'success', attempts)
.then((result) => assert.equal(result, 'success'))
})
});
describe('predicatedRetry() a parameterless function that returns a value', () => {
it('should succeed', () => {
return blue_ext.predicatedRetry(() => 'success')
.then((result) => assert.equal(result, 'success'))
})
});
describe('retry() a parameterized function that returns a value', () => {
it('should fail', () => {
return blue_ext.retry((a) => 'fail', attempts)
.then((result) => bluebird.reject("retry() resolved successfully, when it should have failed"))
.catch((err) => {
assert.equal(err.message.includes("parameterless javascript Function"), true)
})
})
});
describe('predicatedRetry() a failing promise that stops after 3 tries with a custom predicate', () => {
it('should fail', () => {
return blue_ext.predicatedRetry(bluebird.reject('fail'), (attempt) => { return attempt >= 3; })
.then((result) => bluebird.reject("retry() resolved successfully, when it should have failed"))
.catch((err) => {
assert.equal(true, err instanceof blue_ext.exceptions.PredicateViolation)
})
})
});