chore: v2.4.6
[poolifier.git] / tests / worker / abstract-worker.test.js
index ddb8427b82f5dd41c5273815a6dfd51446ebd45e..fbc9a06d26082b007d01572e95f02f610dc1f1a5 100644 (file)
@@ -1,10 +1,53 @@
-const expect = require('expect')
-const { ThreadWorker } = require('../../lib')
+const { expect } = require('expect')
+const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib')
 
 describe('Abstract worker test suite', () => {
-  it('Verify that fn function is mandatory', () => {
-    expect(() => {
-      const worker = new ThreadWorker()
-    }).toThrowError()
+  class StubPoolWithIsMainWorker extends ThreadWorker {
+    constructor (fn, opts) {
+      super(fn, opts)
+      this.mainWorker = undefined
+    }
+  }
+
+  it('Verify that fn parameter is mandatory', () => {
+    expect(() => new ClusterWorker()).toThrowError(
+      new Error('fn parameter is mandatory')
+    )
+  })
+
+  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)).toStrictEqual(error)
+  })
+
+  it('Verify that get main worker throw error if main worker is not set', () => {
+    expect(() =>
+      new StubPoolWithIsMainWorker(() => {}).getMainWorker()
+    ).toThrowError(new Error('Main worker was not set'))
   })
 })