+ it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
+ let pool = new FixedClusterPool(
+ max,
+ './tests/worker-files/cluster/testWorker.js'
+ )
+ let results = new Set()
+ for (let i = 0; i < max; i++) {
+ results.add(pool.chooseWorkerNode()[1].worker.id)
+ }
+ expect(results.size).toBe(max)
+ await pool.destroy()
+ pool = new FixedThreadPool(max, './tests/worker-files/thread/testWorker.js')
+ results = new Set()
+ for (let i = 0; i < max; i++) {
+ results.add(pool.chooseWorkerNode()[1].worker.threadId)
+ }
+ expect(results.size).toBe(max)
+ await pool.destroy()
+ })
+
+ it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => {
+ let pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
+ )
+ expect(
+ pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ ).nextWorkerNodeId
+ ).toBeDefined()
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
+ expect(
+ pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ ).nextWorkerNodeId
+ ).toBe(0)
+ await pool.destroy()
+ pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
+ )
+ expect(
+ pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ ).nextWorkerNodeId
+ ).toBeDefined()
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
+ expect(
+ pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ ).nextWorkerNodeId
+ ).toBe(0)
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify LESS_USED strategy is taken at pool creation', async () => {
+ const pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
+ )
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.LESS_USED
+ )
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify LESS_USED strategy can be set after pool creation', async () => {
+ const pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.LESS_USED
+ )
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify LESS_USED strategy default tasks usage statistics requirements', async () => {
+ let pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
+ expect(
+ pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
+ ).toBe(false)
+ expect(
+ pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
+ ).toBe(false)
+ expect(
+ pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+ ).toBe(false)
+ await pool.destroy()
+ pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
+ expect(
+ pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
+ ).toBe(false)
+ expect(
+ pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
+ ).toBe(false)
+ expect(
+ pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+ ).toBe(false)
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify LESS_USED strategy can be run in a fixed pool', async () => {
+ const pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
+ )
+ // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
+ const promises = []
+ for (let i = 0; i < max * 2; i++) {
+ promises.push(pool.execute())
+ }
+ await Promise.all(promises)
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify LESS_USED strategy can be run in a dynamic pool', async () => {
+ const pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
+ )
+ // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
+ const promises = []
+ for (let i = 0; i < max * 2; i++) {
+ promises.push(pool.execute())
+ }
+ await Promise.all(promises)
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify LESS_BUSY strategy is taken at pool creation', async () => {