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 325d74e06154c973a1d153231e147d87275854af..c88b0abc86e8492aeeddcbc59689d952946ae0a8 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 @@ -459,20 +459,52 @@ describe('threadWorkerTest', function () { done() }) - // check worker handle error is ok + // check worker handle error is ok /** * @tc.name: threadWorker_postMessage_test_007 * @tc.desc: Sends a message to the worker thread when throw error. */ it('threadWorker_postMessage_test_007', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_007.js"); + let isTerminate = false; + ss.onexit = function () { + isTerminate = true; + } try { - const ss = new worker.ThreadWorker("entry/ets/workers/newworker_007.js") - ss.postMessage() + ss.postMessage(); } catch (error) { - expect(error.name).assertEqual("BusinessError") - expect(error.message).assertEqual("Worker messageObject must be not null with postMessage") + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(error.name).assertEqual("BusinessError"); + expect(error.message).assertEqual("Worker messageObject must be not null with postMessage"); } - done() + done(); + }) + + // check worker handle error is ok + /** + * @tc.name: threadWorker_postMessage_test_008 + * @tc.desc: Sends a message to the worker thread when throw error. + */ + it('threadWorker_postMessage_test_008', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_007.js"); + let isTerminate = false; + ss.onexit = function () { + isTerminate = true; + } + try { + ss.postMessage("123", "123"); + } catch (error) { + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(error.name).assertEqual("BusinessError"); + expect(error.message).assertEqual("transfer list must be an Array"); + } + done(); }) // check worker terminate is ok @@ -2298,7 +2330,7 @@ describe('threadWorkerTest', function () { const data = Symbol(); ss.postMessage(data); } catch (error) { - console.info("worker:: recv error message: " + error.message) + console.info("worker:: recv error message: " + error.message); while (!flag) { await promiseCase(); } @@ -2310,5 +2342,115 @@ describe('threadWorkerTest', function () { expect(flag).assertTrue(); done(); }) + + // Check the postmessage of worker is ok. + /** + * @tc.name: threadWorker_worker_postmessage_test_001 + * @tc.desc: Check the postmessage of worker is ok. + */ + it('threadWorker_worker_postmessage_test_001', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_022.js"); + let res = ""; + let flag = false; + let isTerminate = false; + ss.onmessage = function (d) { + res = d.data; + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + ss.postMessage("1"); + while (!flag) { + await promiseCase(); + } + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual("Worker param count must be more than 1 with new"); + done(); + }) + + + // Check the postmessage of worker is ok. + /** + * @tc.name: threadWorker_worker_postmessage_test_002 + * @tc.desc: Check the postmessage of worker is ok. + */ + it('threadWorker_worker_postmessage_test_002', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_023.js"); + let res = ""; + let flag = false; + let isTerminate = false; + ss.onmessage = function (d) { + res = d.data; + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + ss.postMessage("1"); + while (!flag) { + await promiseCase(); + } + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual("Transfer list must be an Array"); + done(); + }) + + // Check the postmessage of worker is ok. + /** + * @tc.name: threadWorker_worker_postmessage_test_003 + * @tc.desc: Check the postmessage of worker is ok. + */ + it('threadWorker_worker_postmessage_test_003', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_024.js"); + let res = ""; + let flag = false; + let isTerminate = false; + ss.onmessage = function (d) { + res = d.data; + flag = true; + } + ss.onexit = function() { + isTerminate = true; + } + ss.postMessage("1") + while (!flag) { + await promiseCase(); + } + ss.terminate(); + while (!isTerminate) { + await promiseCase(); + } + expect(res).assertEqual("Serializing an uncaught exception failed, failed to serialize message."); + done(); + }) + + // Check the close of worker is ok. + /** + * @tc.name: threadWorker_worker_close_test_001 + * @tc.desc: Check the close of worker is ok. + */ + it('threadWorker_worker_close_test_001', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_025.js"); + let res = 0; + let isTerminate = false; + ss.onexit = function() { + res += 1 + isTerminate = true; + } + ss.postMessage("1"); + while (!isTerminate) { + await promiseCase(); + } + + expect(res == 1).assertTrue(); + done(); + }) }) } \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker.js index 2b681336776eebc089730439a0ffea7bff73df21..273f39f1e8cfcc4b079d6c1c6d63a1a997f34747 100644 --- a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker.js +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker.js @@ -16,11 +16,7 @@ import worker from '@ohos.worker'; const parentPort = worker.workerPort; -console.info("worker:: new version") - -parentPort.onclose = function() { - console.info("worker::worker.js onclose"); -} +console.info("worker:: new version"); parentPort.onmessage = function(e) { let data = e.data; diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_022.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_022.js new file mode 100644 index 0000000000000000000000000000000000000000..98e130d4a2704870f4aba58b0fa4ef0014669f8a --- /dev/null +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_022.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(e) { + try { + workerPort.postMessage(); + } catch (e) { + let data = e.message; + workerPort.postMessage(data); + } +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_023.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_023.js new file mode 100644 index 0000000000000000000000000000000000000000..b36ebc2f0f9386448c0f16e9a629ae7421f34c1e --- /dev/null +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_023.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(e) { + try { + workerPort.postMessage("123", "123"); + } catch (e) { + let data = e.message; + workerPort.postMessage(data); + } +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_024.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_024.js new file mode 100644 index 0000000000000000000000000000000000000000..8c42d9ef4d4a802561c89fc87cfc9277b739a49f --- /dev/null +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_024.js @@ -0,0 +1,27 @@ +/* + * 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) { + try { + const symbol = Symbol(); + workerPort.postMessage(symbol); + } catch (e) { + let data = e.message; + workerPort.postMessage(data); + } +} \ No newline at end of file diff --git a/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_025.js b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_025.js new file mode 100644 index 0000000000000000000000000000000000000000..7c581a693f73920d8a40f807c6005859559301cf --- /dev/null +++ b/commonlibrary/ets_utils/threadWorker_lib_standard/entry/src/main/ets/workers/newworker_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 workerPort = worker.workerPort; + +workerPort.onmessage = function(e) { + workerPort.close(); +} \ No newline at end of file