+ // expect(
+ // pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ // .workerChoiceStrategy.workerLastVirtualTaskTimestamp.size
+ // ).toBe(pool.workers.length)
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
+ let pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .workerLastVirtualTaskTimestamp
+ ).toBeUndefined()
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
+ for (const workerKey of pool.workerChoiceStrategyContext
+ .getWorkerChoiceStrategy()
+ .workerLastVirtualTaskTimestamp.keys()) {
+ expect(
+ pool.workerChoiceStrategyContext
+ .getWorkerChoiceStrategy()
+ .workerLastVirtualTaskTimestamp.get(workerKey).start
+ ).toBe(0)
+ expect(
+ pool.workerChoiceStrategyContext
+ .getWorkerChoiceStrategy()
+ .workerLastVirtualTaskTimestamp.get(workerKey).end
+ ).toBe(0)
+ }
+ await pool.destroy()
+ pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .workerChoiceStrategy.workerLastVirtualTaskTimestamp
+ ).toBeUndefined()
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
+ for (const workerKey of pool.workerChoiceStrategyContext
+ .getWorkerChoiceStrategy()
+ .workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
+ expect(
+ pool.workerChoiceStrategyContext
+ .getWorkerChoiceStrategy()
+ .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(workerKey)
+ .start
+ ).toBe(0)
+ expect(
+ pool.workerChoiceStrategyContext
+ .getWorkerChoiceStrategy()
+ .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(workerKey)
+ .end
+ ).toBe(0)
+ }
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
+ const pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
+ )
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+ )
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
+ ).toBe(0)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .defaultWorkerWeight
+ ).toBeGreaterThan(0)
+ for (const workerKey of pool.workerChoiceStrategyContext
+ .getWorkerChoiceStrategy()
+ .workersTaskRunTime.keys()) {
+ expect(
+ pool.workerChoiceStrategyContext
+ .getWorkerChoiceStrategy()
+ .workersTaskRunTime.get(workerKey).weight
+ ).toBeGreaterThan(0)
+ expect(
+ pool.workerChoiceStrategyContext
+ .getWorkerChoiceStrategy()
+ .workersTaskRunTime.get(workerKey).runTime
+ ).toBe(0)
+ }
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
+ const pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+ )
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
+ let pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+ expect(
+ pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
+ ).toBe(true)
+ await pool.destroy()
+ pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+ expect(
+ pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
+ ).toBe(true)
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
+ const pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
+ )
+ // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
+ const promises = []
+ for (let i = 0; i < max * 2; i++) {
+ promises.push(pool.execute())
+ }
+ await Promise.all(promises)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .workersTaskRunTime.size
+ ).toBe(pool.workers.length)
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
+ const pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
+ )
+ // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
+ const promises = []
+ for (let i = 0; i < max * 2; i++) {
+ promises.push(pool.execute())
+ }
+ await Promise.all(promises)
+ // expect(
+ // pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ // .workerChoiceStrategy.workersTaskRunTime.size
+ // ).toBe(pool.workers.length)
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
+ let pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
+ ).toBeUndefined()
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .defaultWorkerWeight
+ ).toBeUndefined()
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .workersTaskRunTime
+ ).toBeUndefined()
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
+ ).toBe(0)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .defaultWorkerWeight
+ ).toBeGreaterThan(0)
+ for (const workerKey of pool.workerChoiceStrategyContext
+ .getWorkerChoiceStrategy()
+ .workersTaskRunTime.keys()) {
+ expect(
+ pool.workerChoiceStrategyContext
+ .getWorkerChoiceStrategy()
+ .workersTaskRunTime.get(workerKey).runTime
+ ).toBe(0)
+ }
+ await pool.destroy()
+ pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .workerChoiceStrategy.currentWorkerId
+ ).toBeUndefined()
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .workerChoiceStrategy.defaultWorkerWeight
+ ).toBeUndefined()
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .workerChoiceStrategy.workersTaskRunTime
+ ).toBeUndefined()
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .workerChoiceStrategy.currentWorkerId
+ ).toBe(0)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .workerChoiceStrategy.defaultWorkerWeight
+ ).toBeGreaterThan(0)
+ for (const workerKey of pool.workerChoiceStrategyContext
+ .getWorkerChoiceStrategy()
+ .workerChoiceStrategy.workersTaskRunTime.keys()) {
+ expect(
+ pool.workerChoiceStrategyContext
+ .getWorkerChoiceStrategy()
+ .workerChoiceStrategy.workersTaskRunTime.get(workerKey).runTime
+ ).toBe(0)
+ }