Skip to content

Commit

Permalink
test: cleanup resources setup and teardown
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
  • Loading branch information
jerome-benoit committed Jul 31, 2024
1 parent 311c5bd commit e1e0cb2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,34 @@ import {
} from '../../../lib/pools/selection-strategies/selection-strategies-utils.cjs'

describe('Selection strategies utils test suite', () => {
it('Verify buildWorkerChoiceStrategyOptions() behavior', async () => {
const numberOfWorkers = 4
const pool = new FixedClusterPool(
const numberOfWorkers = 4
const numberOfThreads = 4
let clusterFixedPool, threadFixedPool

before('Create pools', () => {
clusterFixedPool = new FixedClusterPool(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.cjs'
)
expect(buildWorkerChoiceStrategyOptions(pool)).toStrictEqual({
threadFixedPool = new FixedThreadPool(
numberOfThreads,
'./tests/worker-files/thread/testWorker.mjs'
)
})

after('Destroy pools', async () => {
await clusterFixedPool.destroy()
await threadFixedPool.destroy()
})

it('Verify buildWorkerChoiceStrategyOptions() behavior', async () => {
expect(buildWorkerChoiceStrategyOptions(clusterFixedPool)).toStrictEqual({
runTime: { median: false },
waitTime: { median: false },
elu: { median: false },
weights: expect.objectContaining({
0: expect.any(Number),
[pool.info.maxSize - 1]: expect.any(Number),
[clusterFixedPool.info.maxSize - 1]: expect.any(Number),
}),
})
const workerChoiceStrategyOptions = {
Expand All @@ -32,18 +47,17 @@ describe('Selection strategies utils test suite', () => {
},
}
expect(
buildWorkerChoiceStrategyOptions(pool, workerChoiceStrategyOptions)
buildWorkerChoiceStrategyOptions(
clusterFixedPool,
workerChoiceStrategyOptions
)
).toStrictEqual(workerChoiceStrategyOptions)
await pool.destroy()
})

it('Verify getWorkerChoiceStrategyRetries() behavior', async () => {
const numberOfThreads = 4
const pool = new FixedThreadPool(
numberOfThreads,
'./tests/worker-files/thread/testWorker.mjs'
expect(getWorkerChoiceStrategiesRetries(threadFixedPool)).toBe(
threadFixedPool.info.maxSize * 2
)
expect(getWorkerChoiceStrategiesRetries(pool)).toBe(pool.info.maxSize * 2)
const workerChoiceStrategyOptions = {
runTime: { median: true },
waitTime: { median: true },
Expand All @@ -54,11 +68,13 @@ describe('Selection strategies utils test suite', () => {
},
}
expect(
getWorkerChoiceStrategiesRetries(pool, workerChoiceStrategyOptions)
getWorkerChoiceStrategiesRetries(
threadFixedPool,
workerChoiceStrategyOptions
)
).toBe(
pool.info.maxSize +
threadFixedPool.info.maxSize +
Object.keys(workerChoiceStrategyOptions.weights).length
)
await pool.destroy()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ describe('Weighted round robin strategy worker choice strategy test suite', () =
const max = 3
let pool

before(() => {
before('Create pool', () => {
pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.mjs'
)
})

after(async () => {
after('Destroy pool', async () => {
await pool.destroy()
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Worker choice strategies context test suite', () => {
const max = 3
let fixedPool, dynamicPool

before(() => {
before('Create pools', () => {
fixedPool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.mjs'
Expand All @@ -36,7 +36,7 @@ describe('Worker choice strategies context test suite', () => {
restore()
})

after(async () => {
after('Destroy pools', async () => {
await fixedPool.destroy()
await dynamicPool.destroy()
})
Expand Down
45 changes: 27 additions & 18 deletions tests/pools/worker-node.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,33 @@ import { PriorityQueue } from '../../lib/queues/priority-queue.cjs'
import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs'

describe('Worker node test suite', () => {
const threadWorkerNode = new WorkerNode(
WorkerTypes.thread,
'./tests/worker-files/thread/testWorker.mjs',
{
tasksQueueBackPressureSize: 12,
tasksQueueBucketSize: 6,
tasksQueuePriority: true,
}
)
const clusterWorkerNode = new WorkerNode(
WorkerTypes.cluster,
'./tests/worker-files/cluster/testWorker.cjs',
{
tasksQueueBackPressureSize: 12,
tasksQueueBucketSize: 6,
tasksQueuePriority: true,
}
)
let threadWorkerNode, clusterWorkerNode

before('Create worker nodes', () => {
threadWorkerNode = new WorkerNode(
WorkerTypes.thread,
'./tests/worker-files/thread/testWorker.mjs',
{
tasksQueueBackPressureSize: 12,
tasksQueueBucketSize: 6,
tasksQueuePriority: true,
}
)
clusterWorkerNode = new WorkerNode(
WorkerTypes.cluster,
'./tests/worker-files/cluster/testWorker.cjs',
{
tasksQueueBackPressureSize: 12,
tasksQueueBucketSize: 6,
tasksQueuePriority: true,
}
)
})

after('Terminate worker nodes', async () => {
await threadWorkerNode.terminate()
await clusterWorkerNode.terminate()
})

it('Worker node instantiation', () => {
expect(() => new WorkerNode()).toThrow(
Expand Down

0 comments on commit e1e0cb2

Please sign in to comment.