-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
manager.js
571 lines (481 loc) · 16.3 KB
/
manager.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
const path = require(`path`)
const _ = require(`lodash`)
const { slash } = require(`gatsby-core-utils`)
const worker = require(`./fixtures/node_modules/gatsby-plugin-test/gatsby-worker`)
const reporter = require(`gatsby-cli/lib/reporter`)
const hasha = require(`hasha`)
const fs = require(`fs-extra`)
const pDefer = require(`p-defer`)
const { uuid } = require(`gatsby-core-utils`)
const timers = require(`timers`)
const { MESSAGE_TYPES } = require(`../types`)
let WorkerError
let jobManager = null
// I need a mock to spy on
jest.mock(`p-defer`, () =>
jest.fn().mockImplementation(jest.requireActual(`p-defer`))
)
jest.mock(`gatsby-cli/lib/reporter`, () => {
return {
phantomActivity: jest.fn(),
createProgress: jest.fn(),
warn: jest.fn(),
}
})
jest.mock(`gatsby-core-utils`, () => {
const realCoreUtils = jest.requireActual(`gatsby-core-utils`)
return {
...realCoreUtils,
uuid: {
...realCoreUtils.uuid,
v4: jest.fn(realCoreUtils.uuid.v4),
},
}
})
jest.mock(`hasha`, () => jest.requireActual(`hasha`))
fs.ensureDir = jest.fn().mockResolvedValue(true)
const nodeModulesPluginPath = slash(
path.resolve(__dirname, `fixtures`, `node_modules`, `gatsby-plugin-test`)
)
const plugin = {
name: `gatsby-plugin-test`,
version: `1.0.0`,
resolve: nodeModulesPluginPath,
}
const createMockJob = (overrides = {}) => {
return {
name: `TEST_JOB`,
inputPaths: [
path.resolve(__dirname, `fixtures/input1.jpg`),
path.resolve(__dirname, `fixtures/input2.jpg`),
],
outputDir: path.resolve(__dirname, `public/outputDir`),
args: {
param1: `param1`,
param2: `param2`,
},
...overrides,
}
}
const createInternalMockJob = (overrides = {}) => {
const { createInternalJob } = jobManager
return createInternalJob(createMockJob(overrides), plugin)
}
describe(`Jobs manager`, () => {
const endActivity = jest.fn()
beforeEach(() => {
worker.TEST_JOB.mockReset()
worker.NEXT_JOB.mockReset()
endActivity.mockClear()
pDefer.mockClear()
uuid.v4.mockClear()
reporter.phantomActivity.mockImplementation(() => {
return {
start: jest.fn(),
end: endActivity,
}
})
reporter.createProgress.mockImplementation(() => {
return {
start: jest.fn(),
tick: jest.fn(),
end: jest.fn(),
}
})
jest.isolateModules(() => {
jobManager = require(`../manager`)
WorkerError = require(`../types`).WorkerError
})
})
describe(`createInternalJob`, () => {
it(`should return the correct format`, async () => {
const { createInternalJob } = jobManager
const mockedJob = createMockJob()
const job = createInternalJob(mockedJob, plugin)
expect(job).toStrictEqual(
expect.objectContaining({
id: expect.any(String),
name: mockedJob.name,
contentDigest: expect.any(String),
inputPaths: [
{
path: slash(mockedJob.inputPaths[0]),
contentDigest: expect.any(String),
},
{
path: slash(mockedJob.inputPaths[1]),
contentDigest: expect.any(String),
},
],
outputDir: slash(mockedJob.outputDir),
args: mockedJob.args,
plugin: {
name: `gatsby-plugin-test`,
version: `1.0.0`,
resolve: nodeModulesPluginPath,
isLocal: false,
},
})
)
})
it(`should fail when path is relative`, async () => {
const { createInternalJob } = jobManager
const jobArgs = createMockJob({
inputPaths: [`./files/image.jpg`],
})
expect.assertions(1)
try {
createInternalJob(jobArgs, plugin)
} catch (err) {
expect(err).toMatchInlineSnapshot(
`[Error: ./files/image.jpg should be an absolute path.]`
)
}
})
it(`shouldn't augument a job twice`, () => {
const { createInternalJob } = jobManager
const internalJob = createInternalJob(createMockJob(), plugin)
createInternalJob(internalJob, plugin)
expect(uuid.v4).toHaveBeenCalledTimes(1)
})
})
describe(`enqueueJob`, () => {
it(`should schedule a job`, async () => {
const { enqueueJob } = jobManager
worker.TEST_JOB.mockReturnValue({ output: `myresult` })
worker.NEXT_JOB.mockReturnValue({ output: `another result` })
const mockedJob = createInternalMockJob()
const job1 = enqueueJob(mockedJob)
const job2 = enqueueJob(
createInternalMockJob({
inputPaths: [],
name: `NEXT_JOB`,
})
)
await Promise.all([
expect(job1).resolves.toStrictEqual({ output: `myresult` }),
expect(job2).resolves.toStrictEqual({ output: `another result` }),
])
expect(endActivity).toHaveBeenCalledTimes(1)
expect(worker.TEST_JOB).toHaveBeenCalledTimes(1)
expect(worker.TEST_JOB).toHaveBeenCalledWith({
inputPaths: mockedJob.inputPaths,
outputDir: mockedJob.outputDir,
args: mockedJob.args,
})
expect(worker.NEXT_JOB).toHaveBeenCalledTimes(1)
})
it(`should only enqueue a job once`, async () => {
const { enqueueJob } = jobManager
const jobArgs = createInternalMockJob()
const jobArgs2 = _.cloneDeep(jobArgs)
const jobArgs3 = createInternalMockJob({
args: {
param2: `param2`,
param1: `param1`,
},
})
worker.TEST_JOB.mockReturnValue({ output: `myresult` })
const promises = []
promises.push(enqueueJob(jobArgs))
promises.push(enqueueJob(jobArgs2))
promises.push(enqueueJob(jobArgs3))
await expect(Promise.all(promises)).resolves.toStrictEqual([
{ output: `myresult` },
{ output: `myresult` },
{ output: `myresult` },
])
// we have 1 pdefer for the job & 1 for the wait until all jobs are done
expect(pDefer).toHaveBeenCalledTimes(2) // this should be enough to check if our job is deterministic
expect(endActivity).toHaveBeenCalledTimes(1)
expect(worker.TEST_JOB).toHaveBeenCalledTimes(1)
})
it(`should fail when the worker throws an error`, async () => {
const { enqueueJob } = jobManager
const jobArgs = createInternalMockJob()
const jobArgs2 = createInternalMockJob({ inputPaths: [] })
const jobArgs3 = createInternalMockJob({ args: { key: `value` } })
const jobArgs4 = createInternalMockJob({ args: { key: `value2` } })
worker.TEST_JOB.mockImplementationOnce(() => {
throw new Error(`An error occurred`)
})
.mockImplementationOnce(() =>
Promise.reject(new Error(`An error occurred`))
)
.mockImplementationOnce(() =>
Promise.reject({ message: `An error occurred` })
)
.mockImplementationOnce(() =>
Promise.reject({ key: `weird error object` })
)
expect.assertions(6)
try {
await enqueueJob(jobArgs)
} catch (err) {
expect(err).toMatchInlineSnapshot(`[WorkerError: An error occurred]`)
}
try {
await enqueueJob(jobArgs2)
} catch (err) {
expect(err).toMatchInlineSnapshot(`[WorkerError: An error occurred]`)
}
try {
await enqueueJob(jobArgs3)
} catch (err) {
expect(err).toMatchInlineSnapshot(`[WorkerError: An error occurred]`)
}
try {
await enqueueJob(jobArgs4)
} catch (err) {
expect(err).toMatchInlineSnapshot(
`[WorkerError: {"key":"weird error object"}]`
)
}
expect(endActivity).toHaveBeenCalledTimes(4)
expect(worker.TEST_JOB).toHaveBeenCalledTimes(4)
})
it(`should fail when the worker returns a non object result`, async () => {
const { enqueueJob } = jobManager
const jobArgs = createInternalMockJob()
const jobArgs2 = createInternalMockJob({ inputPaths: [] })
worker.TEST_JOB.mockResolvedValueOnce(`my result`)
worker.TEST_JOB.mockResolvedValueOnce(null)
expect.assertions(2)
try {
await enqueueJob(jobArgs)
} catch (err) {
expect(err).toMatchInlineSnapshot(
`[WorkerError: Result of a worker should be an object, type of "string" was given]`
)
}
await expect(enqueueJob(jobArgs2)).resolves.toBeNull()
})
})
// getInProcessJobPromise
describe(`getInProcessJobPromise`, () => {
// unsure how to test this yet without a real worker
it(`should get a promise when set`, async () => {
worker.TEST_JOB.mockReturnValue({ output: `myresult` })
const { enqueueJob, getInProcessJobPromise } = jobManager
const internalJob = createInternalMockJob()
const promise = enqueueJob(internalJob)
expect(getInProcessJobPromise(internalJob.contentDigest)).toStrictEqual(
promise
)
})
})
describe(`removeInProgressJob`, () => {
// unsure how to test this yet without a real worker
it(`should have all tasks resolved when promise is resolved`, async () => {
worker.TEST_JOB.mockReturnValue({ output: `myresult` })
const { enqueueJob, removeInProgressJob } = jobManager
const internalJob = createInternalMockJob()
await enqueueJob(internalJob)
expect(
jobManager.getInProcessJobPromise(internalJob.contentDigest)
).not.toBeUndefined()
removeInProgressJob(internalJob.contentDigest)
expect(
jobManager.getInProcessJobPromise(internalJob.contentDigest)
).toBeUndefined()
})
})
describe(`waitUntilAllJobsComplete`, () => {
// unsure how to test this yet without a real worker
it(`should have all tasks resolved when promise is resolved`, async () => {
worker.TEST_JOB.mockReturnValue({ output: `myresult` })
const { enqueueJob, waitUntilAllJobsComplete } = jobManager
const promise = enqueueJob(createInternalMockJob())
await waitUntilAllJobsComplete()
expect(worker.TEST_JOB).toHaveBeenCalledTimes(1)
await expect(promise).resolves.toStrictEqual({ output: `myresult` })
})
})
describe(`isJobStale`, () => {
it(`should mark a job as stale if file does not exists`, () => {
const { isJobStale } = jobManager
const inputPaths = [
{
path: `/tmp/unknown-file.jpg`,
contentDigest: `1234`,
},
]
expect(isJobStale({ inputPaths })).toBe(true)
})
it(`should mark a job as stale if contentDigest isn't equal`, () => {
const { isJobStale } = jobManager
const inputPaths = [
{
path: path.resolve(__dirname, `fixtures/input1.jpg`),
contentDigest: `1234`,
},
]
expect(isJobStale({ inputPaths })).toBe(true)
})
it(`shouldn't mark a job as stale if file is the same`, () => {
hasha.fromFileSync = jest.fn().mockReturnValue(`1234`)
const { isJobStale } = jobManager
const inputPaths = [
{
path: path.resolve(__dirname, `fixtures/input1.jpg`),
contentDigest: `1234`,
},
]
expect(isJobStale({ inputPaths })).toBe(false)
})
})
describe(`IPC jobs`, () => {
let listeners = []
let originalProcessOn
let originalSend
/**
* enqueueJob will run some async code before it sends IPC JOB_CREATED message
* This promise allow to await until that moment, to make assertions or execute more code
*/
let waitForJobCreatedIPCSend
beforeEach(() => {
process.env.ENABLE_GATSBY_EXTERNAL_JOBS = `true`
listeners = []
originalProcessOn = process.on
originalSend = process.send
process.on = (type, cb) => {
listeners.push(cb)
}
waitForJobCreatedIPCSend = new Promise(resolve => {
process.send = jest.fn(msg => {
if (msg?.type === MESSAGE_TYPES.JOB_CREATED) {
resolve(msg.payload)
}
})
})
jest.useFakeTimers()
})
afterEach(() => {
jest.runOnlyPendingTimers()
})
afterAll(() => {
delete process.env.ENABLE_GATSBY_EXTERNAL_JOBS
jest.useRealTimers()
process.on = originalProcessOn
process.send = originalSend
})
it(`should schedule a remote job when ipc and env variable are enabled`, async () => {
const { enqueueJob } = jobManager
const jobArgs = createInternalMockJob()
enqueueJob(jobArgs)
await waitForJobCreatedIPCSend
jest.runAllTimers()
expect(process.send).toHaveBeenCalled()
expect(process.send).toHaveBeenCalledWith({
type: `JOB_CREATED`,
payload: jobArgs,
})
expect(listeners.length).toBe(1)
expect(worker.TEST_JOB).not.toHaveBeenCalled()
})
it(`should resolve a job when complete message is received`, async () => {
const { enqueueJob } = jobManager
const jobArgs = createInternalMockJob()
const promise = enqueueJob(jobArgs)
await waitForJobCreatedIPCSend
jest.runAllTimers()
listeners[0]({
type: `JOB_COMPLETED`,
payload: {
id: jobArgs.id,
result: {
output: `hello`,
},
},
})
jest.runAllTimers()
await expect(promise).resolves.toStrictEqual({
output: `hello`,
})
expect(worker.TEST_JOB).not.toHaveBeenCalled()
})
it(`should reject a job when failed message is received`, async () => {
const { enqueueJob } = jobManager
const jobArgs = createInternalMockJob()
const promise = enqueueJob(jobArgs)
await waitForJobCreatedIPCSend
jest.runAllTimers()
listeners[0]({
type: `JOB_FAILED`,
payload: {
id: jobArgs.id,
error: `JOB failed...`,
},
})
jest.runAllTimers()
await expect(promise).rejects.toStrictEqual(
new WorkerError(`JOB failed...`)
)
expect(worker.TEST_JOB).not.toHaveBeenCalled()
})
it(`should run the worker locally when it's not available externally`, async () => {
worker.TEST_JOB.mockReturnValue({ output: `myresult` })
const { enqueueJob } = jobManager
const jobArgs = createInternalMockJob()
const promise = enqueueJob(jobArgs)
await waitForJobCreatedIPCSend
listeners[0]({
type: `JOB_NOT_WHITELISTED`,
payload: {
id: jobArgs.id,
},
})
// Make sure that all the promises get resolved
await new Promise(resolve => {
// If this gets flaky, maybe a waitFor?
timers.setTimeout(resolve, 500)
})
jest.runOnlyPendingTimers()
await expect(promise).resolves.toStrictEqual({ output: `myresult` })
expect(worker.TEST_JOB).toHaveBeenCalledTimes(1)
})
it(`should run the worker locally when it's a local plugin`, async () => {
jest.useRealTimers()
const localPluginPath = slash(
path.resolve(__dirname, `fixtures`, `gatsby-plugin-local`)
)
const localPluginWorkerPath = path.join(localPluginPath, `gatsby-worker`)
const worker = require(localPluginWorkerPath)
const { enqueueJob, createInternalJob } = jobManager
const jobArgs = createInternalJob(createMockJob(), {
name: `gatsby-plugin-local`,
version: `1.0.0`,
resolve: localPluginPath,
})
await expect(enqueueJob(jobArgs)).resolves.toBeUndefined()
expect(process.send).not.toHaveBeenCalled()
expect(worker.TEST_JOB).toHaveBeenCalledTimes(1)
jest.useFakeTimers()
})
it(`shouldn't schedule a remote job when ipc is enabled and env variable is false`, async () => {
process.env.ENABLE_GATSBY_EXTERNAL_JOBS = `false`
jest.useRealTimers()
const { enqueueJob } = jobManager
const jobArgs = createInternalMockJob()
await enqueueJob(jobArgs)
expect(process.send).not.toHaveBeenCalled()
expect(worker.TEST_JOB).toHaveBeenCalled()
jest.useFakeTimers()
})
it(`should warn when external jobs are enabled but ipc isn't used`, async () => {
process.env.ENABLE_GATSBY_EXTERNAL_JOBS = `true`
process.send = null
jest.useRealTimers()
const { enqueueJob } = jobManager
const jobArgs = createInternalMockJob()
const jobArgs2 = createInternalMockJob({
args: { key: `val` },
})
await enqueueJob(jobArgs)
await enqueueJob(jobArgs2)
expect(reporter.warn).toHaveBeenCalledTimes(1)
expect(worker.TEST_JOB).toHaveBeenCalled()
jest.useFakeTimers()
})
})
})