提交 7f045ed2 编写于 作者: W wbdu

Modify the use case of worker

Describe:The worker warehouse has modified the maximum number of
workers, so the test case of the worker needs to be modified.

Issues:https://gitee.com/openharmony/js_worker_module/issues/I4ZPSISigned-off-by: Nwbdu <duwenbo7@huawei.com>
上级 5114e0f5
...@@ -24,8 +24,20 @@ parentPort.onmessage = function(e) { ...@@ -24,8 +24,20 @@ parentPort.onmessage = function(e) {
case "new": case "new":
ss = new worker.Worker("workers/worker_0091.js"); ss = new worker.Worker("workers/worker_0091.js");
console.log("worker:: workerxx "); console.log("worker:: workerxx ");
ss.onexit = function() {
flag = true;
}
parentPort.postMessage(ss != null); parentPort.postMessage(ss != null);
break; break;
case "wait":
if (flag) {
parentPort.postMessage("terminate");
}
break;
case "terminate":
flag = false;
ss.terminate();
break;
default: default:
break; break;
} }
......
...@@ -29,6 +29,10 @@ parentPort.onmessage = function(e) { ...@@ -29,6 +29,10 @@ parentPort.onmessage = function(e) {
backValue = ee.data; backValue = ee.data;
flag = true; flag = true;
} }
ss.onexit = function() {
backValue = "terminate";
flag = true;
}
ss.postMessage(data.value); ss.postMessage(data.value);
break; break;
case "wait": case "wait":
...@@ -36,6 +40,10 @@ parentPort.onmessage = function(e) { ...@@ -36,6 +40,10 @@ parentPort.onmessage = function(e) {
parentPort.postMessage(backValue); parentPort.postMessage(backValue);
} }
break; break;
case "terminate":
flag = false;
ss.terminate();
break;
default: default:
break; break;
} }
......
...@@ -29,6 +29,10 @@ parentPort.onmessage = function(e) { ...@@ -29,6 +29,10 @@ parentPort.onmessage = function(e) {
backValue = ee.data; backValue = ee.data;
flag = true; flag = true;
} }
ss.onexit = function() {
backValue = "terminate";
flag = true;
}
ss.postMessage({type: "new"}); ss.postMessage({type: "new"});
break; break;
case "wait": case "wait":
...@@ -36,6 +40,10 @@ parentPort.onmessage = function(e) { ...@@ -36,6 +40,10 @@ parentPort.onmessage = function(e) {
parentPort.postMessage(backValue); parentPort.postMessage(backValue);
} }
break; break;
case "terminate":
flag = false;
ss.terminate();
break;
default: default:
break; break;
} }
......
...@@ -29,6 +29,10 @@ parentPort.onmessage = function(e) { ...@@ -29,6 +29,10 @@ parentPort.onmessage = function(e) {
backValue = ee.data; backValue = ee.data;
flag = true; flag = true;
} }
ss.onexit = function() {
backValue = "terminate";
flag = true;
}
ss.postMessage(data.value); ss.postMessage(data.value);
break; break;
case "wait": case "wait":
...@@ -36,6 +40,10 @@ parentPort.onmessage = function(e) { ...@@ -36,6 +40,10 @@ parentPort.onmessage = function(e) {
parentPort.postMessage(backValue); parentPort.postMessage(backValue);
} }
break; break;
case "terminate":
flag = false;
ss.terminate();
break;
default: default:
break; break;
} }
......
...@@ -29,6 +29,10 @@ parentPort.onmessage = function(e) { ...@@ -29,6 +29,10 @@ parentPort.onmessage = function(e) {
backValue = ee.data; backValue = ee.data;
flag = true; flag = true;
} }
ss.onexit = function() {
backValue = "terminate";
flag = true;
}
ss.postMessage(data.value); ss.postMessage(data.value);
break; break;
case "wait": case "wait":
...@@ -36,6 +40,10 @@ parentPort.onmessage = function(e) { ...@@ -36,6 +40,10 @@ parentPort.onmessage = function(e) {
parentPort.postMessage(backValue); parentPort.postMessage(backValue);
} }
break; break;
case "terminate":
flag = false;
ss.terminate();
break;
default: default:
break; break;
} }
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
// @ts-nocheck // @ts-nocheck
import app from '@system.app' import app from '@system.app'
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
import worker from "@ohos.worker"; import worker from "@ohos.worker"
describe('workerTest', function () { describe('workerTest', function () {
...@@ -28,43 +28,95 @@ describe('workerTest', function () { ...@@ -28,43 +28,95 @@ describe('workerTest', function () {
let p = new Promise(function (resolve, reject) { let p = new Promise(function (resolve, reject) {
setTimeout(function () { setTimeout(function () {
resolve() resolve()
}, 100); }, 100)
}).then(undefined, (error) => { }).then(undefined, (error) => {
}) })
return p return p
} }
// check worker constructor is ok // check worker constructor is ok
it('worker_constructor_test_001', 0, function () { it('worker_constructor_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js") let ss = new worker.Worker("workers/worker.js")
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
expect(ss != null).assertTrue() expect(ss != null).assertTrue()
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}) })
it('worker_constructor_test_002', 0, function () { it('worker_constructor_test_002', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js",{name:"second worker",shared:"False"}) let ss = new worker.Worker("workers/worker.js", {name:"second worker", shared:"False"})
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
expect(ss != null).assertTrue() expect(ss != null).assertTrue()
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}) })
it('worker_constructor_test_003', 0, function () { it('worker_constructor_test_003', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js",{type:"classic",name:"second worker",shared:"false"}) let ss = new worker.Worker("workers/worker.js", {type:"classic", name:"second worker", shared:"false"})
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
expect(ss != null).assertTrue() expect(ss != null).assertTrue()
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}) })
it('worker_constructor_test_004', 0, function () { it('worker_constructor_test_004', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js",{type:"classic",name:"third worker",shared:"true"}) let ss = new worker.Worker("workers/worker.js", {type:"classic", name:"third worker", shared:"true"})
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
expect(ss != null).assertTrue() expect(ss != null).assertTrue()
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
done()
}) })
// check postMessage is ok // check postMessage is ok
// main post "hello world", will receive "hello world worker" // main post "hello world", will receive "hello world worker"
it('worker_postMessage_test_001', 0, async function (done) { it('worker_postMessage_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker_002.js"); let ss = new worker.Worker("workers/worker_002.js")
let res = undefined let res = undefined
let flag = false let flag = false
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) { ss.onmessage = function (e) {
res = e.data; res = e.data
flag = true flag = true
} }
...@@ -72,6 +124,12 @@ describe('workerTest', function () { ...@@ -72,6 +124,12 @@ describe('workerTest', function () {
while (!flag) { while (!flag) {
await promiseCase() await promiseCase()
} }
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(res).assertEqual("hello world worker") expect(res).assertEqual("hello world worker")
done() done()
}) })
...@@ -79,13 +137,17 @@ describe('workerTest', function () { ...@@ -79,13 +137,17 @@ describe('workerTest', function () {
// check postMessage is ok // check postMessage is ok
// main post 12 , will receive 12 * 2 + 1 // main post 12 , will receive 12 * 2 + 1
it('worker_postMessage_test_002', 0, async function (done) { it('worker_postMessage_test_002', 0, async function (done) {
let ss = new worker.Worker("workers/worker_003.js"); let ss = new worker.Worker("workers/worker_003.js")
let res = undefined let res = undefined
let flag = false let flag = false
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) { ss.onmessage = function (e) {
res = e.data; res = e.data
flag = true flag = true
} }
...@@ -93,6 +155,12 @@ describe('workerTest', function () { ...@@ -93,6 +155,12 @@ describe('workerTest', function () {
while (!flag) { while (!flag) {
await promiseCase() await promiseCase()
} }
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(res).assertEqual(25) expect(res).assertEqual(25)
done() done()
}) })
...@@ -100,13 +168,17 @@ describe('workerTest', function () { ...@@ -100,13 +168,17 @@ describe('workerTest', function () {
// check postMessage is ok // check postMessage is ok
// main post {message:"hello world"} , will receive {message:"hello world worker"} // main post {message:"hello world"} , will receive {message:"hello world worker"}
it('worker_postMessage_test_003', 0, async function (done) { it('worker_postMessage_test_003', 0, async function (done) {
let ss = new worker.Worker("workers/worker_004.js"); let ss = new worker.Worker("workers/worker_004.js")
let res = undefined let res = undefined
let flag = false let flag = false
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) { ss.onmessage = function (e) {
res = e.data.message; res = e.data.message
flag = true flag = true
} }
...@@ -114,19 +186,29 @@ describe('workerTest', function () { ...@@ -114,19 +186,29 @@ describe('workerTest', function () {
while (!flag) { while (!flag) {
await promiseCase() await promiseCase()
} }
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(res).assertEqual("hello world worker") expect(res).assertEqual("hello world worker")
done() done()
}) })
// check worker name is ok // check worker name is ok
it('worker_postMessage_test_004', 0, async function (done) { it('worker_postMessage_test_004', 0, async function (done) {
let ss = new worker.Worker("workers/worker_005.js", {name: "zhangsan"}); let ss = new worker.Worker("workers/worker_005.js", {name: "zhangsan"})
let res = undefined let res = undefined
let flag = false let flag = false
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) { ss.onmessage = function (e) {
res = e.data; res = e.data
flag = true flag = true
} }
...@@ -134,23 +216,39 @@ describe('workerTest', function () { ...@@ -134,23 +216,39 @@ describe('workerTest', function () {
while (!flag) { while (!flag) {
await promiseCase() await promiseCase()
} }
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(res).assertEqual("zhangsan") expect(res).assertEqual("zhangsan")
done() done()
}) })
// check worker transfer buffer is ok // check worker transfer buffer is ok
it('worker_postMessage_test_005', 0, function () { it('worker_postMessage_test_005', 0, async function (done) {
let ss = new worker.Worker("workers/worker_006.js"); let ss = new worker.Worker("workers/worker_006.js")
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
const buffer = new ArrayBuffer(8) const buffer = new ArrayBuffer(8)
expect(buffer.byteLength).assertEqual(8) expect(buffer.byteLength).assertEqual(8)
ss.postMessage(buffer, [buffer]) ss.postMessage(buffer, [buffer])
let length = undefined; let length = undefined
let exception = undefined; let exception = undefined
try { try {
length = buffer.byteLength; length = buffer.byteLength
} catch (e) { } catch (e) {
exception = e.message; exception = e.message
}
ss.terminate()
while (!isTerminate) {
await promiseCase()
} }
console.log("worker:: length is " + length) console.log("worker:: length is " + length)
...@@ -161,55 +259,68 @@ describe('workerTest', function () { ...@@ -161,55 +259,68 @@ describe('workerTest', function () {
} else { } else {
expect(exception).assertEqual("IsDetachedBuffer") expect(exception).assertEqual("IsDetachedBuffer")
} }
done()
}) })
// check worker handle error is ok // check worker handle error is ok
it('worker_postMessage_test_006', 0, async function (done) { it('worker_postMessage_test_006', 0, async function (done) {
let ss = new worker.Worker("workers/worker_007.js"); let ss = new worker.Worker("workers/worker_007.js")
let res = undefined let res = undefined
let flag = false let flag = false
let isTerminate = false
ss.onerror = function (e) { ss.onerror = function (e) {
res = e.message; res = e.message
flag = true flag = true
} }
ss.onexit = function () {
isTerminate = true
}
ss.postMessage("hello world") ss.postMessage("hello world")
while (!flag) { while (!flag) {
await promiseCase() await promiseCase()
} }
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(res).assertEqual("Error: 123") expect(res).assertEqual("Error: 123")
done() done()
}) })
// check worker terminate is ok // check worker terminate is ok
it('worker_terminate_test_001', 0, async function (done) { it('worker_terminate_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let res = 0 let res = 0
let flag = false let flag = false
ss.onexit = function () { ss.onexit = function () {
flag = true flag = true
res++; res++
} }
ss.terminate() ss.terminate()
while (!flag) { while (!flag) {
await promiseCase() await promiseCase()
} }
expect(res).assertEqual(1) expect(res).assertEqual(1)
done() done()
}) })
// check worker terminate is ok // check worker terminate is ok
it('worker_terminate_test_002', 0, async function (done) { it('worker_terminate_test_002', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let res = 0 let res = 0
let flag = false let flag = false
ss.onexit = function () { ss.onexit = function () {
flag = true flag = true
res++; res++
} }
// Repeated execution terminate // Repeated execution terminate
...@@ -219,23 +330,23 @@ describe('workerTest', function () { ...@@ -219,23 +330,23 @@ describe('workerTest', function () {
while (!flag) { while (!flag) {
await promiseCase() await promiseCase()
} }
expect(res).assertEqual(1) expect(res).assertEqual(1)
done() done()
}) })
// check worker terminate is ok // check worker terminate is ok
it('worker_terminate_test_003', 0, async function (done) { it('worker_terminate_test_003', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let res = 0 let res = 0
let flag = false let flag = false
ss.onexit = function () { ss.onexit = function () {
flag = true flag = true
res++; res++
} }
ss.onmessage = function (e) { ss.onmessage = function (e) {
res++; res++
} }
ss.terminate() ss.terminate()
...@@ -251,74 +362,124 @@ describe('workerTest', function () { ...@@ -251,74 +362,124 @@ describe('workerTest', function () {
}) })
// check worker on function is ok // check worker on function is ok
it('worker_on_test_001', 0, function () { it('worker_on_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let times = 0
let isTerminate = false
let times = 0; ss.onexit = function () {
isTerminate = true
}
ss.on("zhangsan", ()=>{ ss.on("zhangsan", ()=>{
times++; times++
}) })
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(1) expect(times).assertEqual(1)
done()
}) })
// check worker on function is ok // check worker on function is ok
it('worker_on_test_002', 0, function () { it('worker_on_test_002', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let times = 0
let isTerminate = false
let times = 0; ss.onexit = function () {
isTerminate = true
}
ss.on("zhangsan", ()=>{ ss.on("zhangsan", ()=>{
times--; times--
}) })
ss.on("lisi", ()=>{ ss.on("lisi", ()=>{
times++; times++
}) })
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "lisi"}) ss.dispatchEvent({type: "lisi"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(0) expect(times).assertEqual(0)
done()
}) })
// check worker on function is ok // check worker on function is ok
it('worker_on_test_003', 0, function () { it('worker_on_test_003', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let times = 0
let isTerminate = false
let times = 0; ss.onexit = function () {
isTerminate = true
}
ss.on("zhangsan", ()=>{ ss.on("zhangsan", ()=>{
times++; times++
}) })
for (let i=0;i<10;i++) for (let i=0;i<10;i++)
{ {
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
} }
console.log("times " + times);
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
console.log("times " + times)
expect(times).assertEqual(10) expect(times).assertEqual(10)
done()
}) })
// check worker once function is ok // check worker once function is ok
it('worker_once_test_001', 0, function () { it('worker_once_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let times = 0
let isTerminate = false
let times = 0; ss.onexit = function () {
isTerminate = true
}
ss.once("zhangsan", ()=>{ ss.once("zhangsan", ()=>{
times++; times++
}) })
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(1) expect(times).assertEqual(1)
done()
}) })
// check worker once function is ok // check worker once function is ok
it('worker_once_test_002', 0, function () { it('worker_once_test_002', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let times = 0
let isTerminate = false
let times = 0; ss.onexit = function () {
isTerminate = true
}
ss.once("zhangsan", ()=>{ ss.once("zhangsan", ()=>{
times++; times++
}) })
for (let i=0;i<10;i++) for (let i=0;i<10;i++)
...@@ -326,68 +487,111 @@ describe('workerTest', function () { ...@@ -326,68 +487,111 @@ describe('workerTest', function () {
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
} }
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(1) expect(times).assertEqual(1)
done()
}) })
it('worker_once_test_003', 0, function () { it('worker_once_test_003', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let times = 0
let isTerminate = false
let times = 0; ss.onexit = function () {
isTerminate = true
}
ss.once("zhangsan", ()=>{ ss.once("zhangsan", ()=>{
times--; times--
}) })
ss.once("lisi", ()=>{ ss.once("lisi", ()=>{
times++; times++
}) })
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "lisi"}) ss.dispatchEvent({type: "lisi"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(0) expect(times).assertEqual(0)
done()
}) })
// check worker addEventListener function is ok // check worker addEventListener function is ok
it('worker_addEventListener_test_001', 0, function () { it('worker_addEventListener_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let times = 0
let isTerminate = false
let times = 0; ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{ ss.addEventListener("zhangsan", ()=>{
times++; times++
}) })
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(2) expect(times).assertEqual(2)
done()
}) })
// check worker addEventListener function is ok // check worker addEventListener function is ok
it('worker_addEventListener_test_002', 0, function () { it('worker_addEventListener_test_002', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let times = 0; let times = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{ ss.addEventListener("zhangsan", ()=>{
times++; times++
}) })
ss.addEventListener("lisi", ()=>{ ss.addEventListener("lisi", ()=>{
times++; times++
}) })
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "lisi"}) ss.dispatchEvent({type: "lisi"})
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(2) expect(times).assertEqual(2)
done()
}) })
// check worker addEventListener function is ok // check worker addEventListener function is ok
it('worker_addEventListener_test_003', 0, function () { it('worker_addEventListener_test_003', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let times = 0; let times = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{ ss.addEventListener("zhangsan", ()=>{
times++; times++
}) })
for (let i=0;i<10;i++) for (let i=0;i<10;i++)
...@@ -395,40 +599,63 @@ describe('workerTest', function () { ...@@ -395,40 +599,63 @@ describe('workerTest', function () {
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
} }
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(times).assertEqual(10) expect(times).assertEqual(10)
done()
}) })
// check worker off function is ok // check worker off function is ok
it('worker_off_test_001', 0, function () { it('worker_off_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let zhangSanTimes = 0
let isTerminate = false
let zhangsan_times = 0; ss.onexit = function () {
isTerminate = true
}
ss.on("zhangsan", ()=>{ ss.on("zhangsan", ()=>{
zhangsan_times++; zhangSanTimes++
}) })
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
expect(zhangsan_times).assertEqual(1) expect(zhangSanTimes).assertEqual(1)
ss.off("zhangsan") ss.off("zhangsan")
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
expect(zhangsan_times).assertEqual(1)
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(zhangSanTimes).assertEqual(1)
done()
}) })
// check worker off function is ok // check worker off function is ok
it('worker_off_test_002', 0, function () { it('worker_off_test_002', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let zhangSanTimes = 0
let isTerminate = false
let zhangsan_times = 0; ss.onexit = function () {
isTerminate = true
}
ss.on("zhangsan", ()=>{ ss.on("zhangsan", ()=>{
zhangsan_times++; zhangSanTimes++
}) })
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
expect(zhangsan_times).assertEqual(2) expect(zhangSanTimes).assertEqual(2)
for (let i=0;i<3;i++) for (let i=0;i<3;i++)
{ {
...@@ -436,40 +663,64 @@ describe('workerTest', function () { ...@@ -436,40 +663,64 @@ describe('workerTest', function () {
} }
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
expect(zhangsan_times).assertEqual(2)
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(zhangSanTimes).assertEqual(2)
done()
}) })
// check worker removeEventListener function is ok // check worker removeEventListener function is ok
it('worker_removeListener_test_001', 0, function () { it('worker_removeListener_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let zhangSanTimes = 0
let isTerminate = false
let zhangsan_times = 0; ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{ ss.addEventListener("zhangsan", ()=>{
zhangsan_times++; zhangSanTimes++
}) })
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
expect(zhangsan_times).assertEqual(1) expect(zhangSanTimes).assertEqual(1)
ss.removeEventListener("zhangsan") ss.removeEventListener("zhangsan")
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
expect(zhangsan_times).assertEqual(1)
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(zhangSanTimes).assertEqual(1)
done()
}) })
// check worker removeEventListener function is ok // check worker removeEventListener function is ok
it('worker_removeListener_test_002', 0, function () { it('worker_removeListener_test_002', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let zhangSanTimes = 0
let isTerminate = false
let zhangsan_times = 0; ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{ ss.addEventListener("zhangsan", ()=>{
zhangsan_times++; zhangSanTimes++
}) })
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
expect(zhangsan_times).assertEqual(2) expect(zhangSanTimes).assertEqual(2)
for (let i=0;i<3;i++) for (let i=0;i<3;i++)
{ {
...@@ -477,73 +728,92 @@ describe('workerTest', function () { ...@@ -477,73 +728,92 @@ describe('workerTest', function () {
} }
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
expect(zhangsan_times).assertEqual(2)
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(zhangSanTimes).assertEqual(2)
done()
}) })
// check worker removeAllListener function is ok // check worker removeAllListener function is ok
it('worker_removeListener_test_003', 0, function () { it('worker_removeListener_test_003', 0, async function (done) {
let ss = new worker.Worker("workers/worker.js"); let ss = new worker.Worker("workers/worker.js")
let zhangsan_times = 0; let zhangSanTimes = 0
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.addEventListener("zhangsan", ()=>{ ss.addEventListener("zhangsan", ()=>{
zhangsan_times++; zhangSanTimes++
}) })
let lisi_times = 0; let liSiTimes = 0
ss.addEventListener("lisi", ()=>{ ss.addEventListener("lisi", ()=>{
lisi_times++; liSiTimes++
}) })
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
expect(zhangsan_times).assertEqual(2) expect(zhangSanTimes).assertEqual(2)
ss.dispatchEvent({type: "lisi"}) ss.dispatchEvent({type: "lisi"})
ss.dispatchEvent({type: "lisi"}) ss.dispatchEvent({type: "lisi"})
expect(lisi_times).assertEqual(2) expect(liSiTimes).assertEqual(2)
ss.removeAllListener() ss.removeAllListener()
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
ss.dispatchEvent({type: "zhangsan"}) ss.dispatchEvent({type: "zhangsan"})
expect(zhangsan_times).assertEqual(2) expect(zhangSanTimes).assertEqual(2)
ss.dispatchEvent({type: "lisi"}) ss.dispatchEvent({type: "lisi"})
ss.dispatchEvent({type: "lisi"}) ss.dispatchEvent({type: "lisi"})
expect(lisi_times).assertEqual(2)
ss.terminate()
while (!isTerminate) {
await promiseCase()
}
expect(liSiTimes).assertEqual(2)
done()
}) })
// check parentPort.close is ok // check parentPort.close is ok
it('worker_parentPortClose_test_001', 0, async function (done) { it('worker_parentPortClose_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker_008.js"); let ss = new worker.Worker("workers/worker_008.js")
let res = 0 let res = 0
let flag = false; let flag = false
ss.onexit = function (e) { ss.onexit = function (e) {
res++; res++
flag = true; flag = true
} }
ss.postMessage("abc") ss.postMessage("abc")
while (!flag) { while (!flag) {
await promiseCase() await promiseCase()
} }
expect(res).assertEqual(1) expect(res).assertEqual(1)
done() done()
}) })
// check parentPort.close is ok // check parentPort.close is ok
it('worker_parentPortClose_test_002', 0, async function (done) { it('worker_parentPortClose_test_002', 0, async function (done) {
let ss = new worker.Worker("workers/worker_008.js"); let ss = new worker.Worker("workers/worker_008.js")
let res = 0 let res = 0
let flag = false; let flag = false
ss.onexit = function () { ss.onexit = function () {
flag = true flag = true
} }
ss.onmessage = function (e) { ss.onmessage = function (e) {
res++; res++
} }
ss.postMessage("abc") ss.postMessage("abc")
...@@ -559,16 +829,16 @@ describe('workerTest', function () { ...@@ -559,16 +829,16 @@ describe('workerTest', function () {
// check onmessageerror is ok // check onmessageerror is ok
it('worker_onmessageerror_test_001', 0, async function (done) { it('worker_onmessageerror_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker_008.js"); let ss = new worker.Worker("workers/worker_008.js")
let res = 0 let res = 0
let flag = false; let flag = false
ss.onexit = function () { ss.onexit = function () {
flag = true flag = true
} }
ss.onmessageerror = function (e) { ss.onmessageerror = function (e) {
res++; res++
} }
ss.postMessage("abc") ss.postMessage("abc")
...@@ -581,13 +851,13 @@ describe('workerTest', function () { ...@@ -581,13 +851,13 @@ describe('workerTest', function () {
// check onmessageerror is ok // check onmessageerror is ok
it('worker_onmessageerror_test_002', 0, async function (done) { it('worker_onmessageerror_test_002', 0, async function (done) {
let ss = new worker.Worker("workers/worker_008.js"); let ss = new worker.Worker("workers/worker_008.js")
let res = 0 let res = 0
let flag = false; let flag = false
ss.onmessageerror = function (e) { ss.onmessageerror = function (e) {
flag = true; flag = true
res++; res++
} }
function foo() { function foo() {
} }
...@@ -601,180 +871,258 @@ describe('workerTest', function () { ...@@ -601,180 +871,258 @@ describe('workerTest', function () {
// check new second worker is ok // check new second worker is ok
it('worker_new_second_worker_test_001', 0, async function (done) { it('worker_new_second_worker_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker_009.js"); let ss = new worker.Worker("workers/worker_009.js")
let flag = false; let flag = false
let res = undefined; let res = undefined
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) { ss.onmessage = function (e) {
flag = true; flag = true
res = e.data; res = e.data
} }
ss.postMessage({type: "new", value: 12}) ss.postMessage({type: "new", value: 12})
while (!flag) { while (!flag) {
console.log("worker:: foo wait") console.log("worker:: foo wait")
await promiseCase(); await promiseCase()
} }
console.log("worker:: " + res) console.log("worker:: " + res)
expect(res).assertTrue(); expect(res).assertTrue()
done();
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 // check new third worker is ok
it('worker_new_second_worker_test_002', 0, async function (done) { it('worker_new_second_worker_test_002', 0, async function (done) {
let ss = new worker.Worker("workers/worker_012.js"); let ss = new worker.Worker("workers/worker_012.js")
let flag = false; let flag = false
let res = undefined; let res = undefined
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) { ss.onmessage = function (e) {
flag = true; flag = true
res = e.data; res = e.data
} }
ss.onerror = function(ee) { ss.onerror = function(ee) {
console.log("worker:: " + ee.message) console.log("worker:: " + ee.message)
} }
ss.postMessage({type: "new", value: 12}); 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) { while (!flag) {
console.log("worker:: worker_second_worker_postMessage_test_001 wait"); ss.postMessage({type: "wait"})
ss.postMessage({type: "wait"}); await promiseCase()
await promiseCase(); }
ss.terminate()
while (!isTerminate) {
await promiseCase()
} }
expect(res).assertTrue(); done()
done();
}) })
// check second worker postMessage number is ok // check second worker postMessage number is ok
it('worker_second_worker_postMessage_test_001', 0, async function (done) { it('worker_second_worker_postMessage_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker_010.js"); let ss = new worker.Worker("workers/worker_010.js")
let flag = false; let flag = false
let res = undefined; let res = undefined
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) { ss.onmessage = function (e) {
flag = true; flag = true
res = e.data; res = e.data
} }
ss.onerror = function(ee) { ss.onerror = function(ee) {
console.log("worker:: " + ee.message) console.log("worker:: " + ee.message)
} }
ss.postMessage({type: "new", value: 12}); 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) { while (!flag) {
console.log("worker:: worker_second_worker_postMessage_test_001 wait"); ss.postMessage({type: "wait"})
ss.postMessage({type: "wait"}); await promiseCase()
await promiseCase(); }
ss.terminate()
while (!isTerminate) {
await promiseCase()
} }
expect(res).assertEqual(25); done()
done();
}) })
// check second worker postMessage string is ok // check second worker postMessage string is ok
it('worker_second_worker_postMessage_test_002', 0, async function (done) { it('worker_second_worker_postMessage_test_002', 0, async function (done) {
let ss = new worker.Worker("workers/worker_013.js"); let ss = new worker.Worker("workers/worker_013.js")
let flag = false; let flag = false
let res = undefined; let res = undefined
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) { ss.onmessage = function (e) {
flag = true; flag = true
res = e.data; res = e.data
} }
ss.onerror = function(ee) { ss.onerror = function(ee) {
console.log("worker:: " + ee.message) console.log("worker:: " + ee.message)
} }
ss.postMessage({type: "new", value: "hello world"}); 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) { while (!flag) {
console.log("worker:: worker_second_worker_postMessage_test_002 wait") ss.postMessage({type: "wait"})
ss.postMessage({type: "wait"}); await promiseCase()
await promiseCase(); }
ss.terminate()
while (!isTerminate) {
await promiseCase()
} }
expect(res).assertEqual("hello world worker"); done()
done();
}) })
// check second worker postMessage array is ok // check second worker postMessage array is ok
it('worker_second_worker_postMessage_test_003', 0, async function (done) { it('worker_second_worker_postMessage_test_003', 0, async function (done) {
let ss = new worker.Worker("workers/worker_014.js"); let ss = new worker.Worker("workers/worker_014.js")
let flag = false; let flag = false
let res = undefined; let res = undefined
let isTerminate = false
ss.onexit = function () {
isTerminate = true
}
ss.onmessage = function (e) { ss.onmessage = function (e) {
flag = true; flag = true
res = e.data; res = e.data
} }
ss.onerror = function(ee) { ss.onerror = function(ee) {
console.log("worker:: " + ee.message) console.log("worker:: " + ee.message)
} }
ss.postMessage({type: "new", value: [1, 2]}); 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) { while (!flag) {
console.log("worker:: worker_second_worker_postMessage_test_003 wait") ss.postMessage({type: "wait"})
ss.postMessage({type: "wait"}); await promiseCase()
await promiseCase(); }
ss.terminate()
while (!isTerminate) {
await promiseCase()
} }
expect(res[0]).assertEqual(2); done()
expect(res[1]).assertEqual(2);
done();
}) })
// check third worker postMessage is ok // check third worker postMessage is ok
it('worker_second_worker_postMessage_test_004', 0, async function (done) { it('worker_third_worker_postMessage_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker_015.js"); let ss = new worker.Worker("workers/worker_015.js")
let flag = false; let flag = false
let res = undefined; let res = undefined
ss.onmessage = function (e) { ss.onmessage = function (e) {
flag = true; flag = true
res = e.data; res = e.data
} }
ss.onerror = function(ee) { ss.onerror = function(ee) {
console.log("worker:: " + ee.message) console.log("worker:: " + ee.message)
} }
ss.postMessage({type: "new", value: 10}); ss.postMessage({type: "new", value: 10})
while (!flag) { while (!flag) {
console.log("worker:: worker_second_worker_postMessage_test_003 wait") ss.postMessage({type: "wait"})
ss.postMessage({type: "wait"}); await promiseCase()
await promiseCase();
} }
expect(res).assertEqual(16); expect(res).assertEqual(16)
done();
done()
}) })
// check second worker terminate is ok // check second worker terminate is ok
it('worker_second_worker_terminate_test_001', 0, async function (done) { it('worker_second_worker_terminate_test_001', 0, async function (done) {
let ss = new worker.Worker("workers/worker_011.js"); let ss = new worker.Worker("workers/worker_011.js")
let flag = false; let flag = false
let res = undefined; let res = undefined
ss.onmessage = function (e) { ss.onmessage = function (e) {
flag = true; flag = true
res = e.data; res = e.data
} }
ss.onerror = function(ee) { ss.onerror = function(ee) {
console.log("worker:: " + ee.message) console.log("worker:: " + ee.message)
} }
ss.postMessage({type: "new", value: 12}); ss.postMessage({type: "new", value: 12})
while (!flag) { while (!flag) {
ss.postMessage({type: "wait"}); ss.postMessage({type: "wait"})
await promiseCase(); await promiseCase()
} }
flag = false; flag = false
ss.postMessage({type: "terminate"}); ss.postMessage({type: "terminate"})
while (!flag) { while (!flag) {
ss.postMessage({type: "wait"}); ss.postMessage({type: "wait"})
await promiseCase(); await promiseCase()
} }
expect(res).assertEqual("terminate"); expect(res).assertEqual("terminate")
done(); done()
}) })
}) })
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册