X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fworker%2Fthread-worker.test.mjs;h=6d694ec6464c412d1d3860d246438689abb44e47;hb=ecde6ea8c439bbdd9dc2ca118731b5006a1a9884;hp=26da45e12bbe1ac63e3bfa169564faf7d59abcff;hpb=87b217c5d1b3dd83ced97831a59fac8112a44c3e;p=poolifier.git diff --git a/tests/worker/thread-worker.test.mjs b/tests/worker/thread-worker.test.mjs index 26da45e1..6d694ec6 100644 --- a/tests/worker/thread-worker.test.mjs +++ b/tests/worker/thread-worker.test.mjs @@ -1,20 +1,22 @@ import { expect } from 'expect' import { restore, stub } from 'sinon' -import { ThreadWorker } from '../../lib/index.js' -import { DEFAULT_TASK_NAME } from '../../lib/utils.js' -describe('Thread worker test suite', () => { - class SpyWorker extends ThreadWorker { - constructor (fn) { - super(fn) - this.port = { postMessage: stub().returns() } - } - } +import { ThreadWorker } from '../../lib/index.cjs' +import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs' +describe('Thread worker test suite', () => { afterEach(() => { restore() }) + it('Verify worker properties value after initialization', () => { + const worker = new ThreadWorker(() => {}) + expect(worker.isMain).toBe(true) + expect(worker.mainWorker).toBe(null) + expect(worker.taskFunctions).toBeInstanceOf(Map) + expect(worker.taskFunctions.size).toBe(2) + }) + it('Verify that sync kill handler is called when worker is killed', () => { const worker = new ThreadWorker(() => {}, { killHandler: stub().returns() @@ -40,6 +42,9 @@ describe('Thread worker test suite', () => { return 2 } const worker = new ThreadWorker({ fn1, fn2 }) + worker.port = { + postMessage: stub().returns() + } expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({ status: false, error: new TypeError('name parameter is not a string') @@ -48,9 +53,6 @@ describe('Thread worker test suite', () => { status: false, error: new TypeError('name parameter is an empty string') }) - worker.port = { - postMessage: stub().returns() - } expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) @@ -87,8 +89,9 @@ describe('Thread worker test suite', () => { expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage) }) - it('Verify worker invokes the postMessage() method on port property', () => { - const worker = new SpyWorker(() => {}) + it('Verify that sendToMainWorker() method invokes the port property postMessage() method', () => { + const worker = new ThreadWorker(() => {}) + worker.port = { postMessage: stub().returns() } worker.sendToMainWorker({ ok: 1 }) expect(worker.port.postMessage.calledOnce).toBe(true) })