} = require('../../../lib/index')
describe('Selection strategies test suite', () => {
+ const min = 0
+ const max = 3
+
it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN')
expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED')
+ expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE')
expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe(
'WEIGHTED_ROUND_ROBIN'
)
})
it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
- const min = 0
- const max = 3
const pool = new DynamicThreadPool(
min,
max,
})
it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
- const min = 0
- const max = 3
const pool = new DynamicThreadPool(
min,
max,
await pool.destroy()
})
+ it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
+ let pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .requiredStatistics.runTime
+ ).toBe(false)
+ pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .requiredStatistics.runTime
+ ).toBe(false)
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js',
// TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// We need to clean up the resources after our test
})
it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
- const min = 0
- const max = 3
const pool = new DynamicThreadPool(
min,
max,
// TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// We need to clean up the resources after our test
})
it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js',
})
it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js'
await pool.destroy()
})
+ it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
+ let pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .requiredStatistics.runTime
+ ).toBe(false)
+ pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .requiredStatistics.runTime
+ ).toBe(false)
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js',
// TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// We need to clean up the resources after our test
})
it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
- const min = 0
- const max = 3
const pool = new DynamicThreadPool(
min,
max,
// TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
+ }
+ await Promise.all(promises)
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
+ const pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
+ )
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.FAIR_SHARE
+ )
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
+ const pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.FAIR_SHARE
+ )
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
+ let pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .requiredStatistics.runTime
+ ).toBe(true)
+ pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .requiredStatistics.runTime
+ ).toBe(true)
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
+ const pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
+ )
+ // TODO: Create a better test to cover `FairShareChoiceStrategy#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 FAIR_SHARE strategy can be run in a dynamic pool', async () => {
+ const pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
+ )
+ // TODO: Create a better test to cover `FairShareChoiceStrategy#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
})
it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js',
})
it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js'
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.getWorkerChoiceStrategy()
+ .requiredStatistics.runTime
+ ).toBe(true)
+ pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .requiredStatistics.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 max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js',
// TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// We need to clean up the resources after our test
})
it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
- const min = 0
- const max = 3
const pool = new DynamicThreadPool(
min,
max,
// TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// We need to clean up the resources after our test
})
it('Verify unknown strategies throw error', () => {
- const min = 1
- const max = 3
expect(
() =>
new DynamicThreadPool(