DynamicThreadPool,
FixedThreadPool,
WorkerChoiceStrategies
-} from '../../../lib/index.js'
-import { WorkerChoiceStrategyContext } from '../../../lib/pools/selection-strategies/worker-choice-strategy-context.js'
-import { RoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy.js'
-import { LeastUsedWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-used-worker-choice-strategy.js'
-import { LeastBusyWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-busy-worker-choice-strategy.js'
-import { LeastEluWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-elu-worker-choice-strategy.js'
-import { FairShareWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy.js'
-import { WeightedRoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.js'
-import { InterleavedWeightedRoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.js'
+} from '../../../lib/index.cjs'
+import { WorkerChoiceStrategyContext } from '../../../lib/pools/selection-strategies/worker-choice-strategy-context.cjs'
+import { RoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy.cjs'
+import { LeastUsedWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-used-worker-choice-strategy.cjs'
+import { LeastBusyWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-busy-worker-choice-strategy.cjs'
+import { LeastEluWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-elu-worker-choice-strategy.cjs'
+import { FairShareWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy.cjs'
+import { WeightedRoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.cjs'
+import { InterleavedWeightedRoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.cjs'
describe('Worker choice strategy context test suite', () => {
const min = 1
before(() => {
fixedPool = new FixedThreadPool(
max,
- './tests/worker-files/thread/testWorker.js'
+ './tests/worker-files/thread/testWorker.mjs'
)
dynamicPool = new DynamicThreadPool(
min,
max,
- './tests/worker-files/thread/testWorker.js'
+ './tests/worker-files/thread/testWorker.mjs'
)
})
})
it('Verify that constructor() initializes the context with all the available worker choice strategies', () => {
- const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
- fixedPool
+ let workerChoiceStrategyContext = new WorkerChoiceStrategyContext(fixedPool)
+ expect(workerChoiceStrategyContext.workerChoiceStrategies.size).toBe(
+ Object.keys(WorkerChoiceStrategies).length
)
+ workerChoiceStrategyContext = new WorkerChoiceStrategyContext(dynamicPool)
expect(workerChoiceStrategyContext.workerChoiceStrategies.size).toBe(
Object.keys(WorkerChoiceStrategies).length
)
})
- it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
+ it('Verify that constructor() initializes the context with retries attribute properly set', () => {
+ let workerChoiceStrategyContext = new WorkerChoiceStrategyContext(fixedPool)
+ expect(workerChoiceStrategyContext.retries).toBe(fixedPool.info.maxSize * 2)
+ workerChoiceStrategyContext = new WorkerChoiceStrategyContext(dynamicPool)
+ expect(workerChoiceStrategyContext.retries).toBe(
+ dynamicPool.info.maxSize * 2
+ )
+ })
+
+ it('Verify that execute() throws error if null or undefined is returned after retries', () => {
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
fixedPool
)
- const WorkerChoiceStrategyStub = createStubInstance(
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
+ const workerChoiceStrategyUndefinedStub = createStubInstance(
RoundRobinWorkerChoiceStrategy,
{
- choose: stub().returns(0)
+ choose: stub().returns(undefined)
+ }
+ )
+ workerChoiceStrategyContext.workerChoiceStrategies.set(
+ workerChoiceStrategyContext.workerChoiceStrategy,
+ workerChoiceStrategyUndefinedStub
+ )
+ expect(() => workerChoiceStrategyContext.execute()).toThrow(
+ new Error(
+ `Worker node key chosen is null or undefined after ${workerChoiceStrategyContext.retries} retries`
+ )
+ )
+ const workerChoiceStrategyNullStub = createStubInstance(
+ RoundRobinWorkerChoiceStrategy,
+ {
+ choose: stub().returns(null)
+ }
+ )
+ workerChoiceStrategyContext.workerChoiceStrategies.set(
+ workerChoiceStrategyContext.workerChoiceStrategy,
+ workerChoiceStrategyNullStub
+ )
+ expect(() => workerChoiceStrategyContext.execute()).toThrow(
+ new Error(
+ `Worker node key chosen is null or undefined after ${workerChoiceStrategyContext.retries} retries`
+ )
+ )
+ })
+
+ it('Verify that execute() retry until a worker node is chosen', () => {
+ const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+ fixedPool
+ )
+ const workerChoiceStrategyStub = createStubInstance(
+ RoundRobinWorkerChoiceStrategy,
+ {
+ choose: stub()
+ .onCall(0)
+ .returns(undefined)
+ .onCall(1)
+ .returns(undefined)
+ .onCall(2)
+ .returns(undefined)
+ .onCall(3)
+ .returns(undefined)
+ .onCall(4)
+ .returns(undefined)
+ .returns(1)
}
)
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
)
workerChoiceStrategyContext.workerChoiceStrategies.set(
workerChoiceStrategyContext.workerChoiceStrategy,
- WorkerChoiceStrategyStub
+ workerChoiceStrategyStub
)
const chosenWorkerKey = workerChoiceStrategyContext.execute()
expect(
workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategyContext.workerChoiceStrategy
- ).choose.calledOnce
- ).toBe(true)
- expect(chosenWorkerKey).toBe(0)
+ ).choose.callCount
+ ).toBe(6)
+ expect(chosenWorkerKey).toBe(1)
})
- it('Verify that execute() throws error if null or undefined is returned after retries', () => {
+ it('Verify that execute() return the worker node key chosen by the strategy with fixed pool', () => {
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
fixedPool
)
- const WorkerChoiceStrategyUndefinedStub = createStubInstance(
- RoundRobinWorkerChoiceStrategy,
- {
- choose: stub().returns(undefined)
- }
- )
- const WorkerChoiceStrategyNullStub = createStubInstance(
+ const workerChoiceStrategyStub = createStubInstance(
RoundRobinWorkerChoiceStrategy,
{
- choose: stub().returns(null)
+ choose: stub().returns(0)
}
)
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
)
workerChoiceStrategyContext.workerChoiceStrategies.set(
workerChoiceStrategyContext.workerChoiceStrategy,
- WorkerChoiceStrategyUndefinedStub
- )
- expect(() => workerChoiceStrategyContext.execute()).toThrowError(
- new Error('Worker node key chosen is null or undefined after 6 retries')
- )
- workerChoiceStrategyContext.workerChoiceStrategies.set(
- workerChoiceStrategyContext.workerChoiceStrategy,
- WorkerChoiceStrategyNullStub
- )
- expect(() => workerChoiceStrategyContext.execute()).toThrowError(
- new Error('Worker node key chosen is null or undefined after 6 retries')
+ workerChoiceStrategyStub
)
+ const chosenWorkerKey = workerChoiceStrategyContext.execute()
+ expect(
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategyContext.workerChoiceStrategy
+ ).choose.calledOnce
+ ).toBe(true)
+ expect(chosenWorkerKey).toBe(0)
})
- it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
+ it('Verify that execute() return the worker node key chosen by the strategy with dynamic pool', () => {
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
dynamicPool
)
- const WorkerChoiceStrategyStub = createStubInstance(
+ const workerChoiceStrategyStub = createStubInstance(
RoundRobinWorkerChoiceStrategy,
{
choose: stub().returns(0)
)
workerChoiceStrategyContext.workerChoiceStrategies.set(
workerChoiceStrategyContext.workerChoiceStrategy,
- WorkerChoiceStrategyStub
+ workerChoiceStrategyStub
)
const chosenWorkerKey = workerChoiceStrategyContext.execute()
expect(