FixedThreadPool,
DynamicThreadPool,
WorkerChoiceStrategies
-} = require('../../../lib/index')
+} = require('../../../lib')
const {
WorkerChoiceStrategyContext
} = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context')
RoundRobinWorkerChoiceStrategy
} = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy')
const {
- LessRecentlyUsedWorkerChoiceStrategy
-} = require('../../../lib/pools/selection-strategies/less-recently-used-worker-choice-strategy')
+ LeastUsedWorkerChoiceStrategy
+} = require('../../../lib/pools/selection-strategies/least-used-worker-choice-strategy')
+const {
+ LeastBusyWorkerChoiceStrategy
+} = require('../../../lib/pools/selection-strategies/least-busy-worker-choice-strategy')
const {
FairShareWorkerChoiceStrategy
} = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy')
WeightedRoundRobinWorkerChoiceStrategy
} = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy')
const {
- DynamicPoolWorkerChoiceStrategy
-} = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy')
+ InterleavedWeightedRoundRobinWorkerChoiceStrategy
+} = require('../../../lib/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy')
describe('Worker choice strategy context test suite', () => {
const min = 1
await dynamicPool.destroy()
})
+ it('Verify that constructor() initializes the context with all the available worker choice strategies', () => {
+ const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+ fixedPool
+ )
+ expect(workerChoiceStrategyContext.workerChoiceStrategies.size).toBe(
+ Object.keys(WorkerChoiceStrategies).length
+ )
+ })
+
it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
fixedPool
const WorkerChoiceStrategyStub = sinon.createStubInstance(
RoundRobinWorkerChoiceStrategy,
{
- choose: sinon.stub().returns('worker')
+ choose: sinon.stub().returns(0)
}
)
- workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
- const worker = workerChoiceStrategyContext.execute()
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
+ workerChoiceStrategyContext.workerChoiceStrategies.set(
+ workerChoiceStrategyContext.workerChoiceStrategy,
+ WorkerChoiceStrategyStub
+ )
+ const chosenWorkerKey = workerChoiceStrategyContext.execute()
expect(
- workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategyContext.workerChoiceStrategy
+ ).choose.calledOnce
).toBe(true)
- expect(worker).toBe('worker')
+ expect(chosenWorkerKey).toBe(0)
+ })
+
+ it('Verify that execute() throws error if null or undefined is returned', () => {
+ const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+ fixedPool
+ )
+ const WorkerChoiceStrategyUndefinedStub = sinon.createStubInstance(
+ RoundRobinWorkerChoiceStrategy,
+ {
+ choose: sinon.stub().returns(undefined)
+ }
+ )
+ const WorkerChoiceStrategyNullStub = sinon.createStubInstance(
+ RoundRobinWorkerChoiceStrategy,
+ {
+ choose: sinon.stub().returns(null)
+ }
+ )
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
+ workerChoiceStrategyContext.workerChoiceStrategies.set(
+ workerChoiceStrategyContext.workerChoiceStrategy,
+ WorkerChoiceStrategyUndefinedStub
+ )
+ expect(() => workerChoiceStrategyContext.execute()).toThrowError(
+ new Error('Worker node key chosen is null or undefined')
+ )
+ workerChoiceStrategyContext.workerChoiceStrategies.set(
+ workerChoiceStrategyContext.workerChoiceStrategy,
+ WorkerChoiceStrategyNullStub
+ )
+ expect(() => workerChoiceStrategyContext.execute()).toThrowError(
+ new Error('Worker node key chosen is null or undefined')
+ )
})
it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
const WorkerChoiceStrategyStub = sinon.createStubInstance(
RoundRobinWorkerChoiceStrategy,
{
- choose: sinon.stub().returns('worker')
+ choose: sinon.stub().returns(0)
}
)
- workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
- const worker = workerChoiceStrategyContext.execute()
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
+ workerChoiceStrategyContext.workerChoiceStrategies.set(
+ workerChoiceStrategyContext.workerChoiceStrategy,
+ WorkerChoiceStrategyStub
+ )
+ const chosenWorkerKey = workerChoiceStrategyContext.execute()
expect(
- workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategyContext.workerChoiceStrategy
+ ).choose.calledOnce
).toBe(true)
- expect(worker).toBe('worker')
+ expect(chosenWorkerKey).toBe(0)
})
it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
+ const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
fixedPool
)
- workerChoiceStrategyContext.setWorkerChoiceStrategy(
- WorkerChoiceStrategies.ROUND_ROBIN
+ expect(
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
+ ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
)
- expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
- RoundRobinWorkerChoiceStrategy
+ workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
+ expect(
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
+ ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
)
})
it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
+ const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
dynamicPool
)
- workerChoiceStrategyContext.setWorkerChoiceStrategy(
- WorkerChoiceStrategies.ROUND_ROBIN
- )
- expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
- DynamicPoolWorkerChoiceStrategy
+ expect(
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
+ ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
)
+ workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
expect(
- workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
+ )
})
- it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => {
+ it('Verify that setWorkerChoiceStrategy() works with LEAST_USED and fixed pool', () => {
+ const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
fixedPool
)
- workerChoiceStrategyContext.setWorkerChoiceStrategy(
- WorkerChoiceStrategies.LESS_RECENTLY_USED
- )
- expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
- LessRecentlyUsedWorkerChoiceStrategy
+ workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
+ expect(
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
+ ).toBeInstanceOf(LeastUsedWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
)
})
- it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and dynamic pool', () => {
+ it('Verify that setWorkerChoiceStrategy() works with LEAST_USED and dynamic pool', () => {
+ const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
dynamicPool
)
- workerChoiceStrategyContext.setWorkerChoiceStrategy(
- WorkerChoiceStrategies.LESS_RECENTLY_USED
+ workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
+ expect(
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
+ ).toBeInstanceOf(LeastUsedWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
)
- expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
- DynamicPoolWorkerChoiceStrategy
+ })
+
+ it('Verify that setWorkerChoiceStrategy() works with LEAST_BUSY and fixed pool', () => {
+ const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY
+ const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+ fixedPool
)
+ workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
expect(
- workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
- ).toBeInstanceOf(LessRecentlyUsedWorkerChoiceStrategy)
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
+ ).toBeInstanceOf(LeastBusyWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
+ )
+ })
+
+ it('Verify that setWorkerChoiceStrategy() works with LEAST_BUSY and dynamic pool', () => {
+ const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY
+ const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+ dynamicPool
+ )
+ workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
+ expect(
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
+ ).toBeInstanceOf(LeastBusyWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
+ )
})
it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
+ const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
fixedPool
)
- workerChoiceStrategyContext.setWorkerChoiceStrategy(
- WorkerChoiceStrategies.FAIR_SHARE
- )
- expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
- FairShareWorkerChoiceStrategy
+ workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
+ expect(
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
+ ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
)
})
it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
+ const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
dynamicPool
)
- workerChoiceStrategyContext.setWorkerChoiceStrategy(
- WorkerChoiceStrategies.FAIR_SHARE
- )
- expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
- DynamicPoolWorkerChoiceStrategy
- )
+ workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
expect(
- workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
).toBeInstanceOf(FairShareWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
+ )
})
it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
+ const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
fixedPool
)
- workerChoiceStrategyContext.setWorkerChoiceStrategy(
- WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
- )
- expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
- WeightedRoundRobinWorkerChoiceStrategy
+ workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
+ expect(
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
+ ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
)
})
it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
+ const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
dynamicPool
)
- workerChoiceStrategyContext.setWorkerChoiceStrategy(
- WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+ workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
+ expect(
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
+ ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
)
- expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
- DynamicPoolWorkerChoiceStrategy
+ })
+
+ it('Verify that setWorkerChoiceStrategy() works with INTERLEAVED_WEIGHTED_ROUND_ROBIN and fixed pool', () => {
+ const workerChoiceStrategy =
+ WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
+ const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+ fixedPool
)
+ workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
expect(
- workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
- ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
+ ).toBeInstanceOf(InterleavedWeightedRoundRobinWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
+ )
+ })
+
+ it('Verify that setWorkerChoiceStrategy() works with INTERLEAVED_WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
+ const workerChoiceStrategy =
+ WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
+ const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+ dynamicPool
+ )
+ workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
+ expect(
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategy
+ )
+ ).toBeInstanceOf(InterleavedWeightedRoundRobinWorkerChoiceStrategy)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ workerChoiceStrategy
+ )
+ })
+
+ it('Verify that worker choice strategy options enable median runtime pool statistics', () => {
+ const wwrWorkerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+ let workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+ fixedPool,
+ wwrWorkerChoiceStrategy,
+ {
+ medRunTime: true
+ }
+ )
+ expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe(
+ false
+ )
+ expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe(
+ true
+ )
+ workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+ dynamicPool,
+ wwrWorkerChoiceStrategy,
+ {
+ medRunTime: true
+ }
+ )
+ expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe(
+ false
+ )
+ expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe(
+ true
+ )
+ const fsWorkerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
+ workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+ fixedPool,
+ fsWorkerChoiceStrategy,
+ {
+ medRunTime: true
+ }
+ )
+ expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe(
+ false
+ )
+ expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe(
+ true
+ )
+ workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+ dynamicPool,
+ fsWorkerChoiceStrategy,
+ {
+ medRunTime: true
+ }
+ )
+ expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe(
+ false
+ )
+ expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe(
+ true
+ )
})
})