const {
WorkerChoiceStrategies,
DynamicThreadPool,
- FixedThreadPool
+ FixedThreadPool,
+ FixedClusterPool
} = require('../../../lib/index')
describe('Selection strategies test suite', () => {
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.LESS_USED).toBe('LESS_USED')
+ expect(WorkerChoiceStrategies.LESS_BUSY).toBe('LESS_BUSY')
expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE')
expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe(
'WEIGHTED_ROUND_ROBIN'
WorkerChoiceStrategies.ROUND_ROBIN
)
expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerIndex
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerId
).toBe(0)
// We need to clean up the resources after our test
await pool.destroy()
)
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .requiredStatistics.runTime
+ pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
).toBe(false)
await pool.destroy()
pool = new DynamicThreadPool(
)
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .requiredStatistics.runTime
+ pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
).toBe(false)
// We need to clean up the resources after our test
await pool.destroy()
await pool.destroy()
})
+ 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.chooseWorker()[1].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.chooseWorker()[1].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.getWorkerChoiceStrategy().nextWorkerId
+ ).toBeUndefined()
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerIndex
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerId
).toBe(0)
await pool.destroy()
pool = new DynamicThreadPool(
'./tests/worker-files/thread/testWorker.js',
{ workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .workerChoiceStrategy.nextWorkerId
+ ).toBeUndefined()
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .workerChoiceStrategy.nextWorkerIndex
+ .workerChoiceStrategy.nextWorkerId
).toBe(0)
// We need to clean up the resources after our test
await pool.destroy()
})
- it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
+ 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_RECENTLY_USED }
+ { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
)
expect(pool.opts.workerChoiceStrategy).toBe(
- WorkerChoiceStrategies.LESS_RECENTLY_USED
+ WorkerChoiceStrategies.LESS_USED
)
// We need to clean up the resources after our test
await pool.destroy()
})
- it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
+ 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_RECENTLY_USED)
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
expect(pool.opts.workerChoiceStrategy).toBe(
- WorkerChoiceStrategies.LESS_RECENTLY_USED
+ WorkerChoiceStrategies.LESS_USED
)
// We need to clean up the resources after our test
await pool.destroy()
})
- it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
+ 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_RECENTLY_USED)
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .requiredStatistics.runTime
+ pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
).toBe(false)
await pool.destroy()
pool = new DynamicThreadPool(
max,
'./tests/worker-files/thread/testWorker.js'
)
- pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .requiredStatistics.runTime
+ pool.workerChoiceStrategyContext.getRequiredStatistics().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 () => {
+ 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 () => {
+ const pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
+ )
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.LESS_BUSY
+ )
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify LESS_BUSY strategy can be set after pool creation', async () => {
+ const pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY)
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.LESS_BUSY
+ )
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify LESS_BUSY strategy default tasks usage statistics requirements', async () => {
+ let pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY)
+ expect(
+ pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
+ ).toBe(true)
+ await pool.destroy()
+ pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY)
+ expect(
+ pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
+ ).toBe(true)
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify LESS_BUSY strategy can be run in a fixed pool', async () => {
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js',
- { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
+ { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
)
- // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
+ // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
promises.push(pool.execute())
await pool.destroy()
})
- it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
+ it('Verify LESS_BUSY strategy can be run in a dynamic pool', async () => {
const pool = new DynamicThreadPool(
min,
max,
'./tests/worker-files/thread/testWorker.js',
- { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
+ { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
)
- // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
+ // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
promises.push(pool.execute())
expect(pool.opts.workerChoiceStrategy).toBe(
WorkerChoiceStrategies.FAIR_SHARE
)
- for (const worker of pool.workerChoiceStrategyContext
+ for (const workerKey of pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
.workerLastVirtualTaskTimestamp.keys()) {
expect(
pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
- .workerLastVirtualTaskTimestamp.get(worker).start
+ .workerLastVirtualTaskTimestamp.get(workerKey).start
).toBe(0)
expect(
pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
- .workerLastVirtualTaskTimestamp.get(worker).end
+ .workerLastVirtualTaskTimestamp.get(workerKey).end
).toBe(0)
}
// We need to clean up the resources after our test
)
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .requiredStatistics.runTime
+ pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
).toBe(true)
await pool.destroy()
pool = new DynamicThreadPool(
)
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .requiredStatistics.runTime
+ pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
).toBe(true)
// We need to clean up the resources after our test
await pool.destroy()
promises.push(pool.execute())
}
await Promise.all(promises)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .workerLastVirtualTaskTimestamp.size
+ ).toBe(pool.workers.length)
// We need to clean up the resources after our test
await pool.destroy()
})
promises.push(pool.execute())
}
await Promise.all(promises)
+ // expect(
+ // pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ // .workerChoiceStrategy.workerLastVirtualTaskTimestamp.size
+ // ).toBe(pool.workers.length)
// We need to clean up the resources after our test
await pool.destroy()
})
.workerLastVirtualTaskTimestamp
).toBeUndefined()
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
- for (const worker of pool.workerChoiceStrategyContext
+ for (const workerKey of pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
.workerLastVirtualTaskTimestamp.keys()) {
expect(
pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
- .workerLastVirtualTaskTimestamp.get(worker).start
+ .workerLastVirtualTaskTimestamp.get(workerKey).start
).toBe(0)
expect(
pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
- .workerLastVirtualTaskTimestamp.get(worker).end
+ .workerLastVirtualTaskTimestamp.get(workerKey).end
).toBe(0)
}
await pool.destroy()
.workerChoiceStrategy.workerLastVirtualTaskTimestamp
).toBeUndefined()
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
- for (const worker of pool.workerChoiceStrategyContext
+ for (const workerKey of pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
expect(
pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
- .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(worker).start
+ .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(workerKey)
+ .start
).toBe(0)
expect(
pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
- .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(worker).end
+ .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(workerKey)
+ .end
).toBe(0)
}
// We need to clean up the resources after our test
WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
)
expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .previousWorkerIndex
- ).toBe(0)
- expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .currentWorkerIndex
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
).toBe(0)
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.defaultWorkerWeight
).toBeGreaterThan(0)
- for (const worker of pool.workerChoiceStrategyContext
+ for (const workerKey of pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
.workersTaskRunTime.keys()) {
expect(
pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
- .workersTaskRunTime.get(worker).weight
+ .workersTaskRunTime.get(workerKey).weight
).toBeGreaterThan(0)
expect(
pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
- .workersTaskRunTime.get(worker).runTime
+ .workersTaskRunTime.get(workerKey).runTime
).toBe(0)
}
// We need to clean up the resources after our test
)
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .requiredStatistics.runTime
+ pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
).toBe(true)
await pool.destroy()
pool = new DynamicThreadPool(
)
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .requiredStatistics.runTime
+ pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
).toBe(true)
// We need to clean up the resources after our test
await pool.destroy()
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()
})
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()
})
'./tests/worker-files/thread/testWorker.js'
)
expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .workersTaskRunTime
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
).toBeUndefined()
- pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .previousWorkerIndex
- ).toBe(0)
+ .defaultWorkerWeight
+ ).toBeUndefined()
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .currentWorkerIndex
+ .workersTaskRunTime
+ ).toBeUndefined()
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
).toBe(0)
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.defaultWorkerWeight
).toBeGreaterThan(0)
- for (const worker of pool.workerChoiceStrategyContext
+ for (const workerKey of pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
.workersTaskRunTime.keys()) {
expect(
pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
- .workersTaskRunTime.get(worker).runTime
+ .workersTaskRunTime.get(workerKey).runTime
).toBe(0)
}
await pool.destroy()
)
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .workerChoiceStrategy.workersTaskRunTime
+ .workerChoiceStrategy.currentWorkerId
).toBeUndefined()
- pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .workerChoiceStrategy.previousWorkerIndex
- ).toBe(0)
+ .workerChoiceStrategy.defaultWorkerWeight
+ ).toBeUndefined()
+ expect(
+ pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .workerChoiceStrategy.workersTaskRunTime
+ ).toBeUndefined()
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .workerChoiceStrategy.currentWorkerIndex
+ .workerChoiceStrategy.currentWorkerId
).toBe(0)
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.workerChoiceStrategy.defaultWorkerWeight
).toBeGreaterThan(0)
- for (const worker of pool.workerChoiceStrategyContext
+ for (const workerKey of pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
.workerChoiceStrategy.workersTaskRunTime.keys()) {
expect(
pool.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
- .workerChoiceStrategy.workersTaskRunTime.get(worker).runTime
+ .workerChoiceStrategy.workersTaskRunTime.get(workerKey).runTime
).toBe(0)
}
// We need to clean up the resources after our test