Data = unknown,
Response = unknown
> {
- /**
- * The number of worker choice strategies execution retries.
- */
- public retriesCount: number
-
/**
* The default worker choice strategy in the context.
*/
buildWorkerChoiceStrategiesTaskStatisticsRequirements(
this.workerChoiceStrategies
)
- this.retriesCount = 0
this.retries = getWorkerChoiceStrategiesRetries<Worker, Data, Response>(
this.pool,
opts
return this.workerChoiceStrategiesPolicy
}
+ /**
+ * Gets the number of worker choice strategies execution retries.
+ * @returns The number of retries.
+ */
+ public getStrategyRetries (): number {
+ return Array.from(
+ this.workerChoiceStrategies,
+ ([_, workerChoiceStrategy]) => workerChoiceStrategy.retriesCount
+ ).reduce((accumulator, retries) => accumulator + retries, 0)
+ }
+
/**
* Gets the active worker choice strategies in the context task statistics requirements.
* @returns The strategies task statistics requirements.
let workerNodeKey: number | undefined = workerChoiceStrategy.choose()
let retriesCount = 0
while (workerNodeKey == null && retriesCount < this.retries) {
- workerNodeKey = workerChoiceStrategy.choose()
retriesCount++
- this.retriesCount++
+ workerNodeKey = workerChoiceStrategy.choose()
}
+ workerChoiceStrategy.retriesCount = retriesCount
if (workerNodeKey == null) {
throw new Error(
- `Worker node key chosen by ${workerChoiceStrategy.name} is null or undefined after ${retriesCount.toString()} retries (max: ${this.retries.toString()})`
+ `Worker node key chosen by ${workerChoiceStrategy.name} is null or undefined after ${workerChoiceStrategy.retriesCount.toString()} retries (max: ${this.retries.toString()})`
)
}
return workerNodeKey
import { expect } from '@std/expect'
-import { createStubInstance, restore, stub } from 'sinon'
+import { restore, stub } from 'sinon'
import {
DynamicThreadPool,
expect(workerChoiceStrategiesContext.defaultWorkerChoiceStrategy).toBe(
WorkerChoiceStrategies.ROUND_ROBIN
)
- const workerChoiceStrategyUndefinedStub = createStubInstance(
- RoundRobinWorkerChoiceStrategy,
- {
- choose: stub().returns(undefined),
- }
- )
- workerChoiceStrategiesContext.workerChoiceStrategies.set(
- workerChoiceStrategiesContext.defaultWorkerChoiceStrategy,
- workerChoiceStrategyUndefinedStub
- )
- expect(() => workerChoiceStrategiesContext.execute()).toThrow(
- new Error(
- `Worker node key chosen by ${workerChoiceStrategyUndefinedStub.name} is null or undefined after ${workerChoiceStrategiesContext.retries.toString()} retries (max: ${workerChoiceStrategiesContext.retries.toString()})`
+ const workerChoiceStrategyUndefinedStub =
+ workerChoiceStrategiesContext.workerChoiceStrategies.get(
+ workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
)
- )
- const workerChoiceStrategyNullStub = createStubInstance(
- RoundRobinWorkerChoiceStrategy,
- {
- choose: stub().returns(null),
- }
- )
- workerChoiceStrategiesContext.workerChoiceStrategies.set(
- workerChoiceStrategiesContext.defaultWorkerChoiceStrategy,
- workerChoiceStrategyNullStub
- )
- expect(() => workerChoiceStrategiesContext.execute()).toThrow(
- new Error(
- `Worker node key chosen by ${workerChoiceStrategyNullStub.name} is null or undefined after ${workerChoiceStrategiesContext.retries.toString()} retries (max: ${workerChoiceStrategiesContext.retries.toString()})`
+ const chooseUndefinedStub = stub(
+ workerChoiceStrategyUndefinedStub,
+ 'choose'
+ ).returns(undefined)
+ let err
+ try {
+ workerChoiceStrategiesContext.execute()
+ } catch (e) {
+ err = e
+ }
+ expect(err).toBeInstanceOf(Error)
+ expect(err.message).toBe(
+ `Worker node key chosen by ${workerChoiceStrategyUndefinedStub.name} is null or undefined after ${workerChoiceStrategyUndefinedStub.retriesCount.toString()} retries (max: ${workerChoiceStrategiesContext.retries.toString()})`
+ )
+ expect(chooseUndefinedStub.callCount).toBe(
+ workerChoiceStrategyUndefinedStub.retriesCount + 1
+ )
+ expect(workerChoiceStrategiesContext.getStrategyRetries()).toBe(
+ workerChoiceStrategyUndefinedStub.retriesCount
+ )
+ chooseUndefinedStub.restore()
+ const workerChoiceStrategyNullStub =
+ workerChoiceStrategiesContext.workerChoiceStrategies.get(
+ workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
)
- )
+ const chooseNullStub = stub(workerChoiceStrategyNullStub, 'choose').returns(
+ null
+ )
+ err = undefined
+ try {
+ workerChoiceStrategiesContext.execute()
+ } catch (e) {
+ err = e
+ }
+ expect(err).toBeInstanceOf(Error)
+ expect(err.message).toBe(
+ `Worker node key chosen by ${workerChoiceStrategyNullStub.name} is null or undefined after ${workerChoiceStrategyNullStub.retriesCount.toString()} retries (max: ${workerChoiceStrategiesContext.retries.toString()})`
+ )
+ expect(chooseNullStub.callCount).toBe(
+ workerChoiceStrategyNullStub.retriesCount + 1
+ )
+ expect(workerChoiceStrategiesContext.getStrategyRetries()).toBe(
+ workerChoiceStrategyNullStub.retriesCount
+ )
+ chooseNullStub.restore()
})
it('Verify that execute() retry until a worker node is chosen', () => {
const workerChoiceStrategiesContext = new WorkerChoiceStrategiesContext(
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(workerChoiceStrategiesContext.defaultWorkerChoiceStrategy).toBe(
WorkerChoiceStrategies.ROUND_ROBIN
)
- workerChoiceStrategiesContext.workerChoiceStrategies.set(
- workerChoiceStrategiesContext.defaultWorkerChoiceStrategy,
- workerChoiceStrategyStub
- )
- const chosenWorkerKey = workerChoiceStrategiesContext.execute()
- expect(
+ const workerChoiceStrategyStub =
workerChoiceStrategiesContext.workerChoiceStrategies.get(
workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
- ).choose.callCount
- ).toBe(6)
+ )
+ stub(workerChoiceStrategyStub, 'choose')
+ .onCall(0)
+ .returns(undefined)
+ .onCall(1)
+ .returns(undefined)
+ .onCall(2)
+ .returns(undefined)
+ .onCall(3)
+ .returns(undefined)
+ .onCall(4)
+ .returns(undefined)
+ .returns(1)
+ const chosenWorkerKey = workerChoiceStrategiesContext.execute()
+ expect(workerChoiceStrategyStub.choose.callCount).toBe(6)
+ expect(workerChoiceStrategiesContext.getStrategyRetries()).toBe(5)
expect(chosenWorkerKey).toBe(1)
})
const workerChoiceStrategiesContext = new WorkerChoiceStrategiesContext(
fixedPool
)
- const workerChoiceStrategyStub = createStubInstance(
- RoundRobinWorkerChoiceStrategy,
- {
- choose: stub().returns(0),
- }
- )
expect(workerChoiceStrategiesContext.defaultWorkerChoiceStrategy).toBe(
WorkerChoiceStrategies.ROUND_ROBIN
)
- workerChoiceStrategiesContext.workerChoiceStrategies.set(
- workerChoiceStrategiesContext.defaultWorkerChoiceStrategy,
- workerChoiceStrategyStub
- )
- const chosenWorkerKey = workerChoiceStrategiesContext.execute()
- expect(
+ const workerChoiceStrategyStub =
workerChoiceStrategiesContext.workerChoiceStrategies.get(
workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
- ).choose.calledOnce
- ).toBe(true)
+ )
+ stub(workerChoiceStrategyStub, 'choose').returns(0)
+ const chosenWorkerKey = workerChoiceStrategiesContext.execute()
+ expect(workerChoiceStrategyStub.choose.calledOnce).toBe(true)
+ expect(workerChoiceStrategiesContext.getStrategyRetries()).toBe(0)
expect(chosenWorkerKey).toBe(0)
})
const workerChoiceStrategiesContext = new WorkerChoiceStrategiesContext(
dynamicPool
)
- const workerChoiceStrategyStub = createStubInstance(
- RoundRobinWorkerChoiceStrategy,
- {
- choose: stub().returns(0),
- }
- )
expect(workerChoiceStrategiesContext.defaultWorkerChoiceStrategy).toBe(
WorkerChoiceStrategies.ROUND_ROBIN
)
- workerChoiceStrategiesContext.workerChoiceStrategies.set(
- workerChoiceStrategiesContext.defaultWorkerChoiceStrategy,
- workerChoiceStrategyStub
- )
- const chosenWorkerKey = workerChoiceStrategiesContext.execute()
- expect(
+ const workerChoiceStrategyStub =
workerChoiceStrategiesContext.workerChoiceStrategies.get(
workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
- ).choose.calledOnce
- ).toBe(true)
+ )
+ stub(workerChoiceStrategyStub, 'choose').returns(0)
+ const chosenWorkerKey = workerChoiceStrategiesContext.execute()
+ expect(workerChoiceStrategyStub.choose.calledOnce).toBe(true)
+ expect(workerChoiceStrategiesContext.getStrategyRetries()).toBe(0)
expect(chosenWorkerKey).toBe(0)
})