it('Verify worker options default values', () => {
const worker = new ThreadWorker(() => {})
- expect(worker.opts.maxInactiveTime).toStrictEqual(60000)
- expect(worker.opts.killBehavior).toBe(KillBehaviors.SOFT)
- expect(worker.opts.killHandler).toStrictEqual(EMPTY_FUNCTION)
- expect(worker.opts.async).toBe(undefined)
+ expect(worker.opts).toStrictEqual({
+ killBehavior: KillBehaviors.SOFT,
+ maxInactiveTime: 60000,
+ killHandler: EMPTY_FUNCTION
+ })
+ })
+
+ it('Verify that worker options are checked at worker creation', () => {
+ expect(() => new ClusterWorker(() => {}, '')).toThrowError(
+ new TypeError('opts worker options parameter is not a plain object')
+ )
+ expect(
+ () => new ClusterWorker(() => {}, { killBehavior: '' })
+ ).toThrowError(new TypeError("killBehavior option '' is not valid"))
+ expect(() => new ClusterWorker(() => {}, { killBehavior: 0 })).toThrowError(
+ new TypeError("killBehavior option '0' is not valid")
+ )
+ expect(
+ () => new ThreadWorker(() => {}, { maxInactiveTime: '' })
+ ).toThrowError(new TypeError('maxInactiveTime option is not an integer'))
+ expect(
+ () => new ThreadWorker(() => {}, { maxInactiveTime: 0.5 })
+ ).toThrowError(new TypeError('maxInactiveTime option is not an integer'))
+ expect(
+ () => new ThreadWorker(() => {}, { maxInactiveTime: 0 })
+ ).toThrowError(
+ new TypeError(
+ 'maxInactiveTime option is not a positive integer greater or equal than 5'
+ )
+ )
+ expect(
+ () => new ThreadWorker(() => {}, { maxInactiveTime: 4 })
+ ).toThrowError(
+ new TypeError(
+ 'maxInactiveTime option is not a positive integer greater or equal than 5'
+ )
+ )
+ expect(() => new ThreadWorker(() => {}, { killHandler: '' })).toThrowError(
+ new TypeError('killHandler option is not a function')
+ )
+ expect(() => new ThreadWorker(() => {}, { killHandler: 0 })).toThrowError(
+ new TypeError('killHandler option is not a function')
+ )
+ expect(() => new ThreadWorker(() => {}, { async: true })).toThrowError(
+ new TypeError('async option is deprecated')
+ )
})
it('Verify that worker options are set at worker creation', () => {
console.info('Worker received kill message')
}
const worker = new ClusterWorker(() => {}, {
+ killBehavior: KillBehaviors.HARD,
maxInactiveTime: 6000,
+ killHandler
+ })
+ expect(worker.opts).toStrictEqual({
killBehavior: KillBehaviors.HARD,
- killHandler,
- async: true
+ maxInactiveTime: 6000,
+ killHandler
})
- expect(worker.opts.maxInactiveTime).toStrictEqual(6000)
- expect(worker.opts.killBehavior).toBe(KillBehaviors.HARD)
- expect(worker.opts.killHandler).toStrictEqual(killHandler)
- expect(worker.opts.async).toBe(undefined)
})
it('Verify that taskFunctions parameter is mandatory', () => {
expect(() => new ClusterWorker()).toThrowError(
- 'taskFunctions parameter is mandatory'
+ new Error('taskFunctions parameter is mandatory')
)
})