fix: ensure task error proper throw with worker-threads
[poolifier.git] / tests / utils.test.js
index 21a6a7c4aac6d07d8e1ec215ff8df9703a30f1f2..0bf5e50ca8a4989099500100ff2b097a96092713 100644 (file)
@@ -1,13 +1,39 @@
 const { expect } = require('expect')
-const { isPlainObject, median } = require('../lib/utils')
+const {
+  availableParallelism,
+  isPlainObject,
+  median,
+  round
+} = require('../lib/utils')
+const {
+  isKillBehavior,
+  KillBehaviors
+} = require('../lib/worker/worker-options')
 
 describe('Utils test suite', () => {
-  it('Verify median computation', () => {
+  it('Verify availableParallelism() behavior', () => {
+    expect(typeof availableParallelism() === 'number').toBe(true)
+  })
+
+  it('Verify median() computation', () => {
     expect(median([])).toBe(0)
-    const array0 = [0.08]
-    expect(median(array0)).toBe(0.08)
-    const array1 = [0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03]
-    expect(median(array1)).toBe(3.05)
+    expect(median([0.08])).toBe(0.08)
+    expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.05)
+    expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535)
+  })
+
+  it('Verify round() behavior', () => {
+    expect(round(0)).toBe(0)
+    expect(round(0.5, 0)).toBe(1)
+    expect(round(0.5)).toBe(0.5)
+    expect(round(-0.5, 0)).toBe(-1)
+    expect(round(-0.5)).toBe(-0.5)
+    expect(round(1.005)).toBe(1.01)
+    expect(round(2.175)).toBe(2.18)
+    expect(round(5.015)).toBe(5.02)
+    expect(round(-1.005)).toBe(-1.01)
+    expect(round(-2.175)).toBe(-2.18)
+    expect(round(-5.015)).toBe(-5.02)
   })
 
   it('Verify isPlainObject() behavior', () => {
@@ -46,4 +72,15 @@ describe('Utils test suite', () => {
     expect(isPlainObject({})).toBe(true)
     expect(isPlainObject({ a: 1 })).toBe(true)
   })
+
+  it('Verify isKillBehavior() behavior', () => {
+    expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.SOFT)).toBe(true)
+    expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.HARD)).toBe(false)
+    expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.HARD)).toBe(true)
+    expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.SOFT)).toBe(false)
+    expect(isKillBehavior(KillBehaviors.SOFT)).toBe(false)
+    expect(isKillBehavior(KillBehaviors.HARD)).toBe(false)
+    expect(isKillBehavior(KillBehaviors.HARD, null)).toBe(false)
+    expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false)
+  })
 })