| 1 | const { expect } = require('expect') |
| 2 | const sinon = require('sinon') |
| 3 | const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib') |
| 4 | const { DEFAULT_TASK_NAME, EMPTY_FUNCTION } = require('../../lib/utils') |
| 5 | |
| 6 | describe('Abstract worker test suite', () => { |
| 7 | class StubWorkerWithMainWorker extends ThreadWorker { |
| 8 | constructor (fn, opts) { |
| 9 | super(fn, opts) |
| 10 | this.mainWorker = undefined |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | afterEach(() => { |
| 15 | sinon.restore() |
| 16 | }) |
| 17 | |
| 18 | it('Verify worker options default values', () => { |
| 19 | const worker = new ThreadWorker(() => {}) |
| 20 | expect(worker.opts).toStrictEqual({ |
| 21 | killBehavior: KillBehaviors.SOFT, |
| 22 | maxInactiveTime: 60000, |
| 23 | killHandler: EMPTY_FUNCTION |
| 24 | }) |
| 25 | }) |
| 26 | |
| 27 | it('Verify that worker options are set at worker creation', () => { |
| 28 | const killHandler = () => { |
| 29 | console.info('Worker received kill message') |
| 30 | } |
| 31 | const worker = new ClusterWorker(() => {}, { |
| 32 | killBehavior: KillBehaviors.HARD, |
| 33 | maxInactiveTime: 6000, |
| 34 | killHandler, |
| 35 | async: true |
| 36 | }) |
| 37 | expect(worker.opts).toStrictEqual({ |
| 38 | killBehavior: KillBehaviors.HARD, |
| 39 | maxInactiveTime: 6000, |
| 40 | killHandler |
| 41 | }) |
| 42 | }) |
| 43 | |
| 44 | it('Verify that taskFunctions parameter is mandatory', () => { |
| 45 | expect(() => new ClusterWorker()).toThrowError( |
| 46 | 'taskFunctions parameter is mandatory' |
| 47 | ) |
| 48 | }) |
| 49 | |
| 50 | it('Verify that taskFunctions parameter is a function or a plain object', () => { |
| 51 | expect(() => new ClusterWorker(0)).toThrowError( |
| 52 | new TypeError( |
| 53 | 'taskFunctions parameter is not a function or a plain object' |
| 54 | ) |
| 55 | ) |
| 56 | expect(() => new ClusterWorker('')).toThrowError( |
| 57 | new TypeError( |
| 58 | 'taskFunctions parameter is not a function or a plain object' |
| 59 | ) |
| 60 | ) |
| 61 | expect(() => new ClusterWorker(true)).toThrowError( |
| 62 | new TypeError( |
| 63 | 'taskFunctions parameter is not a function or a plain object' |
| 64 | ) |
| 65 | ) |
| 66 | expect(() => new ClusterWorker([])).toThrowError( |
| 67 | new TypeError( |
| 68 | 'taskFunctions parameter is not a function or a plain object' |
| 69 | ) |
| 70 | ) |
| 71 | expect(() => new ClusterWorker(new Map())).toThrowError( |
| 72 | new TypeError( |
| 73 | 'taskFunctions parameter is not a function or a plain object' |
| 74 | ) |
| 75 | ) |
| 76 | expect(() => new ClusterWorker(new Set())).toThrowError( |
| 77 | new TypeError( |
| 78 | 'taskFunctions parameter is not a function or a plain object' |
| 79 | ) |
| 80 | ) |
| 81 | expect(() => new ClusterWorker(new WeakMap())).toThrowError( |
| 82 | new TypeError( |
| 83 | 'taskFunctions parameter is not a function or a plain object' |
| 84 | ) |
| 85 | ) |
| 86 | expect(() => new ClusterWorker(new WeakSet())).toThrowError( |
| 87 | new TypeError( |
| 88 | 'taskFunctions parameter is not a function or a plain object' |
| 89 | ) |
| 90 | ) |
| 91 | }) |
| 92 | |
| 93 | it('Verify that taskFunctions parameter is not an empty object', () => { |
| 94 | expect(() => new ClusterWorker({})).toThrowError( |
| 95 | new Error('taskFunctions parameter object is empty') |
| 96 | ) |
| 97 | }) |
| 98 | |
| 99 | it('Verify that taskFunctions parameter with unique function is taken', () => { |
| 100 | const worker = new ThreadWorker(() => {}) |
| 101 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
| 102 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 103 | expect(worker.taskFunctions.size).toBe(2) |
| 104 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 105 | worker.taskFunctions.get('fn1') |
| 106 | ) |
| 107 | }) |
| 108 | |
| 109 | it('Verify that taskFunctions parameter with multiple task functions is checked', () => { |
| 110 | const fn1 = () => { |
| 111 | return 1 |
| 112 | } |
| 113 | const fn2 = '' |
| 114 | expect(() => new ThreadWorker({ '': fn1 })).toThrowError( |
| 115 | new TypeError('A taskFunctions parameter object key is an empty string') |
| 116 | ) |
| 117 | expect(() => new ThreadWorker({ fn1, fn2 })).toThrowError( |
| 118 | new TypeError('A taskFunctions parameter object value is not a function') |
| 119 | ) |
| 120 | }) |
| 121 | |
| 122 | it('Verify that taskFunctions parameter with multiple task functions is taken', () => { |
| 123 | const fn1 = () => { |
| 124 | return 1 |
| 125 | } |
| 126 | const fn2 = () => { |
| 127 | return 2 |
| 128 | } |
| 129 | const worker = new ClusterWorker({ fn1, fn2 }) |
| 130 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
| 131 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 132 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) |
| 133 | expect(worker.taskFunctions.size).toBe(3) |
| 134 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 135 | worker.taskFunctions.get('fn1') |
| 136 | ) |
| 137 | }) |
| 138 | |
| 139 | it('Verify that sync kill handler is called when worker is killed', () => { |
| 140 | const worker = new ClusterWorker(() => {}, { |
| 141 | killHandler: sinon.stub().returns() |
| 142 | }) |
| 143 | worker.isMain = false |
| 144 | worker.getMainWorker = sinon.stub().returns({ |
| 145 | id: 1, |
| 146 | send: sinon.stub().returns() |
| 147 | }) |
| 148 | worker.handleKillMessage() |
| 149 | expect(worker.getMainWorker().send.calledOnce).toBe(true) |
| 150 | expect(worker.opts.killHandler.calledOnce).toBe(true) |
| 151 | }) |
| 152 | |
| 153 | it('Verify that async kill handler is called when worker is killed', () => { |
| 154 | const killHandlerStub = sinon.stub().returns() |
| 155 | const worker = new ClusterWorker(() => {}, { |
| 156 | killHandler: async () => Promise.resolve(killHandlerStub()) |
| 157 | }) |
| 158 | worker.isMain = false |
| 159 | worker.handleKillMessage() |
| 160 | expect(killHandlerStub.calledOnce).toBe(true) |
| 161 | }) |
| 162 | |
| 163 | it('Verify that handleError() method works properly', () => { |
| 164 | const error = new Error('Error as an error') |
| 165 | const worker = new ClusterWorker(() => {}) |
| 166 | expect(worker.handleError(error)).not.toBeInstanceOf(Error) |
| 167 | expect(worker.handleError(error)).toStrictEqual(error.message) |
| 168 | const errorMessage = 'Error as a string' |
| 169 | expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage) |
| 170 | }) |
| 171 | |
| 172 | it('Verify that getMainWorker() throw error if main worker is not set', () => { |
| 173 | expect(() => |
| 174 | new StubWorkerWithMainWorker(() => {}).getMainWorker() |
| 175 | ).toThrowError('Main worker not set') |
| 176 | }) |
| 177 | |
| 178 | it('Verify that hasTaskFunction() works', () => { |
| 179 | const fn1 = () => { |
| 180 | return 1 |
| 181 | } |
| 182 | const fn2 = () => { |
| 183 | return 2 |
| 184 | } |
| 185 | const worker = new ClusterWorker({ fn1, fn2 }) |
| 186 | expect(worker.hasTaskFunction(0)).toStrictEqual({ |
| 187 | status: false, |
| 188 | error: new TypeError('name parameter is not a string') |
| 189 | }) |
| 190 | expect(worker.hasTaskFunction('')).toStrictEqual({ |
| 191 | status: false, |
| 192 | error: new TypeError('name parameter is an empty string') |
| 193 | }) |
| 194 | expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({ |
| 195 | status: true |
| 196 | }) |
| 197 | expect(worker.hasTaskFunction('fn1')).toStrictEqual({ status: true }) |
| 198 | expect(worker.hasTaskFunction('fn2')).toStrictEqual({ status: true }) |
| 199 | expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false }) |
| 200 | }) |
| 201 | |
| 202 | it('Verify that addTaskFunction() works', () => { |
| 203 | const fn1 = () => { |
| 204 | return 1 |
| 205 | } |
| 206 | const fn2 = () => { |
| 207 | return 2 |
| 208 | } |
| 209 | const fn1Replacement = () => { |
| 210 | return 3 |
| 211 | } |
| 212 | const worker = new ThreadWorker(fn1) |
| 213 | expect(worker.addTaskFunction(0, fn1)).toStrictEqual({ |
| 214 | status: false, |
| 215 | error: new TypeError('name parameter is not a string') |
| 216 | }) |
| 217 | expect(worker.addTaskFunction('', fn1)).toStrictEqual({ |
| 218 | status: false, |
| 219 | error: new TypeError('name parameter is an empty string') |
| 220 | }) |
| 221 | expect(worker.addTaskFunction('fn3', '')).toStrictEqual({ |
| 222 | status: false, |
| 223 | error: new TypeError('fn parameter is not a function') |
| 224 | }) |
| 225 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
| 226 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 227 | expect(worker.taskFunctions.size).toBe(2) |
| 228 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 229 | worker.taskFunctions.get('fn1') |
| 230 | ) |
| 231 | expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({ |
| 232 | status: false, |
| 233 | error: new Error( |
| 234 | 'Cannot add a task function with the default reserved name' |
| 235 | ) |
| 236 | }) |
| 237 | worker.addTaskFunction('fn2', fn2) |
| 238 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
| 239 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 240 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) |
| 241 | expect(worker.taskFunctions.size).toBe(3) |
| 242 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 243 | worker.taskFunctions.get('fn1') |
| 244 | ) |
| 245 | worker.addTaskFunction('fn1', fn1Replacement) |
| 246 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
| 247 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 248 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) |
| 249 | expect(worker.taskFunctions.size).toBe(3) |
| 250 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 251 | worker.taskFunctions.get('fn1') |
| 252 | ) |
| 253 | }) |
| 254 | |
| 255 | it('Verify that removeTaskFunction() works', () => { |
| 256 | const fn1 = () => { |
| 257 | return 1 |
| 258 | } |
| 259 | const fn2 = () => { |
| 260 | return 2 |
| 261 | } |
| 262 | const worker = new ClusterWorker({ fn1, fn2 }) |
| 263 | expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({ |
| 264 | status: false, |
| 265 | error: new TypeError('name parameter is not a string') |
| 266 | }) |
| 267 | expect(worker.removeTaskFunction('', fn1)).toStrictEqual({ |
| 268 | status: false, |
| 269 | error: new TypeError('name parameter is an empty string') |
| 270 | }) |
| 271 | worker.getMainWorker = sinon.stub().returns({ |
| 272 | id: 1, |
| 273 | send: sinon.stub().returns() |
| 274 | }) |
| 275 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
| 276 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 277 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) |
| 278 | expect(worker.taskFunctions.size).toBe(3) |
| 279 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 280 | worker.taskFunctions.get('fn1') |
| 281 | ) |
| 282 | expect(worker.removeTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({ |
| 283 | status: false, |
| 284 | error: new Error( |
| 285 | 'Cannot remove the task function with the default reserved name' |
| 286 | ) |
| 287 | }) |
| 288 | expect(worker.removeTaskFunction('fn1')).toStrictEqual({ |
| 289 | status: false, |
| 290 | error: new Error( |
| 291 | 'Cannot remove the task function used as the default task function' |
| 292 | ) |
| 293 | }) |
| 294 | worker.removeTaskFunction('fn2') |
| 295 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
| 296 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 297 | expect(worker.taskFunctions.get('fn2')).toBeUndefined() |
| 298 | expect(worker.taskFunctions.size).toBe(2) |
| 299 | expect(worker.getMainWorker().send.calledOnce).toBe(true) |
| 300 | }) |
| 301 | |
| 302 | it('Verify that listTaskFunctions() works', () => { |
| 303 | const fn1 = () => { |
| 304 | return 1 |
| 305 | } |
| 306 | const fn2 = () => { |
| 307 | return 2 |
| 308 | } |
| 309 | const worker = new ClusterWorker({ fn1, fn2 }) |
| 310 | expect(worker.listTaskFunctionNames()).toStrictEqual([ |
| 311 | DEFAULT_TASK_NAME, |
| 312 | 'fn1', |
| 313 | 'fn2' |
| 314 | ]) |
| 315 | }) |
| 316 | |
| 317 | it('Verify that setDefaultTaskFunction() works', () => { |
| 318 | const fn1 = () => { |
| 319 | return 1 |
| 320 | } |
| 321 | const fn2 = () => { |
| 322 | return 2 |
| 323 | } |
| 324 | const worker = new ThreadWorker({ fn1, fn2 }) |
| 325 | expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({ |
| 326 | status: false, |
| 327 | error: new TypeError('name parameter is not a string') |
| 328 | }) |
| 329 | expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({ |
| 330 | status: false, |
| 331 | error: new TypeError('name parameter is an empty string') |
| 332 | }) |
| 333 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
| 334 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 335 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) |
| 336 | expect(worker.taskFunctions.size).toBe(3) |
| 337 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 338 | worker.taskFunctions.get('fn1') |
| 339 | ) |
| 340 | expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({ |
| 341 | status: false, |
| 342 | error: new Error( |
| 343 | 'Cannot set the default task function reserved name as the default task function' |
| 344 | ) |
| 345 | }) |
| 346 | expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({ |
| 347 | status: false, |
| 348 | error: new Error( |
| 349 | 'Cannot set the default task function to a non-existing task function' |
| 350 | ) |
| 351 | }) |
| 352 | worker.setDefaultTaskFunction('fn1') |
| 353 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 354 | worker.taskFunctions.get('fn1') |
| 355 | ) |
| 356 | worker.setDefaultTaskFunction('fn2') |
| 357 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 358 | worker.taskFunctions.get('fn2') |
| 359 | ) |
| 360 | }) |
| 361 | }) |