test: add tests
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 28 Aug 2023 14:11:40 +0000 (16:11 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 28 Aug 2023 14:11:40 +0000 (16:11 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
tests/pools/cluster/fixed.test.js
tests/pools/thread/fixed.test.js

index 6edfcd67b8c53517b7f4d635b3738ca43131a76b..675bf70759ebb9911198f1a15012420c384bb04a 100644 (file)
@@ -276,16 +276,38 @@ describe('Fixed cluster pool test suite', () => {
   })
 
   it('Should work even without opts in input', async () => {
-    const pool = new FixedClusterPool(
-      numberOfWorkers,
-      './tests/worker-files/cluster/testWorker.js'
-    )
+    const workerFilePath = './tests/worker-files/cluster/testWorker.js'
+    const pool = new FixedClusterPool(numberOfWorkers, workerFilePath)
     const res = await pool.execute()
     expect(res).toStrictEqual({ ok: 1 })
     // We need to clean up the resources after our test
     await pool.destroy()
   })
 
+  it('Verify destroyWorkerNode()', async () => {
+    const workerFilePath = './tests/worker-files/cluster/testWorker.js'
+    const pool = new FixedClusterPool(numberOfWorkers, workerFilePath)
+    let disconnectEvent = 0
+    pool.workerNodes[0].worker.on('disconnect', () => {
+      ++disconnectEvent
+    })
+    let exitEvent = 0
+    pool.workerNodes[0].worker.on('exit', () => {
+      ++exitEvent
+    })
+    let error
+    try {
+      await pool.destroyWorkerNode(0)
+    } catch (e) {
+      error = e
+    }
+    expect(error).toBeUndefined()
+    expect(disconnectEvent).toBe(1)
+    expect(exitEvent).toBe(1)
+    expect(pool.workerNodes.length).toBe(numberOfWorkers - 1)
+    await pool.destroy()
+  })
+
   it('Verify that a pool with zero worker fails', async () => {
     expect(
       () =>
index 065774ec695ef29a3c94dec2a96830d5f82cd68d..ec756aa53b99097e23568d0c02e0a586031c0e6c 100644 (file)
@@ -301,16 +301,33 @@ describe('Fixed thread pool test suite', () => {
   })
 
   it('Should work even without opts in input', async () => {
-    const pool = new FixedThreadPool(
-      numberOfThreads,
-      './tests/worker-files/thread/testWorker.js'
-    )
+    const workerFilePath = './tests/worker-files/thread/testWorker.js'
+    const pool = new FixedThreadPool(numberOfThreads, workerFilePath)
     const res = await pool.execute()
     expect(res).toStrictEqual({ ok: 1 })
     // We need to clean up the resources after our test
     await pool.destroy()
   })
 
+  it('Verify destroyWorkerNode()', async () => {
+    const workerFilePath = './tests/worker-files/thread/testWorker.js'
+    const pool = new FixedThreadPool(numberOfThreads, workerFilePath)
+    let exitEvent = 0
+    pool.workerNodes[0].worker.on('exit', () => {
+      ++exitEvent
+    })
+    let error
+    try {
+      await pool.destroyWorkerNode(0)
+    } catch (e) {
+      error = e
+    }
+    expect(error).toBeUndefined()
+    expect(exitEvent).toBe(1)
+    expect(pool.workerNodes.length).toBe(numberOfThreads - 1)
+    await pool.destroy()
+  })
+
   it('Verify that a pool with zero worker fails', async () => {
     expect(
       () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js')