未验证 提交 7699d90d 编写于 作者: O openharmony_ci 提交者: Gitee

!9772 Worker的基于listener机制添加测试用例

Merge pull request !9772 from yuqing_han/master
......@@ -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
......@@ -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
/*
* 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
/*
* 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
/*
* 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
......@@ -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
/*
* 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
/*
* 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
/*
* 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册