forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
402 lines (326 loc) · 10.1 KB
/
index.test.js
File metadata and controls
402 lines (326 loc) · 10.1 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/**
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
let Farm;
let Worker;
let mockWorkers;
function workerReply(i, error, result) {
return mockWorkers[i].send.mock.calls[0][1].call(
mockWorkers[i],
error,
result,
);
}
beforeEach(() => {
mockWorkers = [];
// The worker mock returns a worker with custom methods, plus it stores them
// in a global list, so that they can be accessed later. This list is reset in
// every test.
jest.mock('../worker', () => {
const fakeClass = jest.fn(() => {
const fakeWorker = {
getStderr: () => ({once() {}, pipe() {}}),
getStdout: () => ({once() {}, pipe() {}}),
send: jest.fn(),
};
mockWorkers.push(fakeWorker);
return fakeWorker;
});
return {
__esModule: true,
default: fakeClass,
};
});
jest.mock(
'/fake-worker.js',
() => {
return {
_shouldNotExist1() {},
methodA() {},
methodB() {},
};
},
{virtual: true},
);
jest.mock(
'/fake-worker-with-default-method.js',
() => {
return () => {};
},
{virtual: true},
);
Worker = require('../worker').default;
Farm = require('../index').default;
});
afterEach(() => {
jest.resetModules();
});
it('exposes the right API', () => {
const farm = new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
});
expect(typeof farm.foo).toBe('function');
expect(typeof farm.bar).toBe('function');
});
it('breaks if any of the forbidden methods is tried to be exposed', () => {
expect(
() => new Farm('/tmp/baz.js', {exposedMethods: ['getStdout']}),
).toThrow();
expect(
() => new Farm('/tmp/baz.js', {exposedMethods: ['getStderr']}),
).toThrow();
expect(() => new Farm('/tmp/baz.js', {exposedMethods: ['end']})).toThrow();
});
it('works with minimal options', () => {
// eslint-disable-next-line no-new
const farm1 = new Farm('/fake-worker.js');
expect(Worker).toHaveBeenCalledTimes(require('os').cpus().length - 1);
expect(typeof farm1.methodA).toBe('function');
expect(typeof farm1.methodB).toBe('function');
expect(typeof farm1._shouldNotExist).not.toBe('function');
// eslint-disable-next-line no-new
const farm2 = new Farm('/fake-worker-with-default-method.js');
expect(typeof farm2.default).toBe('function');
});
it('tries instantiating workers with the right options', () => {
// eslint-disable-next-line no-new
new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
forkOptions: {execArgv: []},
maxRetries: 6,
numWorkers: 4,
});
expect(Worker).toHaveBeenCalledTimes(4);
expect(Worker.mock.calls[0][0]).toEqual({
forkOptions: {execArgv: []},
maxRetries: 6,
workerId: 1,
workerPath: '/tmp/baz.js',
});
});
it('create multiple workers with unique worker ids', () => {
// eslint-disable-next-line no-new
new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
forkOptions: {execArgv: []},
maxRetries: 6,
numWorkers: 3,
});
expect(Worker).toHaveBeenCalledTimes(3);
expect(Worker.mock.calls[0][0].workerId).toEqual(1);
expect(Worker.mock.calls[1][0].workerId).toEqual(2);
expect(Worker.mock.calls[2][0].workerId).toEqual(3);
});
it('makes a non-existing relative worker throw', () => {
expect(
() =>
new Farm('./baz.js', {
exposedMethods: [],
numWorkers: 1,
}),
).toThrow();
});
it('aggregates all stdouts and stderrs from all workers', () => {
const out = [];
const err = [];
Worker.mockImplementation(() => {
return {
getStderr: () => ({
once() {},
pipe(errStream) {
err.push(errStream);
},
}),
getStdout: () => ({
once() {},
pipe(outStream) {
out.push(outStream);
},
}),
};
});
const farm = new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
numWorkers: 2,
});
expect(out.length).toBe(2);
expect(err.length).toBe(2);
const stdout = jest.fn();
const stderr = jest.fn();
farm.getStdout().on('data', stdout);
farm.getStderr().on('data', stderr);
out[0].write(Buffer.from('hello'));
out[1].write(Buffer.from('bye'));
err[1].write(Buffer.from('house'));
err[0].write(Buffer.from('tree'));
expect(stdout.mock.calls[0][0].toString()).toBe('hello');
expect(stdout.mock.calls[1][0].toString()).toBe('bye');
expect(stderr.mock.calls[0][0].toString()).toBe('house');
expect(stderr.mock.calls[1][0].toString()).toBe('tree');
});
it('works when stdout and stderr are not piped to the parent', () => {
Worker.mockImplementation(() => ({
getStderr: () => null,
getStdout: () => null,
send: () => null,
}));
const farm = new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
forkOptions: {
silent: false,
stdio: 'inherit',
},
numWorkers: 2,
});
expect(() => farm.foo()).not.toThrow();
expect(() => farm.bar()).not.toThrow();
});
it('does not let make calls after the farm is ended', () => {
const farm = new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
});
farm.end();
expect(() => farm.foo()).toThrow();
expect(() => farm.bar()).toThrow();
});
it('does not let end the farm after it is ended', () => {
const farm = new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
});
farm.end();
expect(() => farm.end()).toThrow();
});
it('calls "computeWorkerKey" for each of the calls', () => {
const computeWorkerKey = jest.fn();
const farm = new Farm('/tmp/baz.js', {
computeWorkerKey,
exposedMethods: ['foo', 'bar'],
numWorkers: 3,
});
farm.foo('car', 'plane');
expect(computeWorkerKey.mock.calls[0]).toEqual(['foo', 'car', 'plane']);
});
it('returns the result if the call worked', async () => {
const farm = new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
numWorkers: 1,
});
const promise = farm.foo('car', 'plane');
workerReply(0, null, 34);
expect(await promise).toEqual(34);
});
it('throws if the call failed', async () => {
const farm = new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
numWorkers: 1,
});
const promise = farm.foo('car', 'plane');
let error = null;
workerReply(0, new TypeError('Massively broken'));
try {
await promise;
} catch (err) {
error = err;
}
expect(error).not.toBe(null);
expect(error).toBeInstanceOf(TypeError);
});
it('sends non-sticked tasks to all workers', () => {
const farm = new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
numWorkers: 3,
});
farm.foo('car', 'plane');
expect(mockWorkers[0].send).toHaveBeenCalledTimes(1);
expect(mockWorkers[1].send).toHaveBeenCalledTimes(1);
expect(mockWorkers[2].send).toHaveBeenCalledTimes(1);
});
it('sends first-time sticked tasks to all workers', () => {
const farm = new Farm('/tmp/baz.js', {
computeWorkerKey: () => '1234567890abcdef',
exposedMethods: ['foo', 'bar'],
numWorkers: 3,
});
farm.foo('car', 'plane');
expect(mockWorkers[0].send).toHaveBeenCalledTimes(1);
expect(mockWorkers[1].send).toHaveBeenCalledTimes(1);
expect(mockWorkers[2].send).toHaveBeenCalledTimes(1);
});
it('checks that once a sticked task finishes, next time is sent to that worker', async () => {
const farm = new Farm('/tmp/baz.js', {
computeWorkerKey: () => '1234567890abcdef',
exposedMethods: ['foo', 'bar'],
numWorkers: 3,
});
// Worker 1 successfully replies with "17" as a result.
const promise = farm.foo('car', 'plane');
workerReply(1, null, 17);
await promise;
// Note that the stickiness is not created by the method name or the arguments
// it is solely controlled by the provided "computeWorkerKey" method, which in
// the test example always returns the same key, so all calls should be
// redirected to worker 1 (which is the one that resolved the first call).
farm.bar();
// The first time, a call with a "1234567890abcdef" hash had never been done
// earlier ("foo" call), so it got queued to all workers. Later, since the one
// that resolved the call was the one in position 1, all subsequent calls are
// only redirected to that worker.
expect(mockWorkers[0].send).toHaveBeenCalledTimes(1); // Only "foo".
expect(mockWorkers[1].send).toHaveBeenCalledTimes(2); // "foo" + "bar".
expect(mockWorkers[2].send).toHaveBeenCalledTimes(1); // Only "foo".
});
it('checks that once a non-sticked task finishes, next time is sent to all workers', async () => {
// Note there is no "computeWorkerKey".
const farm = new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
numWorkers: 3,
});
// Worker 1 successfully replies with "17" as a result.
const promise = farm.foo('car', 'plane');
workerReply(1, null, 17);
await promise;
farm.bar();
// Since "computeWorkerKey" does not return anything, new jobs are sent again to
// all existing workers.
expect(mockWorkers[0].send).toHaveBeenCalledTimes(2);
expect(mockWorkers[1].send).toHaveBeenCalledTimes(2);
expect(mockWorkers[2].send).toHaveBeenCalledTimes(2);
});
it('rotates workers when they are idling', async () => {
let order;
let promise;
// Note there is no "computeWorkerKey".
const farm = new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
numWorkers: 3,
});
[0, 1, 2].forEach(i => {
mockWorkers[i].send.mockReset();
mockWorkers[i].send.mockImplementation(() => order.push(i));
});
// First time, the order is 0, 1, 2.
order = [];
promise = farm.foo('car', 'plane');
expect(order).toEqual([0, 1, 2]);
// Worker 1 successfully replies with "17" as a result.
workerReply(1, null, 17);
await promise;
[0, 1, 2].forEach(i => {
mockWorkers[i].send.mockReset();
mockWorkers[i].send.mockImplementation(() => order.push(i));
});
// Now, the order is 1, 2, 0 (shifted one).
order = [];
promise = farm.foo('car', 'plane');
expect(order).toEqual([1, 2, 0]);
// Worker 1 successfully replies again.
workerReply(1, null, 17);
await promise;
});