From 98446b391031d7add0e0d7f7e310e25a014370e2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 25 Aug 2023 20:25:08 +0200 Subject: [PATCH] test: add UTs MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .eslintrc.js | 1 + .../selection-strategies-types.ts | 2 +- src/utils.ts | 3 ++- tests/utils.test.js | 20 +++++++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index a6ad5b1d..9a058e82 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -96,6 +96,7 @@ module.exports = defineConfig({ 'unref', 'unregister', 'utf8', + 'webcrypto', 'workerpool', 'ws', 'wss', diff --git a/src/pools/selection-strategies/selection-strategies-types.ts b/src/pools/selection-strategies/selection-strategies-types.ts index 855c7ad1..e7bf0e1e 100644 --- a/src/pools/selection-strategies/selection-strategies-types.ts +++ b/src/pools/selection-strategies/selection-strategies-types.ts @@ -97,7 +97,7 @@ export interface WorkerChoiceStrategyOptions { readonly elu?: MeasurementOptions /** * Worker weights to use for weighted round robin worker selection strategies. - * A weight is the tasks maximum execution time in milliseconds for each worker node. + * A weight is tasks maximum execution time in milliseconds for a worker node. * * @defaultValue Weights computed automatically given the CPU performance. */ diff --git a/src/utils.ts b/src/utils.ts index 7a0ed2f6..b84c4c80 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,5 @@ import * as os from 'node:os' +import { webcrypto } from 'node:crypto' import type { MeasurementStatisticsRequirements, WorkerChoiceStrategyOptions @@ -253,5 +254,5 @@ export const once = ( * @returns A number in the [0,1[ range */ export const secureRandom = (): number => { - return crypto.getRandomValues(new Uint32Array(1))[0] / 0x100000000 + return webcrypto.getRandomValues(new Uint32Array(1))[0] / 0x100000000 } diff --git a/tests/utils.test.js b/tests/utils.test.js index 76abf046..c00f0307 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -1,3 +1,4 @@ +const { randomInt } = require('crypto') const { expect } = require('expect') const { CircularArray, @@ -6,12 +7,14 @@ const { const { availableParallelism, average, + exponentialDelay, isAsyncFunction, isKillBehavior, isPlainObject, median, round, secureRandom, + sleep, updateMeasurementStatistics } = require('../lib/utils') const { KillBehaviors } = require('../lib/worker/worker-options') @@ -23,6 +26,23 @@ describe('Utils test suite', () => { expect(Number.isSafeInteger(availableParallelism())).toBe(true) }) + it('Verify sleep() behavior', async () => { + const now = performance.now() + await sleep(1000) + const elapsed = performance.now() - now + expect(elapsed).toBeGreaterThanOrEqual(1000) + }) + + it('Verify exponentialDelay() behavior', () => { + expect(typeof exponentialDelay(randomInt(1000)) === 'number').toBe(true) + expect(exponentialDelay(randomInt(1000))).toBeGreaterThanOrEqual( + Number.MIN_VALUE + ) + expect(exponentialDelay(randomInt(1000))).toBeLessThanOrEqual( + Number.MAX_VALUE + ) + }) + it('Verify average() computation', () => { expect(average([])).toBe(0) expect(average([0.08])).toBe(0.08) -- 2.34.1