From 07e3e8d5c01c036a87c5d5ea6b7a44cc991c29a6 Mon Sep 17 00:00:00 2001 From: yqhan Date: Mon, 27 Feb 2023 18:47:08 +0800 Subject: [PATCH] Add XTS of worker module. issue: https://gitee.com/openharmony/xts_acts/issues/I6I69J Signed-off-by: yqhan --- .../src/main/ets/test/ThreadWorker.test.js | 292 ++++++++++++++++++ .../src/main/ets/workers/newworker_016.js | 23 ++ .../src/main/ets/workers/newworker_017.js | 29 ++ .../src/main/ets/workers/newworker_018.js | 24 ++ .../src/main/ets/workers/newworker_019.js | 23 ++ .../src/main/ets/workers/newworker_020.js | 23 ++ .../src/main/ets/workers/newworker_021.js | 24 ++ .../src/main/ets/test/WorkerTest.test.js | 290 +++++++++++++++++ .../entry/src/main/ets/workers/worker_016.js} | 6 +- .../entry/src/main/ets/workers/worker_017.js | 29 ++ .../entry/src/main/ets/workers/worker_018.js | 24 ++ .../entry/src/main/ets/workers/worker_019.js | 23 ++ .../entry/src/main/ets/workers/worker_020.js | 23 ++ .../entry/src/main/ets/workers/worker_021.js | 24 ++ 14 files changed, 854 insertions(+), 3 deletions(-) create mode 100644 commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_016.js create mode 100644 commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_017.js create mode 100644 commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_018.js create mode 100644 commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_019.js create mode 100644 commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_020.js create mode 100644 commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_021.js rename commonlibrary/ets_utils/{threadWorker_lib_standard/entry/src/main/ets/workers/worker_002.js => worker_lib_standard/entry/src/main/ets/workers/worker_016.js} (84%) create mode 100644 commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_017.js create mode 100644 commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_018.js create mode 100644 commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_019.js create mode 100644 commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_020.js create mode 100644 commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_021.js 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 c5e8da9c5..325d74e06 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 @@ -209,6 +209,42 @@ describe('threadWorkerTest', function () { } }) + /** + * @tc.name: threadWorker_constructor_test_010 + * @tc.desc: worker constructor to Creates a worker instance. + */ + it('threadWorker_constructor_test_010', 0, async function (done) { + let ss = new worker.ThreadWorker("/entry/ets/workers/newworker.js"); + let isTerminate = false + ss.onexit = function () { + isTerminate = true + } + expect(ss != null).assertTrue() + ss.terminate() + while (!isTerminate) { + await promiseCase() + } + done() + }) + + /** + * @tc.name: threadWorker_constructor_test_011 + * @tc.desc: worker constructor to Creates a worker instance. + */ + it('threadWorker_constructor_test_011', 0, async function (done) { + let ss = new worker.ThreadWorker("@bundle:com.example.threadWorkertest/entry/ets/workers/newworker.js"); + let isTerminate = false + ss.onexit = function () { + isTerminate = true + } + expect(ss != null).assertTrue() + ss.terminate() + while (!isTerminate) { + await promiseCase() + } + done() + }) + // check postMessage is ok // main post "hello world", will receive "hello world worker" /** @@ -2018,5 +2054,261 @@ describe('threadWorkerTest', function () { expect(res).assertEqual("terminate") done() }) + + function CreateArray(type,b) { + switch(type) { + case 0 : + return new Int8Array(b); + case 1 : + return new Int16Array(b); + case 2 : + return new Int32Array(b); + case 3 : + return new Uint8Array(b); + case 4 : + return new Uint16Array(b); + case 5 : + return new Uint32Array(b); + case 6 : + return new Float32Array(b); + case 7 : + return new Float64Array(b); + case 8 : + return new BigInt64Array(b); + case 9 : + return new BigUint64Array(b); + default : + return; + } + } + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: threadWorker_support_types_test_001 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('threadWorker_support_types_test_001', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_016.js"); + let array = [] + for (let i = 0; i < 10; i++) { + array[i] = CreateArray(i, 62); + } + + let res = 0; + let flag = 0; + let isTerminate = false; + ss.onmessage = function(d) { + res += d.data; + flag += 1; + } + ss.onexit = function() { + isTerminate = true; + } + + for (let i = 0; i < 10; i++) { + ss.postMessage(array[i]); + } + + while (flag != 10) { + await promiseCase(); + } + + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual(620) + + done(); + }) + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: threadWorker_support_types_test_002 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('threadWorker_support_types_test_002', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_017.js"); + let res; + let flag = false; + let isTerminate = false; + ss.onmessage = function(d) { + res = d.data.name; + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + ss.postMessage("start"); + while (!flag) { + await promiseCase(); + } + + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual("worker") + + done(); + }) + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: threadWorker_support_types_test_003 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('threadWorker_support_types_test_003', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_018.js"); + let res; + let flag = false; + let isTerminate = false; + ss.onmessage = function(d) { + res = d.data; + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + const data = new Set(); + ss.postMessage(data); + while (!flag) { + await promiseCase(); + } + + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res.has(1)).assertTrue() + expect(res.has(2)).assertTrue() + expect(!res.has(1010)).assertTrue() + done(); + }) + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: threadWorker_support_types_test_004 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('threadWorker_support_types_test_004', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_019.js"); + let res; + let flag = false; + let isTerminate = false; + ss.onmessage = function(d) { + res = d.data; + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + const data = new Map(); + ss.postMessage(data); + while (!flag) { + await promiseCase(); + } + + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res.get("worker")).assertEqual("success"); + done(); + }) + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: threadWorker_support_types_test_005 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('threadWorker_support_types_test_005', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_020.js"); + let res; + let flag = false; + let isTerminate = false; + ss.onmessage = function(d) { + res = d.data; + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + const buffer = new ArrayBuffer(8); + ss.postMessage(buffer); + while (!flag) { + await promiseCase(); + } + + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res[res.length - 1]).assertEqual(3); + done(); + }) + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: threadWorker_support_types_test_006 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('threadWorker_support_types_test_006', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_021.js"); + let res; + let flag = false; + let isTerminate = false; + ss.onmessage = function(d) { + res = d.data; + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + var re = new RegExp("(\\w+)\\s(\\w+)"); + ss.postMessage(re); + while (!flag) { + await promiseCase(); + } + + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual("Worker, HI!"); + done(); + }) + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: threadWorker_support_types_test_007 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('threadWorker_support_types_test_007', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_021.js"); + let flag = false; + let isTerminate = false; + ss.onmessageerror = function(d) { + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + try { + const data = Symbol(); + ss.postMessage(data); + } catch (error) { + console.info("worker:: recv error message: " + error.message) + while (!flag) { + await promiseCase(); + } + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + } + expect(flag).assertTrue(); + done(); + }) }) } \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_016.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_016.js new file mode 100644 index 000000000..074986206 --- /dev/null +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_016.js @@ -0,0 +1,23 @@ +/* + * 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.workerPort; + +parentPort.onmessage = function(e) { + console.info("worker:: worker receive data " + e.data); + let data = e.data; + parentPort.postMessage(data.length); +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_017.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_017.js new file mode 100644 index 000000000..1cca8e9f1 --- /dev/null +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_017.js @@ -0,0 +1,29 @@ +/* + * 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; + +class MyModel { + name = "worker" + Init() { + this.name = "MyModel" + } +} + +workerPort.onmessage = function(e) { + let obj = new MyModel(); + workerPort.postMessage(obj); +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_018.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_018.js new file mode 100644 index 000000000..362592a30 --- /dev/null +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_018.js @@ -0,0 +1,24 @@ +/* + * 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(e) { + const data = e.data; + data.add(1); + data.add(2); + workerPort.postMessage(data); +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_019.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_019.js new file mode 100644 index 000000000..877efeec2 --- /dev/null +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_019.js @@ -0,0 +1,23 @@ +/* + * 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(e) { + const data = e.data; + data.set("worker", "success"); + workerPort.postMessage(data); +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_020.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_020.js new file mode 100644 index 000000000..db984c6eb --- /dev/null +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_020.js @@ -0,0 +1,23 @@ +/* + * 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(e) { + const data = e.data; + const view = new Int8Array(data).fill(3); + workerPort.postMessage(view); +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_021.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_021.js new file mode 100644 index 000000000..5c92783aa --- /dev/null +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_021.js @@ -0,0 +1,24 @@ +/* + * 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(e) { + const data = e.data; + let str = "HI Worker"; + let newStr = str.replace(data, "$2, $1!"); + workerPort.postMessage(newStr); +} \ 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 bba30e377..3c10b3bef 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 @@ -120,6 +120,42 @@ describe('WorkerTest', function () { done() }) + /** + * @tc.name: worker_constructor_test_005 + * @tc.desc: worker constructor to Creates a worker instance. + */ + it('worker_constructor_test_005', 0, async function (done) { + let ss = new worker.Worker("/entry/ets/workers/worker.js"); + let isTerminate = false + ss.onexit = function () { + isTerminate = true + } + expect(ss != null).assertTrue() + ss.terminate() + while (!isTerminate) { + await promiseCase() + } + done() + }) + + /** + * @tc.name: worker_constructor_test_006 + * @tc.desc: worker constructor to Creates a worker instance. + */ + it('worker_constructor_test_006', 0, async function (done) { + let ss = new worker.Worker("@bundle:com.example.workertest/entry/ets/workers/worker.js"); + let isTerminate = false + ss.onexit = function () { + isTerminate = true + } + expect(ss != null).assertTrue() + ss.terminate() + while (!isTerminate) { + await promiseCase() + } + done() + }) + // check postMessage is ok // main post "hello world", will receive "hello world worker" /** @@ -1351,5 +1387,259 @@ describe('WorkerTest', function () { expect(res).assertEqual("terminate") done() }) + + function CreateArray(type,b) { + switch(type) { + case 0 : + return new Int8Array(b); + case 1 : + return new Int16Array(b); + case 2 : + return new Int32Array(b); + case 3 : + return new Uint8Array(b); + case 4 : + return new Uint16Array(b); + case 5 : + return new Uint32Array(b); + case 6 : + return new Float32Array(b); + case 7 : + return new Float64Array(b); + case 8 : + return new BigInt64Array(b); + case 9 : + return new BigUint64Array(b); + default : + return; + } + } + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: worker_support_types_test_001 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('worker_support_types_test_001', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_016.js"); + let array = [] + for (let i = 0; i < 10; i++) { + array[i] = CreateArray(i, 62); + } + + let res = 0; + let flag = 0; + let isTerminate = false; + ss.onmessage = function(d) { + res += d.data; + flag += 1; + } + ss.onexit = function() { + isTerminate = true; + } + + for (let i = 0; i < 10; i++) { + ss.postMessage(array[i]); + } + + while (flag != 10) { + await promiseCase(); + } + + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual(620) + + done(); + }) + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: worker_support_types_test_002 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('worker_support_types_test_002', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_017.js"); + let res; + let flag = false; + let isTerminate = false; + ss.onmessage = function(d) { + res = d.data.name; + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + ss.postMessage("start"); + while (!flag) { + await promiseCase(); + } + + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual("worker") + + done(); + }) + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: worker_support_types_test_003 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('worker_support_types_test_003', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_018.js"); + let res; + let flag = false; + let isTerminate = false; + ss.onmessage = function(d) { + res = d.data; + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + const data = new Set(); + ss.postMessage(data); + while (!flag) { + await promiseCase(); + } + + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res.has(1)).assertTrue() + expect(res.has(2)).assertTrue() + expect(!res.has(1010)).assertTrue() + done(); + }) + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: worker_support_types_test_004 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('worker_support_types_test_004', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_019.js"); + let res; + let flag = false; + let isTerminate = false; + ss.onmessage = function(d) { + res = d.data; + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + const data = new Map(); + ss.postMessage(data); + while (!flag) { + await promiseCase(); + } + + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res.get("worker")).assertEqual("success"); + done(); + }) + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: worker_support_types_test_005 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('worker_support_types_test_005', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_020.js"); + let res; + let flag = false; + let isTerminate = false; + ss.onmessage = function(d) { + res = d.data; + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + const buffer = new ArrayBuffer(8); + ss.postMessage(buffer); + while (!flag) { + await promiseCase(); + } + + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res[res.length - 1]).assertEqual(3); + done(); + }) + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: worker_support_types_test_006 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('worker_support_types_test_006', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_021.js"); + let res; + let flag = false; + let isTerminate = false; + ss.onmessage = function(d) { + res = d.data; + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + var re = new RegExp("(\\w+)\\s(\\w+)"); + ss.postMessage(re); + while (!flag) { + await promiseCase(); + } + + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual("Worker, HI!"); + done(); + }) + + // Check the transmission types supported by Worker is ok. + /** + * @tc.name: worker_support_types_test_007 + * @tc.desc: Check the transmission types supported by Worker is ok. + */ + it('worker_support_types_test_007', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_021.js"); + let flag = false; + let isTerminate = false; + ss.onmessageerror = function(d) { + console.info("worker:: unsupport the Symbol."); + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + const data = Symbol(); + ss.postMessage(data); + while (!flag) { + await promiseCase(); + } + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + + expect(flag).assertTrue(); + done(); + }) }) } \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/worker_002.js b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_016.js similarity index 84% rename from commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/worker_002.js rename to commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_016.js index ab62c6d1f..7f037e29b 100644 --- a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/worker_002.js +++ b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_016.js @@ -17,7 +17,7 @@ import worker from '@ohos.worker'; const parentPort = worker.parentPort; parentPort.onmessage = function(e) { - console.log("worker:: worker receive data " + e.data); - let data = e.data + " worker"; - parentPort.postMessage(data) + console.info("worker:: worker receive data " + e.data); + let data = e.data; + parentPort.postMessage(data.length); } \ No newline at end of file diff --git a/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_017.js b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_017.js new file mode 100644 index 000000000..63017b9c2 --- /dev/null +++ b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_017.js @@ -0,0 +1,29 @@ +/* + * 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; + +class MyModel { + name = "worker" + Init() { + this.name = "MyModel" + } +} + +parentPort.onmessage = function(e) { + let obj = new MyModel(); + parentPort.postMessage(obj); +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_018.js b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_018.js new file mode 100644 index 000000000..0a2193f46 --- /dev/null +++ b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_018.js @@ -0,0 +1,24 @@ +/* + * 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(e) { + const data = e.data; + data.add(1); + data.add(2); + parentPort.postMessage(data); +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_019.js b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_019.js new file mode 100644 index 000000000..719fe02ae --- /dev/null +++ b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_019.js @@ -0,0 +1,23 @@ +/* + * 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(e) { + const data = e.data; + data.set("worker", "success"); + parentPort.postMessage(data); +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_020.js b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_020.js new file mode 100644 index 000000000..2b2c581aa --- /dev/null +++ b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_020.js @@ -0,0 +1,23 @@ +/* + * 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(e) { + const data = e.data; + const view = new Int8Array(data).fill(3); + parentPort.postMessage(view); +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_021.js b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_021.js new file mode 100644 index 000000000..779e57ead --- /dev/null +++ b/commonlibrary/ets_utils/worker_lib_standard/entry/src/main/ets/workers/worker_021.js @@ -0,0 +1,24 @@ +/* + * 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(e) { + const data = e.data; + let str = "HI Worker"; + let newStr = str.replace(data, "$2, $1!"); + parentPort.postMessage(newStr); +} \ No newline at end of file -- GitLab