diff --git a/commonlibrary/ets_utils/taskpool_lib_standard/entry/src/main/ets/test/TaskPool.test.js b/commonlibrary/ets_utils/taskpool_lib_standard/entry/src/main/ets/test/TaskPool.test.js index e8d2e349960f547ba595c02ca5687649cf511500..bb909852b4ad746b163b469469f98c16ea7305b1 100644 --- a/commonlibrary/ets_utils/taskpool_lib_standard/entry/src/main/ets/test/TaskPool.test.js +++ b/commonlibrary/ets_utils/taskpool_lib_standard/entry/src/main/ets/test/TaskPool.test.js @@ -1671,5 +1671,60 @@ describe('ActsAbilityTest', function () { expect(state != 0).assertTrue(); done(); }) + + /** + * @tc.number : TaskPoolTestClass068 + * @tc.name : SharedArrayBuffer with taskpool + * @tc.desc : transfer SharedArrayBuffer with taskpool + * @tc.size : MEDIUM + * @tc.type : Function + * @tc.level : Level 0 + */ + it('TaskPoolTestClass068', 0, async function (done) { + let sab = new SharedArrayBuffer(20); + let int32 = new Uint32Array(sab); + function testTransfer(arg1) { + "use concurrent" + arg1[0] = 100; + arg1[1] = 200; + arg1[2] = 300; + arg1[3] = 400; + return "success"; + } + let task = new taskpool.Task(testTransfer, int32); + taskpool.execute(task).then((res)=> { + let val = int32[0] + int32[1] + int32[2] + int32[3]; + expect(val).assertEqual(1000); + }); + done(); + }) + + /** + * @tc.number : TaskPoolTestClass069 + * @tc.name : SharedArrayBuffer and Atomics with taskpool + * @tc.desc : transfer SharedArrayBuffer and Atomics with taskpool + * @tc.size : MEDIUM + * @tc.type : Function + * @tc.level : Level 0 + */ + it('TaskPoolTestClass069', 0, async function (done) { + let sab = new SharedArrayBuffer(20); + let int32 = new Int32Array(sab); + function testTransfer(arg1) { + "use concurrent" + console.info("wait begin::"); + let res = Atomics.wait(arg1, 0, 0, 3000); + return res; + } + + let task = new taskpool.Task(testTransfer, int32); + taskpool.execute(task).then((res) => { + expect(res).assertEqual("ok"); + }); + setTimeout(() => { + Atomics.notify(int32, 0, 1); + }, 1000); + done(); + }) }) } \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/test/ThreadWorker.test.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/test/ThreadWorker.test.js index 0e9c9f2af718ad1d563965891e024cd6caae954a..4331d465036cf1f6be61a7e4262176b3b7bfa97e 100644 --- a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/test/ThreadWorker.test.js +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/test/ThreadWorker.test.js @@ -2546,5 +2546,224 @@ describe('threadWorkerTest', function () { expect(res == 1).assertTrue(); done(); }) + + // Check the SharedArrayBuffer with worker. + /** + * @tc.name: threadWorker_worker_SharedArrayBuffer_test_001 + * @tc.desc: Check the SharedArrayBuffer with worker is ok. + */ + it('threadWorker_worker_SharedArrayBuffer_test_001', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_028.js"); + let sab = new SharedArrayBuffer(20); + let int32 = new Uint32Array(sab); + + let res = 0; + let isTerminate = false; + ss.onmessage = function (d) { + if (d.data == "success") { + res = int32[0] + int32[1] + int32[2] + int32[3]; + } + ss.terminate(); + } + ss.onexit = function() { + isTerminate = true; + } + ss.postMessage(int32); + while (!isTerminate) { + await promiseCase(); + } + expect(res == 1000).assertTrue(); + done(); + }) + + // Check the listener of worker. + /** + * @tc.name: threadWorker_worker_listener_test_001 + * @tc.desc: Check the listener of worker is ok. + */ + it('threadWorker_worker_listener_test_001', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_029.js"); + let res = undefined; + let flag = false; + let isTerminate = false; + + ss.on('message', (event) =>{ + flag = true; + let jsonData = JSON.parse(JSON.stringify(event)); + res = jsonData.data; + ss.terminate(); + }); + + ss.on('exit', (code) => { + isTerminate = true; + }) + ss.postMessage("123"); + + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual("hello worker") + done(); + }) + + // Check the listener of worker. + /** + * @tc.name: threadWorker_worker_listener_test_002 + * @tc.desc: Check the listener of worker is ok. + */ + it('threadWorker_worker_listener_test_002', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_029.js"); + let res = 0; + let flag = false; + let isTerminate = false; + + ss.addEventListener('messageerror', (event) =>{ + flag = true; + res += 1; + }); + + ss.addEventListener('exit', (code) => { + isTerminate = true; + }) + + try { + const data = Symbol(); + ss.postMessage(data); + } catch (error) { + while (!flag) { + await promiseCase(); + } + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + } + expect(res).assertEqual(1) + done(); + }) + + // Check the listener of worker. + /** + * @tc.name: threadWorker_worker_listener_test_003 + * @tc.desc: Check the listener of worker is ok. + */ + it('threadWorker_worker_listener_test_003', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_007.js"); + let res = undefined; + let isTerminate = false; + + ss.addEventListener('error', (event) =>{ + let jsonData = JSON.parse(JSON.stringify(event)); + res = jsonData.message; + ss.terminate(); + }); + + ss.addEventListener('exit', (code) => { + isTerminate = true; + }) + ss.postMessage("123"); + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual("Error: 123") + done(); + }) + + // Check the listener of worker. + /** + * @tc.name: threadWorker_worker_listener_test_004 + * @tc.desc: Check the listener of worker is ok. + */ + it('threadWorker_worker_listener_test_004', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_007.js"); + let res = undefined; + let isTerminate = false; + + ss.on('error', (event) =>{ + let jsonData = JSON.parse(JSON.stringify(event)); + res = jsonData.message; + ss.terminate(); + }); + + ss.on('exit', (code) => { + isTerminate = true; + }) + ss.postMessage("123"); + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual("Error: 123") + done(); + }) + + // Check the listener of worker. + /** + * @tc.name: threadWorker_worker_listener_test_005 + * @tc.desc: Check the listener of worker is ok. + */ + it('threadWorker_worker_listener_test_005', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_029.js"); + let res1 = ""; + let res2 = ""; + let flag1 = false; + let flag2 = false; + let isTerminate = false; + + ss.on('message', (event) =>{ + flag1 = true; + let jsonData = JSON.parse(JSON.stringify(event)); + res1 = jsonData.data; + }); + + ss.onmessage = function (d) { + flag2 = true; + let data = d.data; + res2 = data; + } + + ss.on('exit', (code) => { + isTerminate = true; + }) + ss.postMessage("123"); + while (!flag1 || !flag2) { + await promiseCase(); + } + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res1).assertEqual("hello worker") + expect(res2).assertEqual("hello worker") + done(); + }) + + // Check the listener of worker. + /** + * @tc.name: threadWorker_worker_listener_test_006 + * @tc.desc: Check the listener of worker is ok. + */ + it('threadWorker_worker_listener_test_006', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_030.js"); + let res = 0; + let isTerminate = false; + + ss.on('message', (event) =>{ + res += 1; + if (res == 3) { + ss.terminate(); + } + }); + ss.on('exit', (code) => { + isTerminate = true; + }) + + ss.postMessage("123"); + + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual(3) + done(); + }) }) } \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_028.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_028.js new file mode 100644 index 0000000000000000000000000000000000000000..114d6f185ac40cd2f85f3b04f5eec4445c9fdd33 --- /dev/null +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_028.js @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const workerPort = worker.workerPort; + +workerPort.onmessage = function (d) { + let int32 = d.data; + int32[0] = 100; + int32[1] = 200; + int32[2] = 300; + int32[3] = 400; + workerPort.postMessage("success"); +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_029.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_029.js new file mode 100644 index 0000000000000000000000000000000000000000..b3c6f0598312dea3bb25a3462e6796f4bd5407bc --- /dev/null +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_029.js @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const workerPort = worker.workerPort; + +workerPort.addEventListener("message", (event) => { + workerPort.postMessage("hello worker") +}) \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_030.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_030.js new file mode 100644 index 0000000000000000000000000000000000000000..7f5055dce96ee49d8d85296ae89e755c49b445ad --- /dev/null +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_030.js @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const workerPort = worker.workerPort; + +workerPort.addEventListener("addEventTest", (event) => { + workerPort.postMessage("123") +}) + +workerPort.addEventListener("onceTest", (event) => { + workerPort.postMessage("456") +}) + +workerPort.addEventListener("message", (event) => { + workerPort.dispatchEvent({type:"addEventTest", timeStamp:0}) + workerPort.dispatchEvent({type:"addEventTest", timeStamp:0}) + workerPort.removeEventListener("addEventTest"); + workerPort.dispatchEvent({type:"addEventTest", timeStamp:0}) + workerPort.dispatchEvent({type:"onceTest", timeStamp:0}) + workerPort.removeEventListener("onceTest") +}) \ No newline at end of file diff --git a/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/test/WorkerTest.test.js b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/test/WorkerTest.test.js index 898a19327cf541d9099ea3bf41c2617962cc0608..c8bbf0ff27796983240b72819b579b9effd555df 100644 --- a/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/test/WorkerTest.test.js +++ b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/test/WorkerTest.test.js @@ -1734,5 +1734,224 @@ describe('WorkerTest', function () { expect(result).assertEqual("unInit"); done(); }) + + // Check the SharedArrayBuffer with worker. + /** + * @tc.name: worker_SharedArrayBuffer_test_001 + * @tc.desc: Check the SharedArrayBuffer with worker is ok. + */ + it('worker_SharedArrayBuffer_test_001', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_024.js"); + let sab = new SharedArrayBuffer(20); + let int32 = new Uint32Array(sab); + + let res = 0; + let isTerminate = false; + ss.onmessage = function (d) { + if (d.data == "success") { + res = int32[0] + int32[1] + int32[2] + int32[3]; + } + ss.terminate(); + } + ss.onexit = function() { + isTerminate = true; + } + ss.postMessage(int32); + while (!isTerminate) { + await promiseCase(); + } + expect(res == 1000).assertTrue(); + done(); + }) + + // Check the listener of worker. + /** + * @tc.name: worker_listener_test_001 + * @tc.desc: Check the listener of worker is ok. + */ + it('worker_listener_test_001', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_025.js"); + let res = undefined; + let flag = false; + let isTerminate = false; + + ss.on('message', (event) =>{ + flag = true; + let jsonData = JSON.parse(JSON.stringify(event)); + res = jsonData.data; + ss.terminate(); + }); + + ss.on('exit', (code) => { + isTerminate = true; + }) + ss.postMessage("123"); + + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual("hello worker") + done(); + }) + + // Check the listener of worker. + /** + * @tc.name: worker_listener_test_002 + * @tc.desc: Check the listener of worker is ok. + */ + it('worker_listener_test_002', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_025.js"); + let res = 0; + let flag = false; + let isTerminate = false; + + ss.addEventListener('messageerror', (event) =>{ + flag = true; + res += 1; + }); + + ss.addEventListener('exit', (code) => { + isTerminate = true; + }) + + try { + const data = Symbol(); + ss.postMessage(data); + } catch (error) { + while (!flag) { + await promiseCase(); + } + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + } + expect(res).assertEqual(1) + done(); + }) + + // Check the listener of worker. + /** + * @tc.name: worker_listener_test_003 + * @tc.desc: Check the listener of worker is ok. + */ + it('worker_listener_test_003', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_007.js"); + let res = undefined; + let isTerminate = false; + + ss.addEventListener('error', (event) =>{ + let jsonData = JSON.parse(JSON.stringify(event)); + res = jsonData.message; + ss.terminate(); + }); + + ss.addEventListener('exit', (code) => { + isTerminate = true; + }) + ss.postMessage("123"); + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual("Error: 123") + done(); + }) + + // Check the listener of worker. + /** + * @tc.name: worker_listener_test_004 + * @tc.desc: Check the listener of worker is ok. + */ + it('worker_listener_test_004', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_007.js"); + let res = undefined; + let isTerminate = false; + + ss.on('error', (event) =>{ + let jsonData = JSON.parse(JSON.stringify(event)); + res = jsonData.message; + ss.terminate(); + }); + + ss.on('exit', (code) => { + isTerminate = true; + }) + ss.postMessage("123"); + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual("Error: 123") + done(); + }) + + // Check the listener of worker. + /** + * @tc.name: worker_listener_test_005 + * @tc.desc: Check the listener of worker is ok. + */ + it('worker_listener_test_005', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_025.js"); + let res1 = ""; + let res2 = ""; + let flag1 = false; + let flag2 = false; + let isTerminate = false; + + ss.on('message', (event) =>{ + flag1 = true; + let jsonData = JSON.parse(JSON.stringify(event)); + res1 = jsonData.data; + }); + + ss.onmessage = function (d) { + flag2 = true; + let data = d.data; + res2 = data; + } + + ss.on('exit', (code) => { + isTerminate = true; + }) + ss.postMessage("123"); + while (!flag1 || !flag2) { + await promiseCase(); + } + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res1).assertEqual("hello worker") + expect(res2).assertEqual("hello worker") + done(); + }) + + // Check the listener of worker. + /** + * @tc.name: worker_listener_test_006 + * @tc.desc: Check the listener of worker is ok. + */ + it('worker_listener_test_006', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_026.js"); + let res = 0; + let isTerminate = false; + + ss.on('message', (event) =>{ + res += 1; + if (res == 3) { + ss.terminate(); + } + }); + ss.on('exit', (code) => { + isTerminate = true; + }) + + ss.postMessage("123"); + + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual(3) + done(); + }) }) } \ No newline at end of file diff --git a/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_024.js b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_024.js new file mode 100644 index 0000000000000000000000000000000000000000..9e4a18ef2b7f1e6cedb65df94122346431e7a870 --- /dev/null +++ b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_024.js @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const parentPort = worker.parentPort; + +parentPort.onmessage = function (d) { + let int32 = d.data; + int32[0] = 100; + int32[1] = 200; + int32[2] = 300; + int32[3] = 400; + parentPort.postMessage("success"); +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_025.js b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_025.js new file mode 100644 index 0000000000000000000000000000000000000000..69e1a7b26d54a6bd194762289d67b96c811d4415 --- /dev/null +++ b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_025.js @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const parentPort = worker.parentPort; + +parentPort.addEventListener("message", (event) => { + parentPort.postMessage("hello worker") +}) \ No newline at end of file diff --git a/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_026.js b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_026.js new file mode 100644 index 0000000000000000000000000000000000000000..be1fbf1d9171f80189d10801f75db469b177e2ca --- /dev/null +++ b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_026.js @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const parentPort = worker.parentPort; + +parentPort.addEventListener("addEventTest", (event) => { + parentPort.postMessage("123") +}) + +parentPort.addEventListener("onceTest", (event) => { + parentPort.postMessage("456") +}) + +parentPort.addEventListener("message", (event) => { + parentPort.dispatchEvent({type:"addEventTest", timeStamp:0}) + parentPort.dispatchEvent({type:"addEventTest", timeStamp:0}) + parentPort.removeEventListener("addEventTest"); + parentPort.dispatchEvent({type:"addEventTest", timeStamp:0}) + parentPort.dispatchEvent({type:"onceTest", timeStamp:0}) + parentPort.removeEventListener("onceTest") +}) \ No newline at end of file