未验证 提交 7f5670e8 编写于 作者: O openharmony_ci 提交者: Gitee

!7760 新增Worker模块支持传输消息类型的测试用例

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