未验证 提交 9491bb09 编写于 作者: O openharmony_ci 提交者: Gitee

!9039 worker and taskpool add worker terminate xts Test case

Merge pull request !9039 from coollixin/master
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
*/ */
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'
import taskpool from '@ohos.taskpool' import taskpool from '@ohos.taskpool'
import worker from '@ohos.worker'
export default function TaskPoolTest() { export default function TaskPoolTest() {
describe('ActsAbilityTest', function () { describe('ActsAbilityTest', function () {
// Defines a test suite. Two parameters are supported: test suite name and test suite function. // Defines a test suite. Two parameters are supported: test suite name and test suite function.
...@@ -1516,5 +1517,78 @@ describe('ActsAbilityTest', function () { ...@@ -1516,5 +1517,78 @@ describe('ActsAbilityTest', function () {
} }
done(); 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
/*
* 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
...@@ -126,24 +126,24 @@ describe('threadWorkerTest', function () { ...@@ -126,24 +126,24 @@ describe('threadWorkerTest', function () {
*/ */
it('threadWorker_constructor_test_005', 0, async function (done) { it('threadWorker_constructor_test_005', 0, async function (done) {
var ss = [] var ss = []
let flag = 0
try { try {
let a = 0 let a = 0
while (a <= 8) { while (a <= 8) {
ss[a] = new worker.ThreadWorker("entry/ets/workers/newworker.js") ss[a] = new worker.ThreadWorker("entry/ets/workers/newworker.js")
ss[a].onexit = function() {
flag += 1
}
a += 1 a += 1
} }
} catch (error) { } catch (error) {
expect(error.name == "BusinessError").assertTrue() expect(error.name == "BusinessError").assertTrue()
let msg = "Worker initialization failure, the number of workers exceeds the maximum." let msg = "Worker initialization failure, the number of workers exceeds the maximum."
expect(error.message).assertEqual(msg) expect(error.message).assertEqual(msg)
let b = 7 let b = 0
let flag = 0 while (b < 8) {
while (b >= 0) {
ss[b].onexit = function () {
flag += 1
}
ss[b].terminate() ss[b].terminate()
b -= 1 b += 1
} }
while (flag != 8) { while (flag != 8) {
await promiseCase() await promiseCase()
...@@ -592,6 +592,30 @@ describe('threadWorkerTest', function () { ...@@ -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 // check worker on function is ok
/** /**
* @tc.name: threadWorker_on_test_001 * @tc.name: threadWorker_on_test_001
......
...@@ -464,6 +464,29 @@ describe('WorkerTest', function () { ...@@ -464,6 +464,29 @@ describe('WorkerTest', function () {
done() 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 // check worker on function is ok
/** /**
* @tc.name: worker_on_test_001 * @tc.name: worker_on_test_001
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册