From 4cb03b5767f756b783499db65f77e80bdb5c7a9f Mon Sep 17 00:00:00 2001 From: coollixin Date: Tue, 20 Jun 2023 16:01:34 +0800 Subject: [PATCH] worker add worker terminate xts Test case issue:https://gitee.com/openharmony/xts_acts/issues/I7EHG4 Signed-off-by: coollixin --- .../entry/src/main/ets/test/TaskPool.test.js | 74 +++++++++++++++++++ .../entry/src/main/ets/worker/worker.js | 20 +++++ .../src/main/ets/test/ThreadWorker.test.js | 38 ++++++++-- .../src/main/ets/test/WorkerTest.test.js | 23 ++++++ 4 files changed, 148 insertions(+), 7 deletions(-) create mode 100644 commonlibrary/ets_utils/taskpool_lib_standard/entry/src/main/ets/worker/worker.js 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 a952ff18c..cecde0726 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 @@ -14,6 +14,7 @@ */ import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' import taskpool from '@ohos.taskpool' +import worker from '@ohos.worker' export default function TaskPoolTest() { describe('ActsAbilityTest', function () { // Defines a test suite. Two parameters are supported: test suite name and test suite function. @@ -1516,5 +1517,78 @@ describe('ActsAbilityTest', function () { } done(); }) + + /** + * @tc.number : TaskPoolTestClass061 + * @tc.name : Sync Function Cancel task + * @tc.desc : Test Simultaneous use taskpool and worker + * @tc.size : MEDIUM + * @tc.type : Function + * @tc.level : Level 0 + */ + it('TaskPoolTestClass061', 0, async function (done) { + function testTaskPool() { + function addition(arg) { + "use concurrent" + return arg + 1; + } + function additionDelay(arg) { + "use concurrent" + var start = new Date().getTime(); + while (new Date().getTime() - start < 3000) { + continue; + } + return arg + 1; + } + + try { + var task1 = new taskpool.Task(additionDelay, 100); + var task2 = new taskpool.Task(additionDelay, 200); + var task3 = new taskpool.Task(addition, 300); + + taskpool.execute(task1) + taskpool.execute(task2) + taskpool.execute(task3) + + var start = new Date().getTime(); + while (new Date().getTime() - start < 1000) { + continue; + } + for (let i = 1; i <= 10; i++) { + taskpool.cancel(task1); + } + } + catch (e) { + console.info("taskpoolXTS061 catch error: " + e); + } + } + function promiseCase() { + let p = new Promise(function (resolve, reject) { + setTimeout(function () { + resolve(0) + }, 100) + }).then(undefined, (error) => { + }) + return p + } + + let ss = new worker.ThreadWorker("entry/ets/workers/worker.js") + let res = 0 + let flag = false + ss.onexit = function () { + flag = true + res++ + } + testTaskPool(); + for (let i = 0; i < 10; i++) { + ss.terminate(); + } + while (!flag) { + await promiseCase() + } + expect(res).assertEqual(1) + done(); + }) + }) } \ No newline at end of file diff --git a/commonlibrary/ets_utils/taskpool_lib_standard/entry/src/main/ets/worker/worker.js b/commonlibrary/ets_utils/taskpool_lib_standard/entry/src/main/ets/worker/worker.js new file mode 100644 index 000000000..86f38d3e5 --- /dev/null +++ b/commonlibrary/ets_utils/taskpool_lib_standard/entry/src/main/ets/worker/worker.js @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2023 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) { +} \ 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 f55dca240..789181c3a 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 @@ -126,24 +126,24 @@ describe('threadWorkerTest', function () { */ it('threadWorker_constructor_test_005', 0, async function (done) { var ss = [] + let flag = 0 try { let a = 0 while (a <= 8) { ss[a] = new worker.ThreadWorker("entry/ets/workers/newworker.js") + ss[a].onexit = function() { + flag += 1 + } a += 1 } } catch (error) { expect(error.name == "BusinessError").assertTrue() let msg = "Worker initialization failure, the number of workers exceeds the maximum." expect(error.message).assertEqual(msg) - let b = 7 - let flag = 0 - while (b >= 0) { - ss[b].onexit = function () { - flag += 1 - } + let b = 0 + while (b < 8) { ss[b].terminate() - b -= 1 + b += 1 } while (flag != 8) { await promiseCase() @@ -592,6 +592,30 @@ describe('threadWorkerTest', function () { } }) + // check worker terminate is ok + /** + * @tc.name: threadWorker_terminate_test_004 + * @tc.desc: Terminates the worker thread to stop the worker from receiving messages. + */ + it('threadWorker_terminate_test_004', 0, async function (done) { + let ss = new worker.ThreadWorker("entry/ets/workers/newworker_002.js") + let res = 0 + let flag = false + + ss.onexit = function () { + flag = true + res++ + } + for (let i = 0; i < 10; i++) { + ss.terminate(); + } + while (!flag) { + await promiseCase() + } + expect(res).assertEqual(1) + done() + }) + // check worker on function is ok /** * @tc.name: threadWorker_on_test_001 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 6708070ab..8a91255a5 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 @@ -464,6 +464,29 @@ describe('WorkerTest', function () { done() }) + // check worker terminate is ok + /** + * @tc.name: worker_terminate_test_004 + * @tc.desc: Terminates the worker thread to stop the worker from receiving messages. + * @tc.author: hanyuqing + */ + it('worker_terminate_test_004', 0, async function (done) { + let ss = new worker.Worker("entry/ets/workers/worker_002.js") + let res = 0 + let flag = false + ss.onexit = function () { + flag = true + res++ + } + for (let i = 0; i < 10; i++) { + ss.terminate(); + } + while (!flag) { + await promiseCase() + } + expect(res).assertEqual(1) + done() + }) // check worker on function is ok /** * @tc.name: worker_on_test_001 -- GitLab