} from './pools/selection-strategies/selection-strategies-types.js'
export type {
IWorkerChoiceStrategy,
- InternalWorkerChoiceStrategyOptions,
Measurement,
MeasurementOptions,
MeasurementStatisticsRequirements,
this.opts.workerChoiceStrategyOptions = workerChoiceStrategyOptions
}
this.workerChoiceStrategyContext.setOptions(
- this,
this.opts.workerChoiceStrategyOptions
)
}
import {
DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
- buildInternalWorkerChoiceStrategyOptions
+ buildWorkerChoiceStrategyOptions
} from '../../utils.js'
import type { IPool } from '../pool.js'
import type { IWorker } from '../worker.js'
import type {
IWorkerChoiceStrategy,
- InternalWorkerChoiceStrategyOptions,
MeasurementStatisticsRequirements,
StrategyPolicy,
- TaskStatisticsRequirements
+ TaskStatisticsRequirements,
+ WorkerChoiceStrategyOptions
} from './selection-strategies-types.js'
/**
*/
public constructor (
protected readonly pool: IPool<Worker, Data, Response>,
- protected opts: InternalWorkerChoiceStrategyOptions
+ protected opts?: WorkerChoiceStrategyOptions
) {
- this.opts = buildInternalWorkerChoiceStrategyOptions(
- this.pool.info.maxSize,
+ this.opts = buildWorkerChoiceStrategyOptions<Worker, Data, Response>(
+ this.pool,
this.opts
)
this.setTaskStatisticsRequirements(this.opts)
}
protected setTaskStatisticsRequirements (
- opts: InternalWorkerChoiceStrategyOptions
+ opts: WorkerChoiceStrategyOptions | undefined
): void {
this.toggleMedianMeasurementStatisticsRequirements(
this.taskStatisticsRequirements.runTime,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- opts.runTime!.median
+ opts!.runTime!.median
)
this.toggleMedianMeasurementStatisticsRequirements(
this.taskStatisticsRequirements.waitTime,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- opts.waitTime!.median
+ opts!.waitTime!.median
)
this.toggleMedianMeasurementStatisticsRequirements(
this.taskStatisticsRequirements.elu,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- opts.elu!.median
+ opts!.elu!.median
)
}
public abstract remove (workerNodeKey: number): boolean
/** @inheritDoc */
- public setOptions (opts: InternalWorkerChoiceStrategyOptions): void {
- this.opts = buildInternalWorkerChoiceStrategyOptions(
- this.pool.info.maxSize,
+ public setOptions (opts: WorkerChoiceStrategyOptions | undefined): void {
+ this.opts = buildWorkerChoiceStrategyOptions<Worker, Data, Response>(
+ this.pool,
opts
)
this.setTaskStatisticsRequirements(this.opts)
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
import {
type IWorkerChoiceStrategy,
- type InternalWorkerChoiceStrategyOptions,
Measurements,
- type TaskStatisticsRequirements
+ type TaskStatisticsRequirements,
+ type WorkerChoiceStrategyOptions
} from './selection-strategies-types.js'
/**
/** @inheritDoc */
public constructor (
pool: IPool<Worker, Data, Response>,
- opts: InternalWorkerChoiceStrategyOptions
+ opts?: WorkerChoiceStrategyOptions
) {
super(pool, opts)
this.setTaskStatisticsRequirements(this.opts)
workerNodeVirtualTaskStartTimestamp: number
): number {
const workerNodeTaskRunTime =
- this.opts.measurement === Measurements.elu
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.opts!.measurement === Measurements.elu
? this.getWorkerNodeTaskElu(workerNodeKey)
: this.getWorkerNodeTaskRunTime(workerNodeKey)
return workerNodeVirtualTaskStartTimestamp + workerNodeTaskRunTime
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
import type {
IWorkerChoiceStrategy,
- InternalWorkerChoiceStrategyOptions,
- TaskStatisticsRequirements
+ TaskStatisticsRequirements,
+ WorkerChoiceStrategyOptions
} from './selection-strategies-types.js'
/**
/** @inheritDoc */
public constructor (
pool: IPool<Worker, Data, Response>,
- opts: InternalWorkerChoiceStrategyOptions
+ opts?: WorkerChoiceStrategyOptions
) {
super(pool, opts)
this.setTaskStatisticsRequirements(this.opts)
this.workerNodeVirtualTaskRunTime = 0
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- const workerWeight = this.opts.weights![workerNodeKey]!
+ const workerWeight = this.opts!.weights![workerNodeKey]!
if (
this.isWorkerNodeReady(workerNodeKey) &&
workerWeight >= this.roundWeights[roundIndex] &&
}
/** @inheritDoc */
- public setOptions (opts: InternalWorkerChoiceStrategyOptions): void {
+ public setOptions (opts: WorkerChoiceStrategyOptions | undefined): void {
super.setOptions(opts)
this.roundWeights = this.getRoundWeights()
}
return [
...new Set(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- Object.values(this.opts.weights!)
+ Object.values(this.opts!.weights!)
.slice()
.sort((a, b) => a - b)
)
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
import type {
IWorkerChoiceStrategy,
- InternalWorkerChoiceStrategyOptions,
- TaskStatisticsRequirements
+ TaskStatisticsRequirements,
+ WorkerChoiceStrategyOptions
} from './selection-strategies-types.js'
/**
/** @inheritDoc */
public constructor (
pool: IPool<Worker, Data, Response>,
- opts: InternalWorkerChoiceStrategyOptions
+ opts?: WorkerChoiceStrategyOptions
) {
super(pool, opts)
this.setTaskStatisticsRequirements(this.opts)
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
import type {
IWorkerChoiceStrategy,
- InternalWorkerChoiceStrategyOptions,
- TaskStatisticsRequirements
+ TaskStatisticsRequirements,
+ WorkerChoiceStrategyOptions
} from './selection-strategies-types.js'
/**
/** @inheritDoc */
public constructor (
pool: IPool<Worker, Data, Response>,
- opts: InternalWorkerChoiceStrategyOptions
+ opts?: WorkerChoiceStrategyOptions
) {
super(pool, opts)
this.setTaskStatisticsRequirements(this.opts)
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
import type {
IWorkerChoiceStrategy,
- InternalWorkerChoiceStrategyOptions
+ WorkerChoiceStrategyOptions
} from './selection-strategies-types.js'
/**
/** @inheritDoc */
public constructor (
pool: IPool<Worker, Data, Response>,
- opts: InternalWorkerChoiceStrategyOptions
+ opts?: WorkerChoiceStrategyOptions
) {
super(pool, opts)
}
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
import type {
IWorkerChoiceStrategy,
- InternalWorkerChoiceStrategyOptions
+ WorkerChoiceStrategyOptions
} from './selection-strategies-types.js'
/**
/** @inheritDoc */
public constructor (
pool: IPool<Worker, Data, Response>,
- opts: InternalWorkerChoiceStrategyOptions
+ opts?: WorkerChoiceStrategyOptions
) {
super(pool, opts)
}
weights?: Record<number, number>
}
-/**
- * Worker choice strategy internal options.
- *
- * @internal
- */
-export interface InternalWorkerChoiceStrategyOptions
- extends WorkerChoiceStrategyOptions {
- /**
- * Number of worker choice retries to perform if no worker is eligible.
- *
- * @defaultValue pool maximum size
- */
- readonly retries?: number
-}
-
/**
* Measurement statistics requirements.
*
*
* @param opts - The worker choice strategy options.
*/
- readonly setOptions: (opts: WorkerChoiceStrategyOptions) => void
+ readonly setOptions: (opts: WorkerChoiceStrategyOptions | undefined) => void
/**
* Whether the pool has worker nodes ready or not.
*
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
import type {
IWorkerChoiceStrategy,
- InternalWorkerChoiceStrategyOptions,
- TaskStatisticsRequirements
+ TaskStatisticsRequirements,
+ WorkerChoiceStrategyOptions
} from './selection-strategies-types.js'
/**
/** @inheritDoc */
public constructor (
pool: IPool<Worker, Data, Response>,
- opts: InternalWorkerChoiceStrategyOptions
+ opts?: WorkerChoiceStrategyOptions
) {
super(pool, opts)
this.setTaskStatisticsRequirements(this.opts)
private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
const workerWeight =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- this.opts.weights![this.nextWorkerNodeKey ?? this.previousWorkerNodeKey]!
+ this.opts!.weights![this.nextWorkerNodeKey ?? this.previousWorkerNodeKey]!
if (this.workerNodeVirtualTaskRunTime < workerWeight) {
this.workerNodeVirtualTaskRunTime =
this.workerNodeVirtualTaskRunTime +
-import { buildInternalWorkerChoiceStrategyOptions } from '../../utils.js'
import type { IPool } from '../pool.js'
import type { IWorker } from '../worker.js'
+import { getWorkerChoiceStrategyRetries } from '../../utils.js'
import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy.js'
import { InterleavedWeightedRoundRobinWorkerChoiceStrategy } from './interleaved-weighted-round-robin-worker-choice-strategy.js'
import { LeastBusyWorkerChoiceStrategy } from './least-busy-worker-choice-strategy.js'
import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy.js'
import type {
IWorkerChoiceStrategy,
- InternalWorkerChoiceStrategyOptions,
StrategyPolicy,
TaskStatisticsRequirements,
- WorkerChoiceStrategy
+ WorkerChoiceStrategy,
+ WorkerChoiceStrategyOptions
} from './selection-strategies-types.js'
import { WorkerChoiceStrategies } from './selection-strategies-types.js'
import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy.js'
Data = unknown,
Response = unknown
> {
+ /**
+ * The worker choice strategies registered in the context.
+ */
private readonly workerChoiceStrategies: Map<
WorkerChoiceStrategy,
IWorkerChoiceStrategy
>
+ /**
+ * The number of worker choice strategy execution retries.
+ */
+ private readonly retries: number
+
/**
* Worker choice strategy context constructor.
*
public constructor (
pool: IPool<Worker, Data, Response>,
private workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN,
- private opts?: InternalWorkerChoiceStrategyOptions
+ opts?: WorkerChoiceStrategyOptions
) {
- this.opts = buildInternalWorkerChoiceStrategyOptions(
- pool.info.maxSize,
- this.opts
- )
this.execute = this.execute.bind(this)
this.workerChoiceStrategies = new Map<
WorkerChoiceStrategy,
WorkerChoiceStrategies.ROUND_ROBIN,
new (RoundRobinWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
pool,
- this.opts
+ opts
)
],
[
WorkerChoiceStrategies.LEAST_USED,
new (LeastUsedWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
pool,
- this.opts
+ opts
)
],
[
WorkerChoiceStrategies.LEAST_BUSY,
new (LeastBusyWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
pool,
- this.opts
+ opts
)
],
[
WorkerChoiceStrategies.LEAST_ELU,
new (LeastEluWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
pool,
- this.opts
+ opts
)
],
[
WorkerChoiceStrategies.FAIR_SHARE,
new (FairShareWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
pool,
- this.opts
+ opts
)
],
[
Worker,
Data,
Response
- >(pool, this.opts)
+ >(pool, opts)
],
[
WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN,
Worker,
Data,
Response
- >(pool, this.opts)
+ >(pool, opts)
]
])
+ this.retries = getWorkerChoiceStrategyRetries(pool, opts)
}
/**
retriesCount++
}
chooseCount++
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- } while (workerNodeKey == null && retriesCount < this.opts!.retries!)
+ } while (workerNodeKey == null && retriesCount < this.retries)
if (workerNodeKey == null) {
throw new Error(
`Worker node key chosen is null or undefined after ${retriesCount} retries`
/**
* Sets the worker choice strategies in the context options.
*
- * @param pool - The pool instance.
* @param opts - The worker choice strategy options.
*/
- public setOptions (
- pool: IPool<Worker, Data, Response>,
- opts?: InternalWorkerChoiceStrategyOptions
- ): void {
- this.opts = buildInternalWorkerChoiceStrategyOptions(
- pool.info.maxSize,
- opts
- )
+ public setOptions (opts: WorkerChoiceStrategyOptions | undefined): void {
for (const workerChoiceStrategy of this.workerChoiceStrategies.values()) {
- workerChoiceStrategy.setOptions(this.opts)
+ workerChoiceStrategy.setOptions(opts)
}
}
}
import { Worker as ThreadWorker } from 'node:worker_threads'
import { cpus } from 'node:os'
import type {
- InternalWorkerChoiceStrategyOptions,
- MeasurementStatisticsRequirements
+ MeasurementStatisticsRequirements,
+ WorkerChoiceStrategyOptions
} from './pools/selection-strategies/selection-strategies-types.js'
import type { KillBehavior } from './worker/worker-options.js'
import { type IWorker, type WorkerType, WorkerTypes } from './pools/worker.js'
+import type { IPool } from './pools/pool.js'
/**
* Default task name.
/* Intentionally empty */
})
-/**
- * Gets default worker choice strategy options.
- *
- * @param retries - The number of worker choice retries.
- * @returns The default worker choice strategy options.
- */
-const getDefaultInternalWorkerChoiceStrategyOptions = (
- retries: number
-): InternalWorkerChoiceStrategyOptions => {
- return {
- retries,
- runTime: { median: false },
- waitTime: { median: false },
- elu: { median: false }
- }
-}
-
/**
* Default measurement statistics requirements.
*/
}
}
+export const getWorkerChoiceStrategyRetries = <
+ Worker extends IWorker,
+ Data,
+ Response
+>(
+ pool: IPool<Worker, Data, Response>,
+ opts?: WorkerChoiceStrategyOptions
+ ): number => {
+ return (
+ pool.info.maxSize +
+ Object.keys(opts?.weights ?? getDefaultWeights(pool.info.maxSize)).length
+ )
+}
+
const clone = <T>(object: T): T => {
return structuredClone<T>(object)
}
-export const buildInternalWorkerChoiceStrategyOptions = (
- poolMaxSize: number,
- opts?: InternalWorkerChoiceStrategyOptions
-): InternalWorkerChoiceStrategyOptions => {
+export const buildWorkerChoiceStrategyOptions = <
+ Worker extends IWorker,
+ Data,
+ Response
+>(
+ pool: IPool<Worker, Data, Response>,
+ opts?: WorkerChoiceStrategyOptions
+ ): WorkerChoiceStrategyOptions => {
opts = clone(opts ?? {})
- if (opts?.weights == null) {
- opts.weights = getDefaultWeights(poolMaxSize)
- }
+ opts.weights = opts?.weights ?? getDefaultWeights(pool.info.maxSize)
return {
- ...getDefaultInternalWorkerChoiceStrategyOptions(
- poolMaxSize + Object.keys(opts.weights).length
- ),
+ ...{
+ runTime: { median: false },
+ waitTime: { median: false },
+ elu: { median: false }
+ },
...opts
}
}
enableTasksQueue: false,
workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN
})
- expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
- retries:
- pool.info.maxSize +
- Object.keys(pool.workerChoiceStrategyContext.opts.weights).length,
- runTime: { median: false },
- waitTime: { median: false },
- elu: { median: false },
- weights: expect.objectContaining({
- 0: expect.any(Number),
- [pool.info.maxSize - 1]: expect.any(Number)
- })
- })
for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
.workerChoiceStrategies) {
expect(workerChoiceStrategy.opts).toStrictEqual({
- retries:
- pool.info.maxSize +
- Object.keys(workerChoiceStrategy.opts.weights).length,
runTime: { median: false },
waitTime: { median: false },
elu: { median: false },
errorHandler: testHandler,
exitHandler: testHandler
})
- expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
- retries:
- pool.info.maxSize +
- Object.keys(pool.opts.workerChoiceStrategyOptions.weights).length,
- runTime: { median: true },
- waitTime: { median: false },
- elu: { median: false },
- weights: { 0: 300, 1: 200 }
- })
for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
.workerChoiceStrategies) {
expect(workerChoiceStrategy.opts).toStrictEqual({
- retries:
- pool.info.maxSize +
- Object.keys(pool.opts.workerChoiceStrategyOptions.weights).length,
runTime: { median: true },
waitTime: { median: false },
elu: { median: false },
{ workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
)
expect(pool.opts.workerChoiceStrategyOptions).toBeUndefined()
- expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
- retries:
- pool.info.maxSize +
- Object.keys(pool.workerChoiceStrategyContext.opts.weights).length,
- runTime: { median: false },
- waitTime: { median: false },
- elu: { median: false },
- weights: expect.objectContaining({
- 0: expect.any(Number),
- [pool.info.maxSize - 1]: expect.any(Number)
- })
- })
for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
.workerChoiceStrategies) {
expect(workerChoiceStrategy.opts).toStrictEqual({
- retries:
- pool.info.maxSize +
- Object.keys(workerChoiceStrategy.opts.weights).length,
runTime: { median: false },
waitTime: { median: false },
elu: { median: false },
runTime: { median: true },
elu: { median: true }
})
- expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
- retries:
- pool.info.maxSize +
- Object.keys(pool.workerChoiceStrategyContext.opts.weights).length,
- runTime: { median: true },
- waitTime: { median: false },
- elu: { median: true },
- weights: expect.objectContaining({
- 0: expect.any(Number),
- [pool.info.maxSize - 1]: expect.any(Number)
- })
- })
for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
.workerChoiceStrategies) {
expect(workerChoiceStrategy.opts).toStrictEqual({
- retries:
- pool.info.maxSize +
- Object.keys(workerChoiceStrategy.opts.weights).length,
runTime: { median: true },
waitTime: { median: false },
elu: { median: true },
runTime: { median: false },
elu: { median: false }
})
- expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
- retries:
- pool.info.maxSize +
- Object.keys(pool.workerChoiceStrategyContext.opts.weights).length,
- runTime: { median: false },
- waitTime: { median: false },
- elu: { median: false },
- weights: expect.objectContaining({
- 0: expect.any(Number),
- [pool.info.maxSize - 1]: expect.any(Number)
- })
- })
for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
.workerChoiceStrategies) {
expect(workerChoiceStrategy.opts).toStrictEqual({
- retries:
- pool.info.maxSize +
- Object.keys(workerChoiceStrategy.opts.weights).length,
runTime: { median: false },
waitTime: { median: false },
elu: { median: false },
expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
workerChoiceStrategy
)
- expect(pool.opts.workerChoiceStrategyOptions).toBeUndefined()
- expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
- retries:
- pool.info.maxSize +
- Object.keys(pool.workerChoiceStrategyContext.opts.weights).length,
- runTime: { median: false },
- waitTime: { median: false },
- elu: { median: false },
- weights: expect.objectContaining({
- 0: expect.any(Number),
- [pool.info.maxSize - 1]: expect.any(Number)
- })
- })
await pool.destroy()
}
for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
workerChoiceStrategy
)
- expect(pool.opts.workerChoiceStrategyOptions).toBeUndefined()
- expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
- retries:
- pool.info.maxSize +
- Object.keys(pool.workerChoiceStrategyContext.opts.weights).length,
- runTime: { median: false },
- waitTime: { median: false },
- elu: { median: false },
- weights: expect.objectContaining({
- 0: expect.any(Number),
- [pool.info.maxSize - 1]: expect.any(Number)
- })
- })
await pool.destroy()
}
})
})
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 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() return the worker node key chosen by the strategy with fixed pool', () => {
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
fixedPool
)
expect(() => workerChoiceStrategyContext.execute()).toThrow(
new Error(
- `Worker node key chosen is null or undefined after ${
- fixedPool.info.maxSize +
- Object.keys(workerChoiceStrategyContext.opts.weights).length
- } retries`
+ `Worker node key chosen is null or undefined after ${workerChoiceStrategyContext.retries} retries`
)
)
const workerChoiceStrategyNullStub = createStubInstance(
)
expect(() => workerChoiceStrategyContext.execute()).toThrow(
new Error(
- `Worker node key chosen is null or undefined after ${
- fixedPool.info.maxSize +
- Object.keys(workerChoiceStrategyContext.opts.weights).length
- } retries`
+ `Worker node key chosen is null or undefined after ${workerChoiceStrategyContext.retries} retries`
)
)
})
EMPTY_FUNCTION,
availableParallelism,
average,
- buildInternalWorkerChoiceStrategyOptions,
+ buildWorkerChoiceStrategyOptions,
exponentialDelay,
+ getWorkerChoiceStrategyRetries,
getWorkerId,
getWorkerType,
isAsyncFunction,
secureRandom,
sleep
} from '../lib/utils.cjs'
-import { KillBehaviors, WorkerTypes } from '../lib/index.cjs'
+import {
+ FixedClusterPool,
+ FixedThreadPool,
+ KillBehaviors,
+ WorkerTypes
+} from '../lib/index.cjs'
describe('Utils test suite', () => {
it('Verify DEFAULT_TASK_NAME value', () => {
expect(max(1, 1)).toBe(1)
})
- it('Verify buildInternalWorkerChoiceStrategyOptions() behavior', () => {
- const poolMaxSize = 10
- const internalWorkerChoiceStrategyOptions =
- buildInternalWorkerChoiceStrategyOptions(poolMaxSize)
- expect(internalWorkerChoiceStrategyOptions).toStrictEqual({
- retries:
- poolMaxSize +
- Object.keys(internalWorkerChoiceStrategyOptions.weights).length,
+ it('Verify getWorkerChoiceStrategyRetries() behavior', async () => {
+ const numberOfThreads = 4
+ const pool = new FixedThreadPool(
+ numberOfThreads,
+ './tests/worker-files/thread/testWorker.mjs'
+ )
+ expect(getWorkerChoiceStrategyRetries(pool)).toBe(pool.info.maxSize * 2)
+ await pool.destroy()
+ })
+
+ it('Verify buildWorkerChoiceStrategyOptions() behavior', async () => {
+ const numberOfWorkers = 4
+ const pool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.cjs'
+ )
+ expect(buildWorkerChoiceStrategyOptions(pool)).toStrictEqual({
runTime: { median: false },
waitTime: { median: false },
elu: { median: false },
weights: expect.objectContaining({
0: expect.any(Number),
- [poolMaxSize - 1]: expect.any(Number)
+ [pool.info.maxSize - 1]: expect.any(Number)
})
})
+ await pool.destroy()
})
// it('Verify once()', () => {