Removed max tasks (#225)
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index 65d667f141a7cefccaf64e5de6525c77a0d37d2b..5d6311b9b4b8b7d581531007803574d83935610b 100644 (file)
@@ -1,5 +1,5 @@
 const expect = require('expect')
-const { FixedThreadPool } = require('../../../lib/index')
+const { FixedClusterPool, FixedThreadPool } = require('../../../lib/index')
 const expectedError = new Error('Worker could not be found in tasks map')
 
 class StubPoolWithTasksMapClear extends FixedThreadPool {
@@ -14,16 +14,13 @@ class StubPoolWithIsMainMethod extends FixedThreadPool {
   }
 }
 
-describe('Abstract pool test suite ', () => {
+describe('Abstract pool test suite', () => {
   it('Simulate worker not found during increaseWorkersTask', () => {
     const pool = new StubPoolWithTasksMapClear(
       1,
-      './tests/worker-files/cluster/testWorker.js',
-      {
-        errorHandler: e => console.error(e)
-      }
+      './tests/worker-files/thread/testWorker.js'
     )
-    // simulate worker not found.
+    // Simulate worker not found.
     pool.removeAllWorker()
     expect(() => pool.increaseWorkersTask()).toThrowError(expectedError)
   })
@@ -31,25 +28,63 @@ describe('Abstract pool test suite ', () => {
   it('Simulate worker not found during decreaseWorkersTasks', () => {
     const pool = new StubPoolWithTasksMapClear(
       1,
-      './tests/worker-files/cluster/testWorker.js',
+      './tests/worker-files/thread/testWorker.js',
       {
         errorHandler: e => console.error(e)
       }
     )
-    // simulate worker not found.
+    // Simulate worker not found.
     pool.removeAllWorker()
     expect(() => pool.decreaseWorkersTasks()).toThrowError(expectedError)
   })
 
   it('Simulate pool creation from a non main thread/process', () => {
-    expect(() => {
-      const pool = new StubPoolWithIsMainMethod(
-        1,
-        './tests/worker-files/cluster/testWorker.js',
-        {
-          errorHandler: e => console.error(e)
-        }
+    expect(
+      () =>
+        new StubPoolWithIsMainMethod(
+          1,
+          './tests/worker-files/thread/testWorker.js',
+          {
+            errorHandler: e => console.error(e)
+          }
+        )
+    ).toThrowError(new Error('Cannot start a pool from a worker!'))
+  })
+
+  it('Verify that filePath is checked', () => {
+    expect(() => new StubPoolWithIsMainMethod(1)).toThrowError(
+      new Error('Cannot start a pool from a worker!')
+    )
+    expect(() => new StubPoolWithIsMainMethod(1, '')).toThrowError(
+      new Error('Cannot start a pool from a worker!')
+    )
+  })
+
+  it('Verify that numberOfWorkers is checked', () => {
+    expect(() => new FixedThreadPool()).toThrowError(
+      new Error(
+        'Cannot instantiate a pool without specifying the number of workers'
       )
-    }).toThrowError()
+    )
+  })
+
+  it('Verify that a negative number of workers is checked', () => {
+    expect(
+      () =>
+        new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js')
+    ).toThrowError(
+      new Error('Cannot instantiate a pool with a negative number of workers')
+    )
+  })
+
+  it('Verify that a non integer number of workers is checked', () => {
+    expect(
+      () =>
+        new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js')
+    ).toThrowError(
+      new Error(
+        'Cannot instantiate a pool with a non integer number of workers'
+      )
+    )
   })
 })