chore: v2.4.6
[poolifier.git] / tests / worker / abstract-worker.test.js
index 1d0c03d9c398c75a5c88b21cb8a7ff1cb0dfc2c1..fbc9a06d26082b007d01572e95f02f610dc1f1a5 100644 (file)
@@ -1,29 +1,53 @@
-const expect = require('expect')
-const { ClusterWorker, ThreadWorker } = require('../../lib')
+const { expect } = require('expect')
+const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib')
 
-class StubPoolWithIsMainWorker extends ThreadWorker {
-  constructor (fn, opts) {
-    super(fn, opts)
-    this.mainWorker = false
+describe('Abstract worker test suite', () => {
+  class StubPoolWithIsMainWorker extends ThreadWorker {
+    constructor (fn, opts) {
+      super(fn, opts)
+      this.mainWorker = undefined
+    }
   }
-}
 
-describe('Abstract worker test suite', () => {
-  it('Verify that fn function is mandatory', () => {
+  it('Verify that fn parameter is mandatory', () => {
     expect(() => new ClusterWorker()).toThrowError(
       new Error('fn parameter is mandatory')
     )
   })
 
-  it('Verify that handle Error function is working properly', () => {
+  it('Verify that fn parameter is a function', () => {
+    expect(() => new ClusterWorker({})).toThrowError(
+      new TypeError('fn parameter is not a function')
+    )
+  })
+
+  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.async).toBe(false)
+  })
+
+  it('Verify that worker options are set at worker creation', () => {
+    const worker = new ClusterWorker(() => {}, {
+      maxInactiveTime: 6000,
+      async: true,
+      killBehavior: KillBehaviors.HARD
+    })
+    expect(worker.opts.maxInactiveTime).toStrictEqual(6000)
+    expect(worker.opts.killBehavior).toBe(KillBehaviors.HARD)
+    expect(worker.opts.async).toBe(true)
+  })
+
+  it('Verify that handleError function is working properly', () => {
     const error = new Error('My error')
     const worker = new ThreadWorker(() => {})
-    expect(worker.handleError(error)).toBe(error)
+    expect(worker.handleError(error)).toStrictEqual(error)
   })
 
   it('Verify that get main worker throw error if main worker is not set', () => {
     expect(() =>
       new StubPoolWithIsMainWorker(() => {}).getMainWorker()
-    ).toThrowError()
+    ).toThrowError(new Error('Main worker was not set'))
   })
 })