-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
280 lines (213 loc) · 9.63 KB
/
index.test.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// node core modules
// 3rd party modules
const test = require('ava');
const sinon = require('sinon');
// local modules
const methods = require('./lib');
const {
reflectAllPromises, retryAllRejectedPromises, reflectAndRetryAllRejectedPromises, delay, batchPromises,
racePromisesWithTime
} = methods;
test.beforeEach((t) => {
const logger = {
debug: value => value,
error: value => value
};
Object.assign(t.context, { logger });
});
test('Verify that reflectAllPromises does not throw an error', async (t) => {
const errorPromise = () => Promise.reject(new Error('Throw error'));
const { logger } = t.context;
const loggerErrorSpy = sinon.spy(logger, 'error');
const [{ status, error, rejectedPromise }] = await reflectAllPromises([errorPromise], logger);
t.is(status, 'rejected');
t.is(typeof rejectedPromise, 'function');
t.is(error.message, 'Throw error');
t.true(loggerErrorSpy.calledWith('Reflect promise error: '));
t.true(loggerErrorSpy.calledOnce);
});
test('Verify that reflectAllPromises does not throw an error when non-function is provided as promise', async (t) => {
const { logger } = t.context;
const loggerErrorSpy = sinon.spy(logger, 'error');
const [{ status, error, rejectedPromise }] = await reflectAllPromises([Promise.reject(new Error('Throw error'))], logger);
t.is(status, 'rejected');
t.is(rejectedPromise, undefined, 'rejectedPromise is not provided in this case');
t.is(error.message, 'Throw error');
t.true(loggerErrorSpy.calledWith('Reflect promise error: '));
t.true(loggerErrorSpy.calledOnce);
});
test('Verify that reflectAllPromises returns resolved promises', async (t) => {
const { logger } = t.context;
const mockResponse = { a: 'b' };
const loggerErrorSpy = sinon.spy(logger, 'error');
const validResponse = () => Promise.resolve(mockResponse);
const [{ status, data }] = await reflectAllPromises([validResponse], logger);
t.is(status, 'resolved');
t.is(typeof data, 'object');
t.deepEqual(data, mockResponse);
t.false(loggerErrorSpy.calledOnce);
});
test('Verify that reflectAllPromises returns resolved promises when non-function is provided', async (t) => {
const { logger } = t.context;
const mockResponse = { a: 'b' };
const loggerErrorSpy = sinon.spy(logger, 'error');
const [{ status, data }] = await reflectAllPromises([Promise.resolve(mockResponse)], logger);
t.is(status, 'resolved');
t.is(typeof data, 'object');
t.deepEqual(data, mockResponse);
t.false(loggerErrorSpy.calledOnce);
});
test('Verify that retryAllPromises works without retryParams', async (t) => {
const { logger } = t.context;
const mockResponse = { a: 'b' };
const loggerErrorSpy = sinon.spy(logger, 'error');
const validResponse = () => Promise.resolve(mockResponse);
await retryAllRejectedPromises([validResponse], logger);
t.false(loggerErrorSpy.calledOnce);
});
test('Verify that retryAllPromises retries for the number of provided attempts before failing', async (t) => {
const { logger } = t.context;
const errorPromise = () => Promise.reject(new Error('Throw error'));
const loggerErrorSpy = sinon.spy(logger, 'error');
const maxAttempts = 2;
const retryParams = { maxAttempts, delay: 10 };
const result = await (retryAllRejectedPromises([errorPromise], retryParams, logger));
t.is(result.length, 1);
t.is(loggerErrorSpy.callCount, maxAttempts + 1);
});
test('Verify that retryAllPromises retries only failed promises', async (t) => {
const { logger } = t.context;
const errorPromise = () => Promise.reject(new Error('Throw error'));
const validResponse = () => Promise.resolve({});
const maxAttempts = 1;
const retryParams = { maxAttempts, delay: 10 };
const result = await (retryAllRejectedPromises([errorPromise, validResponse, errorPromise], retryParams, logger));
t.is(result.length, 2);
});
test('Verify that reflectAndRetryAllRejectedPromises never throws an error', async (t) => {
const { logger } = t.context;
const errorPromise = () => Promise.reject(new Error('Throw error'));
const validResponse = () => Promise.resolve({});
const loggerErrorSpy = sinon.spy(logger, 'error');
const maxAttempts = 2;
const retryParams = { maxAttempts, delay: 10 };
await reflectAndRetryAllRejectedPromises([errorPromise, validResponse, errorPromise], retryParams, logger);
t.is(loggerErrorSpy.callCount, 2, 'For initial execution there should be 2 error log calls');
await delay(300);
// we try to execute failed promises after in next event loops
t.is(loggerErrorSpy.callCount, 6, 'For each retry attempt there should be more logs per execution');
});
test('Verify that batchPromises returns only resolved promises', async (t) => {
const errorPromise = () => Promise.reject(new Error('Throw error'));
const response1 = 1;
const response2 = 2;
const response3 = 3;
const validResponse1 = () => Promise.resolve(response1);
const validResponse2 = () => Promise.resolve(response2);
const validResponse3 = () => Promise.resolve(response3);
const maxBatchSize = 2;
const batchParams = { maxBatchSize, delay: 100, responseMode: 'ONLY_RESOLVED' };
const promises = [errorPromise, validResponse1, validResponse2, validResponse3];
const response = await batchPromises(batchParams)(promises);
t.deepEqual(response, [response1, response2, response3]);
});
test('Verify that batchPromises returns only rejected promises', async (t) => {
const errorPromise = () => Promise.reject(new Error('Some error'));
const validResponse1 = () => Promise.resolve(1);
const validResponse2 = () => Promise.resolve(2);
const validResponse3 = () => Promise.resolve(3);
const maxBatchSize = 2;
const batchParams = { maxBatchSize, delay: 100, responseMode: 'ONLY_REJECTED' };
const promises = [errorPromise, validResponse1, validResponse2, validResponse3];
const [errorResponse] = await batchPromises(batchParams)(promises);
t.deepEqual(errorResponse.message, 'Some error');
});
test('Verify that batchPromises returns all promises. Order must be preserved', async (t) => {
const errorPromise = () => Promise.reject(new Error('Some error'));
const response1 = 1;
const response2 = 2;
const response3 = 3;
const validResponse1 = () => Promise.resolve(response1);
const validResponse2 = () => Promise.resolve(response2);
const validResponse3 = () => Promise.resolve(response3);
const maxBatchSize = 2;
const batchParams = { maxBatchSize, delay: 100 };
const promises = [errorPromise, validResponse1, validResponse2, validResponse3];
const [error, ...resolved] = await batchPromises(batchParams)(promises);
t.deepEqual(resolved, [response1, response2, response3]);
t.is(error.message, 'Some error');
});
test('Verify that batchPromises returns all promises, with split of resolved/rejected', async (t) => {
const errorPromise = () => Promise.reject(new Error('Some error'));
const response1 = 1;
const response2 = 2;
const response3 = 3;
const validResponse1 = () => Promise.resolve(response1);
const validResponse2 = () => Promise.resolve(response2);
const validResponse3 = () => Promise.resolve(response3);
const maxBatchSize = 2;
const batchParams = { maxBatchSize, delay: 100, responseMode: 'ALL_SPLIT' };
const promises = [errorPromise, validResponse2, validResponse1, validResponse3];
const [resolved, [rejected]] = await batchPromises(batchParams)(promises);
t.deepEqual(resolved, [response2, response1, response3]);
t.is(rejected.message, 'Some error');
});
test('Verify that batchPromises returns [] when no promises array is provided', async (t) => {
const maxBatchSize = 2;
const batchParams = { maxBatchSize, delay: 100 };
const response = await batchPromises(batchParams)([]);
t.deepEqual(response, []);
});
test('Verify that batchPromises throws error for invalid responseMode', async (t) => {
const maxBatchSize = 2;
const batchParams = { maxBatchSize, delay: 100, responseMode: 'Invalid' };
const errorResponse = await t.throwsAsync(batchPromises(batchParams)([]));
t.is(errorResponse.message, 'Invalid responseMode provided');
});
test('Test race promises with all passing', async t => {
const res1 = delay(1000, 1);
const res2 = delay(2000, 2);
const res3 = delay(4000, 3);
const listOfPromises = [res1, res2, res3];
const response = await racePromisesWithTime({ listOfPromises, raceTimeoutInMs: 5000 });
t.is(response.length, 3);
t.is(response[0], 1);
t.is(response[1], 2);
t.is(response[2], 3);
});
test('Test race promises with 0 passing', async t => {
const res1 = delay(1000, 1);
const res2 = delay(2000, 2);
const res3 = delay(5000, 3);
const listOfPromises = [res1, res2, res3];
const response = await racePromisesWithTime({ listOfPromises, raceTimeoutInMs: 700 });
t.is(response.length, 0);
});
test('Test race promises with some passing', async t => {
const res1 = delay(1000, 1);
const res2 = delay(2000, 2);
const res3 = delay(5000, 3);
const listOfPromises = [res2, res1, res3];
const response = await racePromisesWithTime({ listOfPromises, raceTimeoutInMs: 1500 });
t.is(response.length, 1);
t.is(response[0], 1);
});
test('Test race promises and verify all results', async t => {
const res1 = delay(1000, 1);
const res2 = delay(2000, 2);
const res3 = delay(5000, 3);
const listOfPromises = [res1, res2, res3];
const raceTimeoutMessage = 'My error message';
const params = {
listOfPromises,
raceTimeoutMessage,
raceTimeoutInMs: 1500,
responseMode: 'ONLY_RESOLVED'
};
const [valid, invalid1, invalid2] = await racePromisesWithTime(params);
t.is(valid, 1);
[invalid1, invalid2].forEach(({ raceTimeoutMessage, timeoutPromise }) => {
t.is(raceTimeoutMessage, raceTimeoutMessage);
t.true(timeoutPromise instanceof Promise);
});
});