Commit | Line | Data |
---|---|---|
a074ffee | 1 | import { expect } from 'expect' |
bb3d0b23 | 2 | import { restore, stub } from 'sinon' |
d35e5717 JB |
3 | import { ClusterWorker } from '../../lib/index.cjs' |
4 | import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs' | |
fd027a65 S |
5 | |
6 | describe('Cluster worker test suite', () => { | |
bb3d0b23 JB |
7 | afterEach(() => { |
8 | restore() | |
9 | }) | |
10 | ||
f80125ca JB |
11 | it('Verify that sync kill handler is called when worker is killed', () => { |
12 | const worker = new ClusterWorker(() => {}, { | |
13 | killHandler: stub().returns() | |
14 | }) | |
15 | worker.isMain = false | |
16 | worker.getMainWorker = stub().returns({ | |
17 | id: 1, | |
18 | send: stub().returns() | |
19 | }) | |
20 | worker.handleKillMessage() | |
5eb72b9e | 21 | expect(worker.getMainWorker.calledTwice).toBe(true) |
f80125ca JB |
22 | expect(worker.getMainWorker().send.calledOnce).toBe(true) |
23 | expect(worker.opts.killHandler.calledOnce).toBe(true) | |
24 | }) | |
25 | ||
26 | it('Verify that removeTaskFunction() is working', () => { | |
27 | const fn1 = () => { | |
28 | return 1 | |
29 | } | |
30 | const fn2 = () => { | |
31 | return 2 | |
32 | } | |
33 | const worker = new ClusterWorker({ fn1, fn2 }) | |
5eb72b9e JB |
34 | worker.getMainWorker = stub().returns({ |
35 | id: 1, | |
36 | send: stub().returns() | |
37 | }) | |
f80125ca JB |
38 | expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({ |
39 | status: false, | |
40 | error: new TypeError('name parameter is not a string') | |
41 | }) | |
42 | expect(worker.removeTaskFunction('', fn1)).toStrictEqual({ | |
43 | status: false, | |
44 | error: new TypeError('name parameter is an empty string') | |
45 | }) | |
f80125ca JB |
46 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
47 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) | |
48 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) | |
49 | expect(worker.taskFunctions.size).toBe(3) | |
50 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( | |
51 | worker.taskFunctions.get('fn1') | |
52 | ) | |
53 | expect(worker.removeTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({ | |
54 | status: false, | |
55 | error: new Error( | |
56 | 'Cannot remove the task function with the default reserved name' | |
57 | ) | |
58 | }) | |
59 | expect(worker.removeTaskFunction('fn1')).toStrictEqual({ | |
60 | status: false, | |
61 | error: new Error( | |
62 | 'Cannot remove the task function used as the default task function' | |
63 | ) | |
64 | }) | |
65 | worker.removeTaskFunction('fn2') | |
66 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) | |
67 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) | |
68 | expect(worker.taskFunctions.get('fn2')).toBeUndefined() | |
69 | expect(worker.taskFunctions.size).toBe(2) | |
5eb72b9e | 70 | expect(worker.getMainWorker.calledTwice).toBe(true) |
f80125ca JB |
71 | expect(worker.getMainWorker().send.calledOnce).toBe(true) |
72 | }) | |
73 | ||
18ecc6c5 JB |
74 | it('Verify that handleError() method is working properly', () => { |
75 | const error = new Error('Error as an error') | |
76 | const worker = new ClusterWorker(() => {}) | |
77 | expect(worker.handleError(error)).not.toBeInstanceOf(Error) | |
78 | expect(worker.handleError(error)).toStrictEqual(error.message) | |
79 | const errorMessage = 'Error as a string' | |
80 | expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage) | |
81 | }) | |
82 | ||
5eb72b9e JB |
83 | it('Verify that sendToMainWorker() method invokes the getMainWorker() and send() methods', () => { |
84 | const worker = new ClusterWorker(() => {}) | |
85 | worker.getMainWorker = stub().returns({ | |
86 | send: stub().returns() | |
87 | }) | |
f4fb3543 | 88 | worker.sendToMainWorker({ ok: 1 }) |
5eb72b9e | 89 | expect(worker.getMainWorker.calledTwice).toBe(true) |
bb3d0b23 | 90 | expect(worker.getMainWorker().send.calledOnce).toBe(true) |
f4fb3543 | 91 | }) |
fd027a65 | 92 | }) |