*/
export class WorkerChoiceStrategyContext<
Worker extends IPoolWorker,
- Data,
- Response
+ Data = unknown,
+ Response = unknown
> {
private workerChoiceStrategy: IWorkerChoiceStrategy<Worker, Data, Response>
public constructor (
pool: IPoolInternal<Worker, Data, Response>,
private readonly createWorkerCallback: () => number,
- workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
+ private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
) {
this.execute.bind(this)
this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>(
pool,
- workerChoiceStrategy
+ workerChoiceStrategyType
)
}
pool: IPoolInternal<Worker, Data, Response>,
workerChoiceStrategy: WorkerChoiceStrategy
): void {
- this.workerChoiceStrategy?.reset()
- this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>(
- pool,
- workerChoiceStrategy
- )
+ if (this.workerChoiceStrategyType === workerChoiceStrategy) {
+ this.workerChoiceStrategy?.reset()
+ } else {
+ this.workerChoiceStrategyType = workerChoiceStrategy
+ this.workerChoiceStrategy = getWorkerChoiceStrategy<
+ Worker,
+ Data,
+ Response
+ >(pool, this.workerChoiceStrategyType)
+ }
}
/**
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
fixedPool
)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
+ RoundRobinWorkerChoiceStrategy
+ )
+ expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
workerChoiceStrategyContext.setWorkerChoiceStrategy(
fixedPool,
WorkerChoiceStrategies.ROUND_ROBIN
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
RoundRobinWorkerChoiceStrategy
)
+ expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
})
it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
dynamicPool
)
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
+ RoundRobinWorkerChoiceStrategy
+ )
+ expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
workerChoiceStrategyContext.setWorkerChoiceStrategy(
dynamicPool,
WorkerChoiceStrategies.ROUND_ROBIN
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
RoundRobinWorkerChoiceStrategy
)
+ expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
})
it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
LessUsedWorkerChoiceStrategy
)
+ expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+ WorkerChoiceStrategies.LESS_USED
+ )
})
it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
LessUsedWorkerChoiceStrategy
)
+ expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+ WorkerChoiceStrategies.LESS_USED
+ )
})
it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => {
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
LessBusyWorkerChoiceStrategy
)
+ expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+ WorkerChoiceStrategies.LESS_BUSY
+ )
})
it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => {
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
LessBusyWorkerChoiceStrategy
)
+ expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+ WorkerChoiceStrategies.LESS_BUSY
+ )
})
it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
FairShareWorkerChoiceStrategy
)
+ expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+ WorkerChoiceStrategies.FAIR_SHARE
+ )
})
it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
FairShareWorkerChoiceStrategy
)
+ expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+ WorkerChoiceStrategies.FAIR_SHARE
+ )
})
it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
WeightedRoundRobinWorkerChoiceStrategy
)
+ expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+ WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+ )
})
it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
WeightedRoundRobinWorkerChoiceStrategy
)
+ expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+ WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+ )
})
})