+ it('Simulate pool creation from a non main thread/process', () => {
+ expect(
+ () =>
+ new StubPoolWithIsMainMethod(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+ ).toThrowError(new Error('Cannot start a pool from a worker!'))
+ })
+
+ it('Verify that filePath is checked', () => {
+ const expectedError = new Error(
+ 'Please specify a file with a worker implementation'
+ )
+ expect(() => new FixedThreadPool(numberOfWorkers)).toThrowError(
+ expectedError
+ )
+ expect(() => new FixedThreadPool(numberOfWorkers, '')).toThrowError(
+ expectedError
+ )
+ })
+
+ it('Verify that numberOfWorkers is checked', () => {
+ expect(() => new FixedThreadPool()).toThrowError(
+ new Error(
+ 'Cannot instantiate a pool without specifying the number of workers'
+ )
+ )
+ })
+
+ it('Verify that a negative number of workers is checked', () => {
+ expect(
+ () =>
+ new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js')
+ ).toThrowError(
+ new Error('Cannot instantiate a pool with a negative number of workers')
+ )
+ })
+
+ it('Verify that a non integer number of workers is checked', () => {
+ expect(
+ () =>
+ new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js')
+ ).toThrowError(
+ new Error(
+ 'Cannot instantiate a pool with a non integer number of workers'
+ )
+ )
+ })
+
+ 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('Simulate worker not found during increaseWorkerRunningTasks', () => {
+ const pool = new StubPoolWithWorkerTasksUsageMapClear(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js'
+ )
+ // Simulate worker not found.
+ pool.removeAllWorker()
+ expect(() => pool.increaseWorkerRunningTasks()).toThrowError(
+ workerNotFoundInTasksUsageMapError
+ )
+ pool.destroy()
+ })
+
+ it('Simulate worker not found during decreaseWorkerRunningTasks', () => {
+ const pool = new StubPoolWithWorkerTasksUsageMapClear(
+ numberOfWorkers,