const expect = require('expect')
-const { FixedClusterPool, FixedThreadPool } = require('../../../lib/index')
+const {
+ FixedClusterPool,
+ FixedThreadPool,
+ WorkerChoiceStrategies
+} = require('../../../lib/index')
const expectedError = new Error('Worker could not be found in tasks map')
+const numberOfWorkers = 1
+
class StubPoolWithTasksMapClear extends FixedThreadPool {
removeAllWorker () {
this.tasks.clear()
describe('Abstract pool test suite', () => {
it('Simulate worker not found during increaseWorkersTask', () => {
const pool = new StubPoolWithTasksMapClear(
- 1,
+ numberOfWorkers,
'./tests/worker-files/thread/testWorker.js'
)
// Simulate worker not found.
pool.removeAllWorker()
expect(() => pool.increaseWorkersTask()).toThrowError(expectedError)
+ pool.destroy()
})
it('Simulate worker not found during decreaseWorkersTasks', () => {
const pool = new StubPoolWithTasksMapClear(
- 1,
+ numberOfWorkers,
'./tests/worker-files/thread/testWorker.js',
{
errorHandler: e => console.error(e)
// Simulate worker not found.
pool.removeAllWorker()
expect(() => pool.decreaseWorkersTasks()).toThrowError(expectedError)
+ pool.destroy()
})
it('Simulate pool creation from a non main thread/process', () => {
expect(
() =>
new StubPoolWithIsMainMethod(
- 1,
+ numberOfWorkers,
'./tests/worker-files/thread/testWorker.js',
{
errorHandler: e => console.error(e)
})
it('Verify that filePath is checked', () => {
- expect(() => new StubPoolWithIsMainMethod(1)).toThrowError(
- new Error('Cannot start a pool from a worker!')
+ const expectedError = new Error(
+ 'Please specify a file with a worker implementation'
+ )
+ expect(() => new FixedThreadPool(numberOfWorkers)).toThrowError(
+ expectedError
)
- expect(() => new StubPoolWithIsMainMethod(1, '')).toThrowError(
- new Error('Cannot start a pool from a worker!')
+ expect(() => new FixedThreadPool(numberOfWorkers, '')).toThrowError(
+ expectedError
)
})
)
)
})
+
+ it('Verify that pool options are checked', () => {
+ let pool = new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ expect(pool.opts.enableEvents).toBe(true)
+ expect(pool.emitter).toBeDefined()
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
+ expect(pool.opts.messageHandler).toBeUndefined()
+ expect(pool.opts.errorHandler).toBeUndefined()
+ expect(pool.opts.onlineHandler).toBeUndefined()
+ expect(pool.opts.exitHandler).toBeUndefined()
+ pool.destroy()
+ const testHandler = () => console.log('test handler executed')
+ pool = new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED,
+ enableEvents: false,
+ messageHandler: testHandler,
+ errorHandler: testHandler,
+ onlineHandler: testHandler,
+ exitHandler: testHandler
+ }
+ )
+ expect(pool.opts.enableEvents).toBe(false)
+ expect(pool.emitter).toBeUndefined()
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.LESS_RECENTLY_USED
+ )
+ expect(pool.opts.messageHandler).toStrictEqual(testHandler)
+ expect(pool.opts.errorHandler).toStrictEqual(testHandler)
+ expect(pool.opts.onlineHandler).toStrictEqual(testHandler)
+ expect(pool.opts.exitHandler).toStrictEqual(testHandler)
+ pool.destroy()
+ })
+
+ it("Verify that pool event emitter 'busy' event can register a callback", () => {
+ const pool = new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ const promises = []
+ let poolBusy = 0
+ pool.emitter.on('busy', () => poolBusy++)
+ for (let i = 0; i < numberOfWorkers * 2; i++) {
+ promises.push(pool.execute({ test: 'test' }))
+ }
+ // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
+ // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
+ expect(poolBusy).toBe(numberOfWorkers + 1)
+ pool.destroy()
+ })
})