提交 bc656b59 编写于 作者: Y yqhan

Add XTS of worker interface exception information

issue: https://gitee.com/openharmony/interface_sdk-js/issues/I5PTU7Signed-off-by: Nyqhan <hanyuqing2@huawei.com>
上级 cde7709e
/*
* 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;
console.info("worker:: new version")
parentPort.onclose = function() {
console.info("worker::worker.js onclose");
}
parentPort.onmessage = function(e) {
let data = e.data;
console.info("worker:: worker thread worker data is " + data.data);
switch(data.type) {
case "normal":
console.info("worker:: worker thread receive data " + data.data);
parentPort.postMessage(data);
console.info("worker:: worker thread post back");
break;
case "error":
throw new Error("123");
case "buffer":
console.info("worker:: worker.js receive buffer length is " + data.data.byteLength);
parentPort.postMessage(data, [data.data]);
console.info("worker:: worker.js post buffer length is " + data.data.byteLength);
break;
default:
console.info("worker:: worker.js receive unknow type");
break
}
}
// Deserialization error
parentPort.onmessageerror = function() {
console.info("worker:: worker.js onmessageerror");
}
// js execution error
parentPort.onerror = function(data) {
console.info("worker:: worker.js onerror " + data.lineno + ", msg = " + data.message + ", filename = " + data.filename + ", colno = " + data.colno);
}
\ 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.log("worker:: worker receive data " + e.data);
let data = e.data + " worker";
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.workerPort;
parentPort.onmessage = function(e) {
let data = e.data * 2 + 1
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.workerPort;
parentPort.onmessage = function(e) {
let data = e.data;
data.message = "hello world " + "worker"
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.workerPort;
parentPort.onmessage = function(e) {
parentPort.postMessage(name)
}
\ 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) {
parentPort.postMessage("hello world")
}
\ 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) {
throw new Error(123)
}
\ 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) {
parentPort.close()
}
/*
* 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;
var ss = undefined;
var flag = false;
parentPort.onmessage = function(e) {
let data = e.data;
switch(data.type) {
case "new":
ss = new worker.ThreadWorker("workers/newworker_0091.js");
console.log("worker:: workerxx ");
ss.onexit = function() {
flag = true;
}
parentPort.postMessage(ss != null);
break;
case "wait":
if (flag) {
parentPort.postMessage("terminate");
}
break;
case "terminate":
flag = false;
ss.terminate();
break;
default:
break;
}
}
\ 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.log("worker:: onmessage");
}
\ 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;
var ss = undefined;
var backValue = undefined;
var flag = false;
parentPort.onmessage = function(e) {
let data = e.data;
switch(data.type) {
case "new":
ss = new worker.ThreadWorker("workers/newworker_0101.js");
ss.onmessage = function(ee) {
backValue = ee.data;
flag = true;
}
ss.onexit = function() {
backValue = "terminate";
flag = true;
}
ss.postMessage(data.value);
break;
case "wait":
if (flag) {
parentPort.postMessage(backValue);
}
break;
case "terminate":
flag = false;
ss.terminate();
break;
default:
break;
}
}
\ 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) {
let data = e.data; // 12
parentPort.postMessage(data * 2 + 1); // 25
}
\ 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;
var ss = undefined;
var backValue = undefined;
var flag = false;
parentPort.onmessage = function(e) {
let data = e.data;
switch(data.type) {
case "new":
ss = new worker.ThreadWorker("workers/newworker_0111.js");
ss.onmessage = function(ee) {
backValue = ee.data;
flag = true;
}
ss.onexit = function(code) {
backValue = "terminate";
flag = true;
}
ss.postMessage(data.value);
break;
case "wait":
if (flag) {
parentPort.postMessage(backValue);
}
break;
case "terminate":
flag = false;
ss.terminate();
break;
default:
break;
}
}
\ 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.log("worker:: onmessage")
parentPort.postMessage("terminate");
}
\ 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;
var ss = undefined;
var backValue = undefined;
var flag = false;
parentPort.onmessage = function(e) {
let data = e.data;
switch(data.type) {
case "new":
ss = new worker.ThreadWorker("workers/newworker_0121.js");
ss.onmessage = function(ee) {
backValue = ee.data;
flag = true;
}
ss.onexit = function() {
backValue = "terminate";
flag = true;
}
ss.postMessage({type: "new"});
break;
case "wait":
if (flag) {
parentPort.postMessage(backValue);
}
break;
case "terminate":
flag = false;
ss.terminate();
break;
default:
break;
}
}
\ 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;
var ss = undefined;
var backValue = undefined;
var flag = false;
parentPort.onmessage = function(e) {
let data = e.data;
switch(data.type) {
case "new":
ss = new worker.ThreadWorker("workers/newworker_0122.js");
ss.terminate();
ss.onexit = function() {
parentPort.postMessage(ss != null);
}
break;
default:
break;
}
}
\ 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;
var ss = undefined;
var backValue = undefined;
var flag = false;
parentPort.onmessage = function(e) {
console.info("worker:: thrid worker onmessage")
}
\ 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;
var ss = undefined;
var backValue = undefined;
var flag = false;
parentPort.onmessage = function(e) {
var data = e.data;
switch(data.type) {
case "new":
ss = new worker.ThreadWorker("workers/newworker_0131.js");
ss.onmessage = function(ee) {
backValue = ee.data;
flag = true;
}
ss.onexit = function() {
backValue = "terminate";
flag = true;
}
ss.postMessage(data.value);
break;
case "wait":
if (flag) {
parentPort.postMessage(backValue);
}
break;
case "terminate":
flag = false;
ss.terminate();
break;
default:
break;
}
}
\ 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) {
let data = e.data; // hello world
parentPort.postMessage(data + " worker"); // hello world 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.workerPort;
var ss = undefined;
var backValue = undefined;
var flag = false;
parentPort.onmessage = function(e) {
let data = e.data;
switch(data.type) {
case "new":
ss = new worker.ThreadWorker("workers/newworker_0141.js");
ss.onmessage = function(ee) {
backValue = ee.data;
flag = true;
}
ss.onexit = function() {
backValue = "terminate";
flag = true;
}
ss.postMessage(data.value);
break;
case "wait":
if (flag) {
parentPort.postMessage(backValue);
}
break;
case "terminate":
flag = false;
ss.terminate();
break;
default:
break;
}
}
\ 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) {
let data = e.data;
data[0] += 1
parentPort.postMessage(data); // [2, 2]
}
\ 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;
var ss = undefined;
var backValue = undefined;
var flag = false;
parentPort.onmessage = function(e) {
var data = e.data;
switch(data.type) {
case "new":
ss = new worker.ThreadWorker("workers/newworker_0151.js");
ss.onexit = function() {
parentPort.postMessage(backValue); // 16
}
ss.onmessage = function(ee) {
backValue = ee.data; // 16
flag = true;
}
ss.postMessage({type: "new", value: data.value + 1}); // 11
break;
case "wait":
ss.postMessage({type: "wait"});
if (flag) {
ss.terminate();
}
break;
default:
break;
}
}
\ 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;
var ss = undefined;
var backValue = undefined;
var flag = false;
parentPort.onmessage = function(e) {
let data = e.data;
switch(data.type) {
case "new":
ss = new worker.ThreadWorker("workers/newworker_0152.js");
ss.onexit = function() {
parentPort.postMessage(backValue); // 16
}
ss.onmessage = function(ee) {
backValue = ee.data;
flag = true;
}
ss.postMessage(data.value + 2); // 13
break;
case "wait":
if (flag) {
ss.terminate();
}
break;
default:
break;
}
}
\ 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) {
let data = e.data;
parentPort.postMessage(data + 3); // 16
}
\ No newline at end of file
......@@ -13,7 +13,9 @@
* limitations under the License.
*/
import workerTest from './Worker.test.js'
import threadWorkerTest from './ThreadWorker.test.js'
export default function testsuite() {
workerTest()
threadWorkerTest()
}
\ 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.
*/
// @ts-nocheck
import app from '@system.app'
import worker from "@ohos.worker"
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'
export default function threadWorkerTest() {
describe('threadWorkerTest', function () {
afterAll(function () {
console.info('total case over')
})
function promiseCase() {
let p = new Promise(function (resolve, reject) {
setTimeout(function () {
resolve()
}, 100)
}).then(undefined, (error) => {
})
return p
}
// check worker constructor is ok
/**
* @tc.name: threadWorker_constructor_test_001
* @tc.desc: worker constructor to Creates a worker instance.
*/
it('threadWorker_constructor_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("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_002
* @tc.desc: worker constructor to Creates a worker instance.
*/
it('threadWorker_constructor_test_002', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js", {name:"new worker", shared:"False"})
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
expect(ss != null).assertTrue()
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
})
/**
* @tc.name: threadWorker_constructor_test_003
* @tc.desc: worker constructor to Creates a worker instance.
*/
it('threadWorker_constructor_test_003', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js", {type:"classic", name:"new worker", shared:"false"})
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
expect(ss != null).assertTrue()
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
})
/**
* @tc.name: threadWorker_constructor_test_004
* @tc.desc: worker constructor to Creates a worker instance.
*/
it('threadWorker_constructor_test_004', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js", {type:"classic", name:"new worker", shared:"true"})
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
expect(ss != null).assertTrue()
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
})
/**
* @tc.name: threadWorker_constructor_test_005
* @tc.desc: worker constructor to Creates a worker instance when throw error.
*/
it('threadWorker_constructor_test_005', 0, async function (done) {
var ss = []
try {
let a = 0
while (a <= 8) {
ss[a] = new worker.ThreadWorker("workers/newworker.js")
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
}
ss[b].terminate()
b -= 1
}
while (flag != 8) {
await promiseCase()
}
done()
}
})
/**
* @tc.name: threadWorker_constructor_test_006
* @tc.desc: worker constructor to Creates a worker instance when throw error.
*/
it('threadWorker_constructor_test_006', 0, async function (done) {
try {
let ss = new worker.ThreadWorker("workers/newworker.js", {type:"other", name:"new worker", shared:"true"})
} catch (error) {
expect(error.name == "BusinessError").assertTrue()
expect(error.message == "the type must be classic, unsupport others now.").assertTrue()
done()
}
})
/**
* @tc.name: threadWorker_constructor_test_007
* @tc.desc: worker constructor to Creates a worker instance when throw error.
*/
it('threadWorker_constructor_test_007', 0, async function (done) {
try {
let ss = new worker.ThreadWorker("workers/newworker.js", {type:1, name:"new worker", shared:"true"})
} catch (error) {
expect(error.name == "BusinessError").assertTrue()
expect(error.message == "the type of type must be string.").assertTrue()
done()
}
})
/**
* @tc.name: threadWorker_constructor_test_008
* @tc.desc: worker constructor to Creates a worker instance when throw error.
*/
it('threadWorker_constructor_test_008', 0, async function (done) {
try {
let ss = new worker.ThreadWorker("workers/newworker.js", {type:"classic1", name:1, shared:"true"})
} catch (error) {
expect(error.name == "BusinessError").assertTrue()
expect(error.message == "the type of name in worker must be string.").assertTrue()
done()
}
})
/**
* @tc.name: threadWorker_constructor_test_009
* @tc.desc: worker constructor to Creates a worker instance when throw error.
*/
it('threadWorker_constructor_test_009', 0, async function (done) {
try {
let ss = new worker.ThreadWorker()
} catch (error) {
expect(error.name == "BusinessError").assertTrue()
expect(error.message == "the number of create worker param must be more than 1 with new").assertTrue()
done()
}
})
// check postMessage is ok
// main post "hello world", will receive "hello world worker"
/**
* @tc.name: threadWorker_postMessage_test_001
* @tc.desc: Sends a message to the worker thread.
*/
it('threadWorker_postMessage_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_002.js")
let res = undefined
let flag = false
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) {
res = e.data
flag = true
}
ss.postMessage("hello world")
while (!flag) {
await promiseCase()
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(res).assertEqual("hello world worker")
done()
})
// check postMessage is ok
// main post 12 , will receive 12 * 2 + 1
/**
* @tc.name: threadWorker_postMessage_test_002
* @tc.desc: Sends a message to the worker thread.
*/
it('threadWorker_postMessage_test_002', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_003.js")
let res = undefined
let flag = false
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) {
res = e.data
flag = true
}
ss.postMessage(12)
while (!flag) {
await promiseCase()
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(res).assertEqual(25)
done()
})
// check postMessage is ok
// main post {message:"hello world"} , will receive {message:"hello world worker"}
/**
* @tc.name: threadWorker_postMessage_test_003
* @tc.desc: Sends a message to the worker thread.
*/
it('threadWorker_postMessage_test_003', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_004.js")
let res = undefined
let flag = false
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) {
res = e.data.message
flag = true
}
ss.postMessage({"message":"hello world"})
while (!flag) {
await promiseCase()
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(res).assertEqual("hello world worker")
done()
})
// check worker name is ok
/**
* @tc.name: threadWorker_postMessage_test_004
* @tc.desc: Sends a message to the worker thread.
*/
it('threadWorker_postMessage_test_004', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_005.js", {name: "zhangsan"})
let res = undefined
let flag = false
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) {
res = e.data
console.info("worker:: type " + e.type)
console.info("worker:: timeStamp " + e.timeStamp)
flag = true
}
ss.postMessage("hello world")
while (!flag) {
await promiseCase()
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(res).assertEqual("zhangsan")
done()
})
// check worker transfer buffer is ok
/**
* @tc.name: threadWorker_postMessage_test_005
* @tc.desc: Sends a message to the worker thread.
*/
it('threadWorker_postMessage_test_005', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_006.js")
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
const buffer = new ArrayBuffer(8)
expect(buffer.byteLength).assertEqual(8)
ss.postMessage(buffer, [buffer])
let length = undefined
let exception = undefined
try {
length = buffer.byteLength
} catch (e) {
exception = e.message
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
console.log("worker:: length is " + length)
console.log("worker:: exception is " + exception)
if (typeof exception == "undefined") {
expect(length).assertEqual(0)
} else {
expect(exception).assertEqual("IsDetachedBuffer")
}
done()
})
// check worker handle error is ok
/**
* @tc.name: threadWorker_postMessage_test_006
* @tc.desc: Sends a message to the worker thread.
*/
it('threadWorker_postMessage_test_006', 0, async function (done) {
const ss = new worker.ThreadWorker("workers/newworker_007.js")
let res = undefined
let flag = false
let isTerminate = false
ss.onerror = function (e) {
res = e.message
console.info("worker:: filename " + e.filename)
console.info("worker:: lineno " + e.lineno)
console.info("worker:: colno " + e.colno)
console.info("worker:: error " + e.error)
flag = true
}
ss.onexit = function () {
isTerminate = true
}
ss.postMessage("hello world")
while (!flag) {
await promiseCase()
}
expect(res).assertEqual("Error: 123")
done()
})
// 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) {
try {
const ss = new worker.ThreadWorker("workers/newworker_007.js")
ss.postMessage()
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker messageObject must be not null with postMessage")
}
done()
})
// check worker terminate is ok
/**
* @tc.name: threadWorker_terminate_test_001
* @tc.desc: Terminates the worker thread to stop the worker from receiving messages.
*/
it('threadWorker_terminate_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let res = 0
let flag = false
ss.onexit = function () {
flag = true
res++
}
ss.terminate()
while (!flag) {
await promiseCase()
}
expect(res).assertEqual(1)
done()
})
// check worker terminate is ok
/**
* @tc.name: threadWorker_terminate_test_002
* @tc.desc: Terminates the worker thread to stop the worker from receiving messages.
*/
it('threadWorker_terminate_test_002', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let res = 0
let flag = false
ss.onexit = function () {
flag = true
res++
}
// Repeated execution terminate
ss.terminate()
ss.terminate()
while (!flag) {
await promiseCase()
}
expect(res).assertEqual(1)
done()
})
// check worker terminate is ok
/**
* @tc.name: threadWorker_terminate_test_003
* @tc.desc: Terminates the worker thread to stop the worker from receiving messages.
*/
it('threadWorker_terminate_test_003', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let res = 0
let flag = false
ss.onexit = function () {
flag = true
res++
}
ss.onmessage = function (e) {
res++
}
ss.terminate()
while (!flag) {
await promiseCase()
}
expect(res).assertEqual(1)
try {
ss.postMessage("hello world")
await promiseCase()
} catch (error) {
let msg = "Worker instance is not running, maybe worker is terminated when PostMessage"
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual(msg)
done()
}
})
// check worker on function is ok
/**
* @tc.name: threadWorker_on_test_001
* @tc.desc: Adds an event listener to the worker.
*/
it('threadWorker_on_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.on("zhangsan", ()=>{
times++
})
ss.dispatchEvent({type: "zhangsan"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(1)
done()
})
// check worker on function is ok
/**
* @tc.name: threadWorker_on_test_002
* @tc.desc: Adds an event listener to the worker.
*/
it('threadWorker_on_test_002', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.on("zhangsan", ()=>{
times--
})
ss.on("lisi", ()=>{
times++
})
ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "lisi"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(0)
done()
})
// check worker on function is ok
/**
* @tc.name: threadWorker_on_test_003
* @tc.desc: Adds an event listener to the worker.
*/
it('threadWorker_on_test_003', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.on("zhangsan", ()=>{
times++
})
for (let i=0;i<10;i++)
{
ss.dispatchEvent({type: "zhangsan"})
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
console.log("times " + times)
expect(times).assertEqual(10)
done()
})
// check worker on function is ok
/**
* @tc.name: threadWorker_on_test_004
* @tc.desc: Adds an event listener to the worker when throw error.
*/
it('threadWorker_on_test_004', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let isTerminate = false
try {
ss.on(1, ()=>{})
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker add listener 1st param must be string")
ss.onexit = function () {
isTerminate = true
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}
})
// check worker on function is ok
/**
* @tc.name: threadWorker_on_test_005
* @tc.desc: Adds an event listener to the worker when throw error.
*/
it('threadWorker_on_test_005', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let isTerminate = false
try {
ss.on("zhangSan", "error")
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker add listener 2st param must be callable")
ss.onexit = function () {
isTerminate = true
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}
})
// check worker on function is ok
/**
* @tc.name: threadWorker_on_test_006
* @tc.desc: Adds an event listener to the worker when throw error.
*/
it('threadWorker_on_test_006', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let isTerminate = false
try {
ss.on()
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("worker add listener param count must be not less than 2.")
ss.onexit = function () {
isTerminate = true
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}
})
/**
* @tc.name: threadWorker_on_test_007
* @tc.desc: Adds an event listener to the worker and removes the event listener automically
after it is invoked once when throw error.
*/
it('threadWorker_on_test_007', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let isTerminate = false
try {
ss.on("zhangsan", ()=>{
times++
})
ss.dispatchEvent({type: "zhangsan"})
ss.onexit = function () {
isTerminate = true
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
ss.on("lisi", ()=>{
times++
})
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker instance is not running, maybe worker is terminated")
done()
}
})
// check worker once function is ok
/**
* @tc.name: threadWorker_once_test_001
* @tc.desc: Adds an event listener to the worker and removes the event listener automically
after it is invoked once.
*/
it('threadWorker_once_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.once("zhangsan", ()=>{
times++
})
ss.dispatchEvent({type: "zhangsan"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(1)
done()
})
// check worker once function is ok
/**
* @tc.name: threadWorker_once_test_002
* @tc.desc: Adds an event listener to the worker and removes the event listener automically
after it is invoked once.
*/
it('threadWorker_once_test_002', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.once("zhangsan", ()=>{
times++
})
for (let i=0;i<10;i++)
{
ss.dispatchEvent({type: "zhangsan"})
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(1)
done()
})
/**
* @tc.name: threadWorker_once_test_003
* @tc.desc: Adds an event listener to the worker and removes the event listener automically
after it is invoked once.
*/
it('threadWorker_once_test_003', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.once("zhangsan", ()=>{
times--
})
ss.once("lisi", ()=>{
times++
})
ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "lisi"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(0)
done()
})
/**
* @tc.name: threadWorker_once_test_004
* @tc.desc: Adds an event listener to the worker and removes the event listener automically
after it is invoked once when throw error.
*/
it('threadWorker_once_test_004', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
try {
ss.once(1, ()=>{
times++
})
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker add listener 1st param must be string")
ss.onexit = function () {
isTerminate = true
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}
})
/**
* @tc.name: threadWorker_once_test_005
* @tc.desc: Adds an event listener to the worker and removes the event listener automically
after it is invoked once when throw error.
*/
it('threadWorker_once_test_005', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let isTerminate = false
try {
ss.once("zhangsan", "error")
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker add listener 2st param must be callable")
ss.onexit = function () {
isTerminate = true
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}
})
/**
* @tc.name: threadWorker_once_test_006
* @tc.desc: Adds an event listener to the worker and removes the event listener automically
after it is invoked once when throw error.
*/
it('threadWorker_once_test_006', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let isTerminate = false
try {
ss.once()
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("worker add listener param count must be not less than 2.")
ss.onexit = function () {
isTerminate = true
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}
})
/**
* @tc.name: threadWorker_once_test_007
* @tc.desc: Adds an event listener to the worker and removes the event listener automically
after it is invoked once when throw error.
*/
it('threadWorker_once_test_007', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let isTerminate = false
try {
ss.once("zhangsan", ()=>{
times++
})
ss.dispatchEvent({type: "zhangsan"})
ss.onexit = function () {
isTerminate = true
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
ss.once("lisi", ()=>{
times++
})
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker instance is not running, maybe worker is terminated")
done()
}
})
// check worker addEventListener function is ok
/**
* @tc.name: threadWorker_addEventListener_test_001
* @tc.desc: Add an event listener to the Worker.
*/
it('threadWorker_addEventListener_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{
times++
})
ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(2)
done()
})
// check worker addEventListener function is ok
/**
* @tc.name: threadWorker_addEventListener_test_002
* @tc.desc: Add an event listener to the Worker.
*/
it('threadWorker_addEventListener_test_002', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{
times++
})
ss.addEventListener("lisi", ()=>{
times++
})
ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "lisi"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(2)
done()
})
// check worker addEventListener function is ok
/**
* @tc.name: threadWorker_addEventListener_test_003
* @tc.desc: Add an event listener to the Worker.
*/
it('threadWorker_addEventListener_test_003', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{
times++
})
for (let i=0;i<10;i++)
{
ss.dispatchEvent({type: "zhangsan"})
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(10)
done()
})
// check worker addEventListener function is ok
/**
* @tc.name: threadWorker_addEventListener_test_004
* @tc.desc: Add an event listener to the Worker when throw error.
*/
it('threadWorker_addEventListener_test_004', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
try {
ss.addEventListener(1, ()=>{
times++
})
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker add listener 1st param must be string")
ss.onexit = function () {
isTerminate = true
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}
})
// check worker addEventListener function is ok
/**
* @tc.name: threadWorker_addEventListener_test_005
* @tc.desc: Add an event listener to the Worker when throw error.
*/
it('threadWorker_addEventListener_test_005', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
try {
ss.addEventListener("zhangsan", "error")
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker add listener 2st param must be callable")
ss.onexit = function () {
isTerminate = true
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}
})
// check worker addEventListener function is ok
/**
* @tc.name: threadWorker_addEventListener_test_006
* @tc.desc: Add an event listener to the Worker when throw error.
*/
it('threadWorker_addEventListener_test_006', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
try {
ss.addEventListener()
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("worker add listener param count must be not less than 2.")
ss.onexit = function () {
isTerminate = true
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}
})
// check worker addEventListener function is ok
/**
* @tc.name: threadWorker_addEventListener_test_007
* @tc.desc: Add an event listener to the Worker when throw error.
*/
it('threadWorker_addEventListener_test_007', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
try {
ss.addEventListener("zhangsan", ()=>{
times++
})
ss.dispatchEvent({type: "zhangsan"})
ss.onexit = function () {
isTerminate = true
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
ss.addEventListener("lisi", ()=>{
times++
})
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker instance is not running, maybe worker is terminated")
done()
}
})
// check worker addEventListener function is ok
/**
* @tc.name: threadWorker_addEventListener_test_008
* @tc.desc: Add an event listener to the Worker when throw error.
*/
it('threadWorker_addEventListener_test_008', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let times = 0
let isTerminate = false
try {
ss.addEventListener("zhangsan", ()=>{
times++
})
ss.dispatchEvent({type: "zhangsan"})
ss.onexit = function () {
isTerminate = true
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
ss.dispatchEvent({type: "zhangsan"})
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker instance is not running, maybe worker has been terminated")
done()
}
})
// check worker off function is ok
/**
* @tc.name: threadWorker_off_test_001
* @tc.desc: Removes an event listener to the worker.
*/
it('threadWorker_off_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let zhangSanTimes = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.on("zhangsan", ()=>{
zhangSanTimes++
})
ss.dispatchEvent({type: "zhangsan"})
expect(zhangSanTimes).assertEqual(1)
ss.off("zhangsan")
ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(zhangSanTimes).assertEqual(1)
done()
})
// check worker off function is ok
/**
* @tc.name: threadWorker_off_test_002
* @tc.desc: Removes an event listener to the worker.
*/
it('threadWorker_off_test_002', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let zhangSanTimes = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.on("zhangsan", ()=>{
zhangSanTimes++
})
ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"})
expect(zhangSanTimes).assertEqual(2)
for (let i=0;i<3;i++)
{
ss.off("zhangsan")
}
ss.dispatchEvent({type: "zhangsan"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(zhangSanTimes).assertEqual(2)
done()
})
// check worker off function is ok
/**
* @tc.name: threadWorker_off_test_003
* @tc.desc: Removes an event listener to the worker when throw error.
*/
it('threadWorker_off_test_003', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let zhangSanTimes = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.on("zhangsan", ()=>{
zhangSanTimes++
})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
try {
ss.off("zhangsan")
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker instance is not running, maybe worker is terminated")
done()
}
})
// check worker off function is ok
/**
* @tc.name: threadWorker_off_test_004
* @tc.desc: Removes an event listener to the worker when throw error.
*/
it('threadWorker_off_test_004', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let zhangSanTimes = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.on("zhangsan", ()=>{
zhangSanTimes++
})
try {
ss.off()
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("the remove listener param must be not less than 1")
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}
})
// check worker off function is ok
/**
* @tc.name: threadWorker_off_test_005
* @tc.desc: Removes an event listener to the worker when throw error.
*/
it('threadWorker_off_test_005', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let zhangSanTimes = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.on("zhangsan", ()=>{
zhangSanTimes++
})
try {
ss.off(1)
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("the type of remove listener 1st param must be string")
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}
})
// check worker removeEventListener function is ok
/**
* @tc.name: threadWorker_removeListener_test_001
* @tc.desc: Removes an event defined for the worker.
*/
it('threadWorker_removeListener_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let zhangSanTimes = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{
zhangSanTimes++
})
ss.dispatchEvent({type: "zhangsan"})
expect(zhangSanTimes).assertEqual(1)
ss.removeEventListener("zhangsan")
ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(zhangSanTimes).assertEqual(1)
done()
})
// check worker removeEventListener function is ok
/**
* @tc.name: threadWorker_removeListener_test_002
* @tc.desc: Removes an event defined for the worker.
*/
it('threadWorker_removeListener_test_002', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let zhangSanTimes = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{
zhangSanTimes++
})
ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"})
expect(zhangSanTimes).assertEqual(2)
for (let i=0;i<3;i++)
{
ss.removeEventListener("zhangsan")
}
ss.dispatchEvent({type: "zhangsan"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(zhangSanTimes).assertEqual(2)
done()
})
// check worker removeAllListener function is ok
/**
* @tc.name: threadWorker_removeListener_test_003
* @tc.desc: Removes an event defined for the worker.
*/
it('threadWorker_removeListener_test_003', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let zhangSanTimes = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{
zhangSanTimes++
})
let liSiTimes = 0
ss.addEventListener("lisi", ()=>{
liSiTimes++
})
ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"})
expect(zhangSanTimes).assertEqual(2)
ss.dispatchEvent({type: "lisi"})
ss.dispatchEvent({type: "lisi"})
expect(liSiTimes).assertEqual(2)
ss.removeAllListener()
ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"})
expect(zhangSanTimes).assertEqual(2)
ss.dispatchEvent({type: "lisi"})
ss.dispatchEvent({type: "lisi"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(liSiTimes).assertEqual(2)
done()
})
// check worker removeAllListener function is ok
/**
* @tc.name: threadWorker_removeListener_test_003
* @tc.desc: Removes an event defined for the worker when throw error.
*/
it('threadWorker_removeListener_test_003', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let zhangSanTimes = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{
zhangSanTimes++
})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
try {
ss.removeAllListener()
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker instance is not running, maybe worker is terminated")
done()
}
})
// check worker removeAllListener function is ok
/**
* @tc.name: threadWorker_removeListener_test_004
* @tc.desc: Removes an event defined for the worker when throw error.
*/
it('threadWorker_removeListener_test_004', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let zhangSanTimes = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{
zhangSanTimes++
})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
try {
ss.removeEventListener("zhangsan")
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("Worker instance is not running, maybe worker is terminated")
done()
}
})
// check worker removeAllListener function is ok
/**
* @tc.name: threadWorker_removeListener_test_005
* @tc.desc: Removes an event defined for the worker when throw error.
*/
it('threadWorker_removeListener_test_005', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let zhangSanTimes = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{
zhangSanTimes++
})
try {
ss.removeEventListener()
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("the remove listener param must be not less than 1")
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}
})
// check worker removeAllListener function is ok
/**
* @tc.name: threadWorker_removeListener_test_006
* @tc.desc: Removes an event defined for the worker when throw error.
*/
it('threadWorker_removeListener_test_006', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker.js")
let zhangSanTimes = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{
zhangSanTimes++
})
try {
ss.removeEventListener(1)
} catch (error) {
expect(error.name).assertEqual("BusinessError")
expect(error.message).assertEqual("the type of remove listener 1st param must be string")
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}
})
// check parentPort.close is ok
/**
* @tc.name: threadWorker_parentPortClose_test_001
* @tc.desc: Close the worker thread to stop the worker from receiving messages.
*/
it('threadWorker_parentPortClose_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_008.js")
let res = 0
let flag = false
ss.onexit = function (e) {
res++
flag = true
}
ss.postMessage("abc")
while (!flag) {
await promiseCase()
}
expect(res).assertEqual(1)
done()
})
// check parentPort.close is ok
/**
* @tc.name: threadWorker_parentPortClose_test_002
* @tc.desc: Close the worker thread to stop the worker from receiving messages.
*/
it('threadWorker_parentPortClose_test_002', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_008.js")
let res = 0
let flag = false
ss.onexit = function () {
flag = true
}
ss.onmessage = function (e) {
res++
}
ss.postMessage("abc")
while (!flag) {
await promiseCase()
}
try {
ss.postMessage("hello")
} catch (error) {
expect(res).assertEqual(0)
expect(error.name).assertEqual("BusinessError")
let msg = "Worker instance is not running, maybe worker is terminated when PostMessage"
expect(error.message).assertEqual(msg)
done()
}
})
// check onmessageerror is ok
/**
* @tc.name: threadWorker_onmessageerror_test_001
* @tc.desc: The onmessage attribute of the worker specifies the event handler when the worker receives
a message that cannot be serialized. The event handler is executed in the host thread
*/
it('threadWorker_onmessageerror_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_008.js")
let res = 0
let flag = false
ss.onexit = function () {
flag = true
}
ss.onmessageerror = function (e) {
res++
}
ss.postMessage("abc")
while (!flag) {
await promiseCase()
}
expect(res).assertEqual(0)
done()
})
// check onmessageerror is ok
/**
* @tc.name: threadWorker_onmessageerror_test_002
* @tc.desc: The onmessage attribute of the worker specifies the event handler when the worker receives
a message that cannot be serialized. The event handler is executed in the host thread
*/
it('threadWorker_onmessageerror_test_002', 0, async function (done) {
try {
var ss = new worker.ThreadWorker("workers/newworker_008.js")
var res = 0
var flag = false
ss.onexit = function() {
flag = true
}
ss.onmessageerror = function (e) {
flag = true
res++
}
function foo() {
}
ss.postMessage(foo)
while (!flag) {
await promiseCase()
}
} catch (error) {
flag = false
ss.postMessage("terminate")
while (!flag) {
await promiseCase()
}
expect(res).assertEqual(1)
expect(error.name).assertEqual("BusinessError")
let msg = "Serializing an uncaught exception failed, failed to serialize message."
expect(error.message).assertEqual(msg)
done()
}
})
// check new second worker is ok
/**
* @tc.name: threadWorker_new_second_worker_test_001
* @tc.desc: Create a second worker.
*/
it('threadWorker_new_second_worker_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_009.js")
let flag = false
let res = undefined
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) {
flag = true
res = e.data
}
ss.postMessage({type: "new", value: 12})
while (!flag) {
console.log("worker:: foo wait")
await promiseCase()
}
console.log("worker:: " + res)
expect(res).assertTrue()
flag = false
ss.postMessage({type: "terminate"})
while (!flag) {
ss.postMessage({type: "wait"})
await promiseCase()
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
})
// check new third worker is ok
/**
* @tc.name: threadWorker_new_second_worker_test_002
* @tc.desc: Create a third worker.
*/
it('threadWorker_new_second_worker_test_002', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_012.js")
let flag = false
let res = undefined
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) {
flag = true
res = e.data
}
ss.onerror = function(ee) {
console.log("worker:: " + ee.message)
}
ss.postMessage({type: "new", value: 12})
while (!flag) {
ss.postMessage({type: "wait"})
await promiseCase()
}
expect(res).assertTrue()
flag = false
ss.postMessage({type: "terminate"})
while (!flag) {
ss.postMessage({type: "wait"})
await promiseCase()
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
})
// check second worker postMessage number is ok
/**
* @tc.name: threadWorker_second_worker_postMessage_test_001
* @tc.desc: Check if the postMessage function of the second worker is ok.
*/
it('threadWorker_second_worker_postMessage_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_010.js")
let flag = false
let res = undefined
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) {
flag = true
res = e.data
}
ss.onerror = function(ee) {
console.log("worker:: " + ee.message)
}
ss.postMessage({type: "new", value: 12})
while (!flag) {
ss.postMessage({type: "wait"})
await promiseCase()
}
expect(res).assertEqual(25)
flag = false
ss.postMessage({type: "terminate"})
while (!flag) {
ss.postMessage({type: "wait"})
await promiseCase()
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
})
// check second worker postMessage string is ok
/**
* @tc.name: threadWorker_second_worker_postMessage_test_002
* @tc.desc: Check if the postMessage function of the second worker is ok.
*/
it('threadWorker_second_worker_postMessage_test_002', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_013.js")
let flag = false
let res = undefined
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) {
flag = true
res = e.data
}
ss.onerror = function(ee) {
console.log("worker:: " + ee.message)
}
ss.postMessage({type: "new", value: "hello world"})
while (!flag) {
ss.postMessage({type: "wait"})
await promiseCase()
}
expect(res).assertEqual("hello world worker")
flag = false
ss.postMessage({type: "terminate"})
while (!flag) {
ss.postMessage({type: "wait"})
await promiseCase()
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
})
// check second worker postMessage array is ok
/**
* @tc.name: threadWorker_second_worker_postMessage_test_003
* @tc.desc: Check if the postMessage function of the second worker is ok.
*/
it('threadWorker_second_worker_postMessage_test_003', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_014.js")
let flag = false
let res = undefined
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) {
flag = true
res = e.data
}
ss.onerror = function(ee) {
console.log("worker:: " + ee.message)
}
ss.postMessage({type: "new", value: [1, 2]})
while (!flag) {
ss.postMessage({type: "wait"})
await promiseCase()
}
expect(res[0]).assertEqual(2)
expect(res[1]).assertEqual(2)
flag = false
ss.postMessage({type: "terminate"})
while (!flag) {
ss.postMessage({type: "wait"})
await promiseCase()
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
})
// check third worker postMessage is ok
/**
* @tc.name: threadWorker_third_worker_postMessage_test_001
* @tc.desc: Check if the postMessage function of the third worker is ok.
*/
it('threadWorker_third_worker_postMessage_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_015.js")
let flag = false
let res = undefined
let isTerminate = false
ss.onexit = function() {
isTerminate = true
}
ss.onmessage = function(e) {
flag = true
res = e.data
}
ss.onerror = function(ee) {
console.log("worker:: " + ee.message)
}
ss.postMessage({type: "new", value: 10})
while (!flag) {
ss.postMessage({type: "wait"})
await promiseCase()
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(res).assertEqual(16)
done()
})
// check second worker terminate is ok
/**
* @tc.name: threadWorker_second_worker_terminate_test_001
* @tc.desc: Check if the terminate function of the second worker is ok.
*/
it('threadWorker_second_worker_terminate_test_001', 0, async function (done) {
let ss = new worker.ThreadWorker("workers/newworker_011.js")
let flag = false
let res = undefined
let isTerminate = false
ss.onexit = function() {
isTerminate = true
}
ss.onmessage = function(e) {
flag = true
res = e.data
}
ss.onerror = function(ee) {
console.log("worker:: " + ee.message)
}
ss.postMessage({type: "new", value: 12})
while (!flag) {
ss.postMessage({type: "wait"})
await promiseCase()
}
flag = false
ss.postMessage({type: "terminate"})
while (!flag) {
ss.postMessage({type: "wait"})
await promiseCase()
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(res).assertEqual("terminate")
done()
})
})
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册