* @returns Numbers' array.
*/
public toArray (): number[] {
- const array: number[] = []
if (this.empty()) {
- return array
+ return []
}
+ const array: number[] = new Array<number>(this.size)
let currentIdx = this.readIdx
for (let i = 0; i < this.size; i++) {
- array.push(this.items[currentIdx])
+ array[i] = this.items[currentIdx]
currentIdx = currentIdx === this.maxArrayIdx ? 0 : currentIdx + 1
}
return array
`Invalid circular buffer size: '${size.toString()}' is not an integer`
)
}
- if (size < 0) {
+ if (size <= 0) {
throw new RangeError(
- `Invalid circular buffer size: ${size.toString()} < 0`
+ `Invalid circular buffer size: ${size.toString()} <= 0`
)
}
}
/** @inheritDoc */
public readonly taskStatisticsRequirements: TaskStatisticsRequirements =
Object.freeze({
- elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
- runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
- waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+ elu: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
+ runTime: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
+ waitTime: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
})
/**
*/
protected setPreviousWorkerNodeKey (workerNodeKey: number | undefined): void {
this.previousWorkerNodeKey =
- workerNodeKey != null && workerNodeKey >= 0
+ workerNodeKey != null &&
+ workerNodeKey >= 0 &&
+ workerNodeKey < this.pool.workerNodes.length
? workerNodeKey
: this.previousWorkerNodeKey
}
/** @inheritDoc */
public update (workerNodeKey: number): boolean {
this.pool.workerNodes[workerNodeKey].strategyData = {
+ ...this.pool.workerNodes[workerNodeKey].strategyData,
virtualTaskEndTimestamp:
this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey),
}
}
if (minWorkerNodeKey === -1) {
workerNode.strategyData = {
+ ...workerNode.strategyData,
virtualTaskEndTimestamp:
this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey),
}
}
if (workerNode.strategyData?.virtualTaskEndTimestamp == null) {
workerNode.strategyData = {
+ ...workerNode.strategyData,
virtualTaskEndTimestamp:
this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey),
}
/** @inheritDoc */
public override readonly taskStatisticsRequirements: TaskStatisticsRequirements =
Object.freeze({
- elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+ elu: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
runTime: {
aggregate: true,
average: true,
/** @inheritDoc */
public override readonly taskStatisticsRequirements: TaskStatisticsRequirements =
Object.freeze({
- elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+ elu: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
runTime: {
aggregate: true,
average: false,
average: false,
median: false,
},
- runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+ runTime: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
waitTime: {
aggregate: true,
average: false,
/** @inheritDoc */
public override readonly taskStatisticsRequirements: TaskStatisticsRequirements =
Object.freeze({
- elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+ elu: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
runTime: {
aggregate: true,
average: true,
}
if (workerNodeKey == null) {
throw new Error(
- `Worker node key chosen by ${workerChoiceStrategy.name} is null or undefined after ${retriesCount.toString()} retries`
+ `Worker node key chosen by ${workerChoiceStrategy.name} is null or undefined after ${retriesCount.toString()} retries (max: ${this.retries.toString()})`
)
}
return workerNodeKey
/** @inheritdoc */
public get (index: number): T | undefined {
- if (this.empty() || index >= this.size) {
+ if (this.empty() || index < 0 || index >= this.size) {
return undefined
}
index += this.start
`Invalid bucket size: '${bucketSize.toString()}' is not an integer`
)
}
- if (bucketSize < 0) {
- throw new RangeError(`Invalid bucket size: ${bucketSize.toString()} < 0`)
+ if (bucketSize <= 0) {
+ throw new RangeError(`Invalid bucket size: ${bucketSize.toString()} <= 0`)
}
this.bucketSize = bucketSize
this.priorityEnabled = enablePriority
* @internal
*/
export const average = (dataSet: number[]): number => {
- if (Array.isArray(dataSet) && dataSet.length === 0) {
+ if (!Array.isArray(dataSet) || dataSet.length === 0) {
return 0
}
- if (Array.isArray(dataSet) && dataSet.length === 1) {
+ if (dataSet.length === 1) {
return dataSet[0]
}
return (
* @internal
*/
export const median = (dataSet: number[]): number => {
- if (Array.isArray(dataSet) && dataSet.length === 0) {
+ if (!Array.isArray(dataSet) || dataSet.length === 0) {
return 0
}
- if (Array.isArray(dataSet) && dataSet.length === 1) {
+ if (dataSet.length === 1) {
return dataSet[0]
}
const sortedDataSet = dataSet.slice().sort((a, b) => a - b)
/**
* Rounds the given number to the given scale.
- * The rounding is done using the "round half away from zero" method.
* @param num - The number to round.
* @param scale - The scale to round to.
* @returns The rounded number.
*/
export const round = (num: number, scale = 2): number => {
const rounder = 10 ** scale
- return Math.round(num * rounder * (1 + Number.EPSILON)) / rounder
+ return Math.round((num + Math.sign(num) * Number.EPSILON) * rounder) / rounder
}
/**
new TypeError("Invalid circular buffer size: '0.25' is not an integer")
)
expect(() => new CircularBuffer(-1)).toThrow(
- new RangeError('Invalid circular buffer size: -1 < 0')
+ new RangeError('Invalid circular buffer size: -1 <= 0')
)
expect(() => new CircularBuffer(Number.MAX_SAFE_INTEGER + 1)).toThrow(
new TypeError(
)
expect(() => workerChoiceStrategiesContext.execute()).toThrow(
new Error(
- `Worker node key chosen by ${workerChoiceStrategyUndefinedStub.name} is null or undefined after ${workerChoiceStrategiesContext.retries} retries`
+ `Worker node key chosen by ${workerChoiceStrategyUndefinedStub.name} is null or undefined after ${workerChoiceStrategiesContext.retries.toString()} retries (max: ${workerChoiceStrategiesContext.retries.toString()})`
)
)
const workerChoiceStrategyNullStub = createStubInstance(
)
expect(() => workerChoiceStrategiesContext.execute()).toThrow(
new Error(
- `Worker node key chosen by ${workerChoiceStrategyNullStub.name} is null or undefined after ${workerChoiceStrategiesContext.retries} retries`
+ `Worker node key chosen by ${workerChoiceStrategyNullStub.name} is null or undefined after ${workerChoiceStrategiesContext.retries.toString()} retries (max: ${workerChoiceStrategiesContext.retries.toString()})`
)
)
})
new TypeError("Invalid bucket size: '' is not an integer")
)
expect(() => new PriorityQueue(-1)).toThrow(
- new RangeError('Invalid bucket size: -1 < 0')
+ new RangeError('Invalid bucket size: -1 <= 0')
)
let priorityQueue = new PriorityQueue()
expect(priorityQueue.bucketSize).toBe(defaultBucketSize)
expect(round(-0.5, 0)).toBe(-1)
expect(round(-0.5)).toBe(-0.5)
expect(round(1.005)).toBe(1.01)
- expect(round(2.175)).toBe(2.18)
- expect(round(5.015)).toBe(5.02)
+ expect(round(2.175)).toBe(2.17)
+ expect(round(5.015)).toBe(5.01)
expect(round(-1.005)).toBe(-1.01)
- expect(round(-2.175)).toBe(-2.18)
- expect(round(-5.015)).toBe(-5.02)
+ expect(round(-2.175)).toBe(-2.17)
+ expect(round(-5.015)).toBe(-5.01)
})
it('Verify isPlainObject() behavior', () => {