+ expect(workerNode.usage).toStrictEqual({
+ tasks: {
+ executed: expect.any(Number),
+ executing: 0,
+ queued: 0,
+ maxQueued: 0,
+ stolen: 0,
+ failed: 0
+ },
+ runTime: expect.objectContaining({
+ history: expect.any(CircularArray)
+ }),
+ waitTime: expect.objectContaining({
+ history: expect.any(CircularArray)
+ }),
+ elu: {
+ idle: {
+ history: new CircularArray()
+ },
+ active: {
+ history: new CircularArray()
+ }
+ }
+ })
+ expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
+ expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
+ max * maxMultiplier
+ )
+ if (workerNode.usage.runTime.aggregate == null) {
+ expect(workerNode.usage.runTime.aggregate).toBeUndefined()
+ } else {
+ expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
+ }
+ if (workerNode.usage.waitTime.aggregate == null) {
+ expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
+ } else {
+ expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
+ }
+ }
+ expect(
+ pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+ pool.workerChoiceStrategyContext.workerChoiceStrategy
+ ).nextWorkerNodeKey
+ ).toEqual(expect.any(Number))
+ expect(
+ pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+ pool.workerChoiceStrategyContext.workerChoiceStrategy
+ ).previousWorkerNodeKey
+ ).toEqual(expect.any(Number))
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify LEAST_BUSY strategy can be run in a dynamic pool', async () => {
+ const pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY }
+ )
+ // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
+ const promises = new Set()
+ const maxMultiplier = 2
+ for (let i = 0; i < max * maxMultiplier; i++) {
+ promises.add(pool.execute())
+ }
+ await Promise.all(promises)
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode.usage).toStrictEqual({
+ tasks: {
+ executed: expect.any(Number),
+ executing: 0,
+ queued: 0,
+ maxQueued: 0,
+ stolen: 0,
+ failed: 0
+ },
+ runTime: expect.objectContaining({
+ history: expect.any(CircularArray)
+ }),
+ waitTime: expect.objectContaining({
+ history: expect.any(CircularArray)
+ }),
+ elu: {
+ idle: {
+ history: new CircularArray()
+ },
+ active: {
+ history: new CircularArray()
+ }
+ }
+ })
+ expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
+ expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
+ max * maxMultiplier
+ )
+ if (workerNode.usage.runTime.aggregate == null) {
+ expect(workerNode.usage.runTime.aggregate).toBeUndefined()
+ } else {
+ expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
+ }
+ if (workerNode.usage.waitTime.aggregate == null) {
+ expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
+ } else {
+ expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
+ }
+ }
+ expect(
+ pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+ pool.workerChoiceStrategyContext.workerChoiceStrategy
+ ).nextWorkerNodeKey
+ ).toEqual(expect.any(Number))
+ expect(
+ pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+ pool.workerChoiceStrategyContext.workerChoiceStrategy
+ ).previousWorkerNodeKey
+ ).toEqual(expect.any(Number))
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify LEAST_ELU strategy default policy', async () => {
+ const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_ELU
+ let pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy }
+ )
+ expect(pool.workerChoiceStrategyContext.getStrategyPolicy()).toStrictEqual({
+ dynamicWorkerUsage: false,
+ dynamicWorkerReady: true
+ })
+ await pool.destroy()
+ pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy }
+ )
+ expect(pool.workerChoiceStrategyContext.getStrategyPolicy()).toStrictEqual({
+ dynamicWorkerUsage: false,
+ dynamicWorkerReady: true
+ })
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify LEAST_ELU strategy default tasks statistics requirements', async () => {
+ const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_ELU
+ let pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy }
+ )
+ expect(
+ pool.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ ).toStrictEqual({
+ runTime: {
+ aggregate: false,
+ average: false,
+ median: false
+ },
+ waitTime: {
+ aggregate: false,
+ average: false,
+ median: false
+ },
+ elu: {
+ aggregate: true,
+ average: false,
+ median: false
+ }
+ })
+ await pool.destroy()
+ pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy }
+ )
+ expect(
+ pool.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ ).toStrictEqual({
+ runTime: {
+ aggregate: false,
+ average: false,
+ median: false
+ },
+ waitTime: {
+ aggregate: false,
+ average: false,
+ median: false
+ },
+ elu: {
+ aggregate: true,
+ average: false,
+ median: false
+ }
+ })
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify LEAST_ELU strategy can be run in a fixed pool', async () => {
+ const pool = new FixedThreadPool(
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_ELU }
+ )
+ // TODO: Create a better test to cover `LeastEluWorkerChoiceStrategy#choose`
+ const promises = new Set()
+ const maxMultiplier = 2
+ for (let i = 0; i < max * maxMultiplier; i++) {
+ promises.add(pool.execute())
+ }
+ await Promise.all(promises)
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode.usage).toStrictEqual({
+ tasks: {
+ executed: expect.any(Number),
+ executing: 0,
+ queued: 0,
+ maxQueued: 0,
+ stolen: 0,
+ failed: 0
+ },
+ runTime: {
+ history: new CircularArray()
+ },
+ waitTime: {
+ history: new CircularArray()
+ },
+ elu: expect.objectContaining({
+ idle: expect.objectContaining({
+ history: expect.any(CircularArray)
+ }),
+ active: expect.objectContaining({
+ history: expect.any(CircularArray)
+ })
+ })