diff --git a/compileruntime/BUILD.gn b/compileruntime/BUILD.gn index 429d7ad4959e5336c7b43337fc1ce4cdf0439040..92e9d7a4546e3a1c67763e328527a1563a9fdb7c 100644 --- a/compileruntime/BUILD.gn +++ b/compileruntime/BUILD.gn @@ -21,9 +21,8 @@ group("compileruntime") { "uri_lib_standard:uri_js_test", "url_lib_standard:url_js_test", "util_lib_standard:util_js_test", + "worker_lib_standard:worker_js_hap", "xml_lib_standard:xml_js_test", - - # "worker_lib_standard:worker_js_hap", ] } } diff --git a/compileruntime/worker_lib_standard/src/main/config.json b/compileruntime/worker_lib_standard/src/main/config.json index 18665aeb48d8098fa1232be5e0195d8145ada9e7..bcb6c0fff3288358a6e0ccc71d54789a26d37d4b 100644 --- a/compileruntime/worker_lib_standard/src/main/config.json +++ b/compileruntime/worker_lib_standard/src/main/config.json @@ -5,6 +5,10 @@ "version": { "code": 1000000, "name": "1.0.0" + }, + "apiVersion": { + "compatible": 4, + "target": 5 } }, "deviceConfig": {}, diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker.js index 461c656778e9f3b27daf295f197e64b74665c54e..a91ae6a485b3bb4a03596269e2d5b26a474e90f2 100644 --- a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker.js +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker.js @@ -1,66 +1,56 @@ -/* - * Copyright (C) 2021 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. - */ - -/******/ (() => { // webpackBootstrap - var __webpack_exports__ = {}; - /*!********************************************************************************************!*\ - !*** d:\workspace\my_works\MyApplication\entry\src\main\ets\default\workers\WorkerDemo.js ***! - \********************************************************************************************/ - function foo(x) {return x} - - var worker = globalThis.requireNapi('worker'); - - const parentPort = worker.parentPort; - - console.info("worker:: new version") - - parentPort.onclose = function() { - console.info("worker::worker.js onclose"); - } - - parentPort.onmessage = function(e) { - var 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"); - break; - 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 - } - } - - // 反序列错误 - parentPort.onmessageerror = function() { - console.info("worker:: worker.js onmessageerror"); - } - - // js执行异常 - 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) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const parentPort = worker.parentPort; + +console.info("worker:: new version") + +parentPort.onclose = function() { + console.info("worker::worker.js onclose"); +} + +parentPort.onmessage = function(e) { + var 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"); + break; + 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 + } +} + +// 反序列错误 +parentPort.onmessageerror = function() { + console.info("worker:: worker.js onmessageerror"); +} + +// js执行异常 +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 diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_002.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_002.js index 5914eea6d23eb11c8582a5b24a6bbe87844757be..c5eb66b50ba130770b35ea641a0c9aded953305a 100644 --- a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_002.js +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_002.js @@ -1,32 +1,23 @@ -/* - * Copyright (C) 2021 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. - */ - -/******/ (() => { // webpackBootstrap - var __webpack_exports__ = {}; - /*!********************************************************************************************!*\ - !*** d:\workspace\my_works\MyApplication\entry\src\main\ets\default\workers\WorkerDemo.js ***! - \********************************************************************************************/ - console.log("worker:: run script"); - var worker = globalThis.requireNapi('worker'); - - const parentPort = worker.parentPort; - - parentPort.onmessage = function(e) { - console.log("worker:: worker receive data " + e.data); - var data = e.data + " worker"; - parentPort.postMessage(data) - } - /******/ })() -; \ No newline at end of file +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const parentPort = worker.parentPort; + +parentPort.onmessage = function(e) { + console.log("worker:: worker receive data " + e.data); + var data = e.data + " worker"; + parentPort.postMessage(data) +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_003.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_003.js index a680ac1788fdca88e9ca08f75ccad03accfbf692..a2f87c7de322fe6d5c8a76c310df7e082d1eaf99 100644 --- a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_003.js +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_003.js @@ -1,30 +1,22 @@ -/* - * Copyright (C) 2021 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. - */ - -/******/ (() => { // webpackBootstrap - var __webpack_exports__ = {}; - /*!********************************************************************************************!*\ - !*** d:\workspace\my_works\MyApplication\entry\src\main\ets\default\workers\WorkerDemo.js ***! - \********************************************************************************************/ - var worker = globalThis.requireNapi('worker'); - - const parentPort = worker.parentPort; - - parentPort.onmessage = function(e) { - var data = e.data * 2 + 1 - parentPort.postMessage(data) - } - /******/ })() -; \ No newline at end of file +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const parentPort = worker.parentPort; + +parentPort.onmessage = function(e) { + var data = e.data * 2 + 1 + parentPort.postMessage(data) +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_004.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_004.js index 49e4423840930b19e290e3eb4e672d0d20918527..6b29b6a2ee0ec03ceeceaac43e155c91922ecc71 100644 --- a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_004.js +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_004.js @@ -1,31 +1,23 @@ -/* - * Copyright (C) 2021 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. - */ - -/******/ (() => { // webpackBootstrap - var __webpack_exports__ = {}; - /*!********************************************************************************************!*\ - !*** d:\workspace\my_works\MyApplication\entry\src\main\ets\default\workers\WorkerDemo.js ***! - \********************************************************************************************/ - var worker = globalThis.requireNapi('worker'); - - const parentPort = worker.parentPort; - - parentPort.onmessage = function(e) { - var data = e.data; - data.message = "hello world " + "worker" - parentPort.postMessage(data) - } - /******/ })() -; \ No newline at end of file +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const parentPort = worker.parentPort; + +parentPort.onmessage = function(e) { + var data = e.data; + data.message = "hello world " + "worker" + parentPort.postMessage(data) +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_005.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_005.js index cc1cbaaad826dbd06bb862f4faa540498f2cf365..97184693b5dc15b40907cb7a7b3d8ea5aec5c806 100644 --- a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_005.js +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_005.js @@ -1,29 +1,21 @@ -/* - * Copyright (C) 2021 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. - */ - -/******/ (() => { // webpackBootstrap - var __webpack_exports__ = {}; - /*!********************************************************************************************!*\ - !*** d:\workspace\my_works\MyApplication\entry\src\main\ets\default\workers\WorkerDemo.js ***! - \********************************************************************************************/ - var worker = globalThis.requireNapi('worker'); - - const parentPort = worker.parentPort; - - parentPort.onmessage = function(e) { - parentPort.postMessage(name) - } - /******/ })() -; \ No newline at end of file +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const parentPort = worker.parentPort; + +parentPort.onmessage = function(e) { + parentPort.postMessage(name) +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_006.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_006.js index 6b9ce1e02dcf9a32709948b487861bbf2f854fb0..133518f6ca89a551bc5659c62d7196d26e98d002 100644 --- a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_006.js +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_006.js @@ -1,29 +1,21 @@ -/* - * Copyright (C) 2021 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. - */ - -/******/ (() => { // webpackBootstrap - var __webpack_exports__ = {}; - /*!********************************************************************************************!*\ - !*** d:\workspace\my_works\MyApplication\entry\src\main\ets\default\workers\WorkerDemo.js ***! - \********************************************************************************************/ - var worker = globalThis.requireNapi('worker'); - - const parentPort = worker.parentPort; - - parentPort.onmessage = function(e) { - parentPort.postMessage("hello world") - } - /******/ })() -; \ No newline at end of file +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const parentPort = worker.parentPort; + +parentPort.onmessage = function(e) { + parentPort.postMessage("hello world") +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_007.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_007.js index 9d5790347a917d7827d1b6276968e4e205734360..1f966745d18f37d2c63f5b634c961a775a40db27 100644 --- a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_007.js +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_007.js @@ -1,29 +1,21 @@ -/* - * Copyright (C) 2021 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. - */ - -/******/ (() => { // webpackBootstrap - var __webpack_exports__ = {}; - /*!********************************************************************************************!*\ - !*** d:\workspace\my_works\MyApplication\entry\src\main\ets\default\workers\WorkerDemo.js ***! - \********************************************************************************************/ - var worker = globalThis.requireNapi('worker'); - - const parentPort = worker.parentPort; - - parentPort.onmessage = function(e) { - throw new Error(123) - } - /******/ })() -; \ No newline at end of file +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const parentPort = worker.parentPort; + +parentPort.onmessage = function(e) { + throw new Error(123) +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_008.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_008.js index 02d4825b98377c83d4bbc9e8a8542b7dea6fd262..1bede55995cccd698ea5f53226d84f24e32abe6b 100644 --- a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_008.js +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_008.js @@ -1,29 +1,21 @@ -/* - * Copyright (C) 2021 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. - */ - -/******/ (() => { // webpackBootstrap - var __webpack_exports__ = {}; - /*!********************************************************************************************!*\ - !*** d:\workspace\my_works\MyApplication\entry\src\main\ets\default\workers\WorkerDemo.js ***! - \********************************************************************************************/ - var worker = globalThis.requireNapi('worker'); - - const parentPort = worker.parentPort; - - parentPort.onmessage = function(e) { - parentPort.close() - } - /******/ })() -; \ No newline at end of file +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import worker from '@ohos.worker'; +const parentPort = worker.parentPort; + +parentPort.onmessage = function(e) { + parentPort.close() +} diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_009.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_009.js new file mode 100644 index 0000000000000000000000000000000000000000..9e6af87ecebd81fb22725118e2a2b4a3c7c8ea00 --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_009.js @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +var ss = undefined; + +parentPort.onmessage = function(e) { + var data = e.data; + switch(data.type) { + case "new": + ss = new worker.Worker("workers/worker_0091.js"); + console.log("worker:: workerxx "); + parentPort.postMessage(ss != null); + break; + default: + break; + } +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/test/ExampleJsunit.test.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0091.js similarity index 62% rename from compileruntime/worker_lib_standard/src/main/js/test/ExampleJsunit.test.js rename to compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0091.js index d289d77a014adff89086f354807f6caac9c317b7..88dec2cfaf8ac4d4385fa66a3c163df82f08ca6e 100644 --- a/compileruntime/worker_lib_standard/src/main/js/test/ExampleJsunit.test.js +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0091.js @@ -12,14 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import worker from "@ohos.worker" -import app from '@system.app' -import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' +const parentPort = worker.parentPort; -describe('appInfoTest', function () { - it('app_info_test_001', 0, function () { - var info = app.getInfo() - expect(info.versionName).assertEqual('1.0.0') - expect(info.versionCode).assertEqual('1000000') - }) -}) \ No newline at end of file +parentPort.onmessage = function(e) { + console.log("worker:: onmessage"); +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_010.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_010.js new file mode 100644 index 0000000000000000000000000000000000000000..4b2fe2bab92b0cdc84a866967270b207e0982b93 --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_010.js @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +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.Worker("workers/worker_0101.js"); + ss.onmessage = function(ee) { + backValue = ee.data; + flag = true; + } + ss.postMessage(data.value); + break; + case "wait": + if (flag) { + parentPort.postMessage(backValue); + } + break; + default: + break; + } +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0101.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0101.js new file mode 100644 index 0000000000000000000000000000000000000000..3e3ac36797a0e785eadfe98a644f18c8afa3943d --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0101.js @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +parentPort.onmessage = function(e) { + var data = e.data; // 12 + parentPort.postMessage(data * 2 + 1); // 25 +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_011.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_011.js new file mode 100644 index 0000000000000000000000000000000000000000..7fa824a29f831676a94499eefbbf16f237adb6af --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_011.js @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +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.Worker("workers/worker_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 diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0111.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0111.js new file mode 100644 index 0000000000000000000000000000000000000000..f942d2040ec412a8351406acea745b0d864b3c24 --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0111.js @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +parentPort.onmessage = function(e) { + console.log("worker:: onmessage") + parentPort.postMessage("terminate"); +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_012.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_012.js new file mode 100644 index 0000000000000000000000000000000000000000..30acbbe7fe142adf91fb4181b36820b65940ea65 --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_012.js @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +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.Worker("workers/worker_0121.js"); + ss.onmessage = function(ee) { + backValue = ee.data; + flag = true; + } + ss.postMessage({type: "new"}); + break; + case "wait": + if (flag) { + parentPort.postMessage(backValue); + } + break; + default: + break; + } +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0121.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0121.js new file mode 100644 index 0000000000000000000000000000000000000000..7ad306fb78d34919c1757bc7d0dbe7a991914d6f --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0121.js @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +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.Worker("workers/worker_0122.js"); + parentPort.postMessage(ss != null) + break; + default: + break; + } +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0122.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0122.js new file mode 100644 index 0000000000000000000000000000000000000000..92b3f38920fd009ff578234e45fdd59a53223f48 --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0122.js @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +var ss = undefined; +var backValue = undefined; +var flag = false; + +parentPort.onmessage = function(e) { + console.log("worker:: thrid worker onmessage") +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_013.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_013.js new file mode 100644 index 0000000000000000000000000000000000000000..a80bc24eecfde0eebb7fa6e364ed4ab7f7a0a06a --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_013.js @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +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.Worker("workers/worker_0131.js"); + ss.onmessage = function(ee) { + backValue = ee.data; + flag = true; + } + ss.postMessage(data.value); + break; + case "wait": + if (flag) { + parentPort.postMessage(backValue); + } + break; + default: + break; + } +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0131.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0131.js new file mode 100644 index 0000000000000000000000000000000000000000..811ef958ac51c4dd6640e1d8c36491eaeae96965 --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0131.js @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +parentPort.onmessage = function(e) { + var data = e.data; // hello world + parentPort.postMessage(data + " worker"); // hello world worker +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_014.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_014.js new file mode 100644 index 0000000000000000000000000000000000000000..a58f6c5b4e5fea3bb174f2beb9deb3468037d4df --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_014.js @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +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.Worker("workers/worker_0141.js"); + ss.onmessage = function(ee) { + backValue = ee.data; + flag = true; + } + ss.postMessage(data.value); + break; + case "wait": + if (flag) { + parentPort.postMessage(backValue); + } + break; + default: + break; + } +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0141.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0141.js new file mode 100644 index 0000000000000000000000000000000000000000..2cc95a09575fb868197605287d8fb549bd4d0048 --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0141.js @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +parentPort.onmessage = function(e) { + var data = e.data; + data[0] += 1 + parentPort.postMessage(data); // [2, 2] +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_015.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_015.js new file mode 100644 index 0000000000000000000000000000000000000000..5f18b2d4682e085f52ccde66cc0f1a69a8a8ea70 --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_015.js @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +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.Worker("workers/worker_0151.js"); + 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) { + parentPort.postMessage(backValue); // 16 + } + break; + default: + break; + } +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0151.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0151.js new file mode 100644 index 0000000000000000000000000000000000000000..d1114c5d4bc484c215fec4d47717a1440ca44f1f --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0151.js @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +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.Worker("workers/worker_0152.js"); + ss.onmessage = function(ee) { + backValue = ee.data; + flag = true; + } + ss.postMessage(data.value + 2); // 13 + break; + case "wait": + if (flag) { + parentPort.postMessage(backValue); // 16 + } + break; + default: + break; + } +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0152.js b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0152.js new file mode 100644 index 0000000000000000000000000000000000000000..eb22d7afbc5a47808ae6e3d28e3c1a2a77cbab7f --- /dev/null +++ b/compileruntime/worker_lib_standard/src/main/js/default/workers/worker_0152.js @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import worker from "@ohos.worker" + +const parentPort = worker.parentPort; + +parentPort.onmessage = function(e) { + var data = e.data; + parentPort.postMessage(data + 3); // 16 +} \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/test/List.test.js b/compileruntime/worker_lib_standard/src/main/js/test/List.test.js index f0714ab28b031c9721288cfc773fb9cd5a9133f6..6a95903f18c4065ec3a9458ffbca47c6c5a7b837 100644 --- a/compileruntime/worker_lib_standard/src/main/js/test/List.test.js +++ b/compileruntime/worker_lib_standard/src/main/js/test/List.test.js @@ -1,17 +1,16 @@ -/* - * Copyright (C) 2021 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. - */ - -require('./ExampleJsunit.test.js') +/* + * Copyright (C) 2021 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. + */ + require("./Worker.test.js") \ No newline at end of file diff --git a/compileruntime/worker_lib_standard/src/main/js/test/Worker.test.js b/compileruntime/worker_lib_standard/src/main/js/test/Worker.test.js index 0a3f9099b69d5a97f3cbc5458afb15c268ef46e4..245be48e7d164221f7c5c9521cb7d4c4cda51e09 100644 --- a/compileruntime/worker_lib_standard/src/main/js/test/Worker.test.js +++ b/compileruntime/worker_lib_standard/src/main/js/test/Worker.test.js @@ -1,628 +1,780 @@ -/* - * Copyright (C) 2021 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 {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' -//import worker from "@ohos.worker"; -var worker = globalThis.requireNapi('worker'); - -describe('workerTest', 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 - it('worker_constructor_test_001', 0, function () { - var ss = new worker.Worker("workers/worker.js") - expect(ss != null).assertTrue() - }) - - it('worker_constructor_test_002', 0, function () { - var ss = new worker.Worker("workers/worker.js",{name:"second worker",shared:"False"}) - expect(ss != null).assertTrue() - }) - - it('worker_constructor_test_003', 0, function () { - var ss = new worker.Worker("workers/worker.js",{type:"classic",name:"second worker",shared:"false"}) - expect(ss != null).assertTrue() - }) - - it('worker_constructor_test_004', 0, function () { - var ss = new worker.Worker("workers/worker.js",{type:"classic",name:"third worker",shared:"true"}) - expect(ss != null).assertTrue() - }) - - // check postMessage is ok - // main post "hello world", will receive "hello world worker" - it('worker_postMessage_test_001', 0, async function (done) { - var ss = new worker.Worker("workers/worker_002.js"); - - var res = undefined - var flag = false - - ss.onmessage = function (e) { - res = e.data; - flag = true - } - - ss.postMessage("hello world") - while (!flag) { - await promiseCase() - } - expect(res).assertEqual("hello world worker") - done() - }) - - // check postMessage is ok - // main post 12 , will receive 12 * 2 + 1 - it('worker_postMessage_test_002', 0, async function (done) { - var ss = new worker.Worker("workers/worker_003.js"); - - var res = undefined - var flag = false - - ss.onmessage = function (e) { - res = e.data; - flag = true - } - - ss.postMessage(12) - while (!flag) { - await promiseCase() - } - expect(res).assertEqual(25) - done() - }) - - // check postMessage is ok - // main post {message:"hello world"} , will receive {message:"hello world worker"} - it('worker_postMessage_test_003', 0, async function (done) { - var ss = new worker.Worker("workers/worker_004.js"); - - var res = undefined - var flag = false - - ss.onmessage = function (e) { - res = e.data.message; - flag = true - } - - ss.postMessage({"message":"hello world"}) - while (!flag) { - await promiseCase() - } - expect(res).assertEqual("hello world worker") - done() - }) - - // check worker name is ok - it('worker_postMessage_test_004', 0, async function (done) { - var ss = new worker.Worker("workers/worker_005.js", {name: "zhangsan"}); - - var res = undefined - var flag = false - - ss.onmessage = function (e) { - res = e.data; - flag = true - } - - ss.postMessage("hello world") - while (!flag) { - await promiseCase() - } - expect(res).assertEqual("zhangsan") - done() - }) - - // check worker transfer buffer is ok - it('worker_postMessage_test_005', 0, function () { - var ss = new worker.Worker("workers/worker_006.js"); - - const buffer = new ArrayBuffer(8) - expect(buffer.byteLength).assertEqual(8) - ss.postMessage(buffer, [buffer]) - expect(buffer.byteLength).assertEqual(0) - }) - - // check worker handle error is ok - it('worker_postMessage_test_006', 0, async function (done) { - var ss = new worker.Worker("workers/worker_007.js"); - - var res = undefined - var flag = false - - ss.onerror = function (e) { - res = e.message; - flag = true - } - - ss.postMessage("hello world") - while (!flag) { - await promiseCase() - } - expect(res).assertEqual("Error: 123") - done() - }) - - // check worker terminate is ok - it('worker_terminate_test_001', 0, async function (done) { - var ss = new worker.Worker("workers/worker.js"); - var res = 0 - var flag = false - - ss.onexit = function () { - flag = true - res++; - } - ss.terminate() - while (!flag) { - await promiseCase() - } - expect(res).assertEqual(1) - done() - }) - - // check worker terminate is ok - it('worker_terminate_test_002', 0, async function (done) { - var ss = new worker.Worker("workers/worker.js"); - var res = 0 - var 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 - it('worker_terminate_test_003', 0, async function (done) { - var ss = new worker.Worker("workers/worker.js"); - var res = 0 - var flag = false - - ss.onexit = function () { - flag = true - res++; - } - - ss.onmessage = function (e) { - res++; - } - - ss.terminate() - while (!flag) { - await promiseCase() - } - expect(res).assertEqual(1) - - ss.postMessage("hello world") - await promiseCase() - expect(res).assertEqual(1) - done() - }) - - // check worker on function is ok - it('worker_on_test_001', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var times = 0; - ss.on("zhangsan", ()=>{ - times++; - }) - - ss.dispatchEvent({type: "zhangsan"}) - - expect(times).assertEqual(1) - }) - - // check worker on function is ok - it('worker_on_test_002', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var times = 0; - ss.on("zhangsan", ()=>{ - times--; - }) - - ss.on("lisi", ()=>{ - times++; - }) - - ss.dispatchEvent({type: "zhangsan"}) - ss.dispatchEvent({type: "lisi"}) - - expect(times).assertEqual(0) - }) - - // check worker on function is ok - it('worker_on_test_003', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var times = 0; - ss.on("zhangsan", ()=>{ - times++; - }) - - for (var i=0;i<10;i++) - { - ss.dispatchEvent({type: "zhangsan"}) - } - console.log("times " + times); - expect(times).assertEqual(10) - }) - - // check worker once function is ok - it('worker_once_test_001', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var times = 0; - ss.once("zhangsan", ()=>{ - times++; - }) - ss.dispatchEvent({type: "zhangsan"}) - expect(times).assertEqual(1) - }) - - // check worker once function is ok - it('worker_once_test_002', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var times = 0; - ss.once("zhangsan", ()=>{ - times++; - }) - - for (var i=0;i<10;i++) - { - ss.dispatchEvent({type: "zhangsan"}) - } - - expect(times).assertEqual(1) - }) - - it('worker_once_test_003', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var times = 0; - ss.once("zhangsan", ()=>{ - times--; - }) - - ss.once("lisi", ()=>{ - times++; - }) - - ss.dispatchEvent({type: "zhangsan"}) - ss.dispatchEvent({type: "lisi"}) - - expect(times).assertEqual(0) - }) - - // check worker addEventListener function is ok - it('worker_addEventListener_test_001', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var times = 0; - ss.addEventListener("zhangsan", ()=>{ - times++; - }) - - ss.dispatchEvent({type: "zhangsan"}) - ss.dispatchEvent({type: "zhangsan"}) - - expect(times).assertEqual(2) - }) - - // check worker addEventListener function is ok - it('worker_addEventListener_test_002', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var times = 0; - ss.addEventListener("zhangsan", ()=>{ - times++; - }) - - ss.addEventListener("lisi", ()=>{ - times++; - }) - - ss.dispatchEvent({type: "zhangsan"}) - ss.dispatchEvent({type: "lisi"}) - - expect(times).assertEqual(2) - }) - - // check worker addEventListener function is ok - it('worker_addEventListener_test_003', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var times = 0; - ss.addEventListener("zhangsan", ()=>{ - times++; - }) - - for (var i=0;i<10;i++) - { - ss.dispatchEvent({type: "zhangsan"}) - } - - expect(times).assertEqual(10) - }) - - // check worker off function is ok - it('worker_off_test_001', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var zhangsan_times = 0; - ss.on("zhangsan", ()=>{ - zhangsan_times++; - }) - - ss.dispatchEvent({type: "zhangsan"}) - expect(zhangsan_times).assertEqual(1) - - ss.off("zhangsan") - - ss.dispatchEvent({type: "zhangsan"}) - ss.dispatchEvent({type: "zhangsan"}) - expect(zhangsan_times).assertEqual(1) - }) - - // check worker off function is ok - it('worker_off_test_002', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var zhangsan_times = 0; - ss.on("zhangsan", ()=>{ - zhangsan_times++; - }) - - ss.dispatchEvent({type: "zhangsan"}) - ss.dispatchEvent({type: "zhangsan"}) - expect(zhangsan_times).assertEqual(2) - - for (var i=0;i<3;i++) - { - ss.off("zhangsan") - } - - ss.dispatchEvent({type: "zhangsan"}) - expect(zhangsan_times).assertEqual(2) - }) - - // check worker removeEventListener function is ok - it('worker_removeListener_test_001', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var zhangsan_times = 0; - ss.addEventListener("zhangsan", ()=>{ - zhangsan_times++; - }) - - ss.dispatchEvent({type: "zhangsan"}) - expect(zhangsan_times).assertEqual(1) - - ss.removeEventListener("zhangsan") - - ss.dispatchEvent({type: "zhangsan"}) - ss.dispatchEvent({type: "zhangsan"}) - expect(zhangsan_times).assertEqual(1) - }) - - // check worker removeEventListener function is ok - it('worker_removeListener_test_002', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var zhangsan_times = 0; - ss.addEventListener("zhangsan", ()=>{ - zhangsan_times++; - }) - - ss.dispatchEvent({type: "zhangsan"}) - ss.dispatchEvent({type: "zhangsan"}) - expect(zhangsan_times).assertEqual(2) - - for (var i=0;i<3;i++) - { - ss.removeEventListener("zhangsan") - } - - ss.dispatchEvent({type: "zhangsan"}) - expect(zhangsan_times).assertEqual(2) - }) - - // check worker removeAllListener function is ok - it('worker_removeListener_test_003', 0, function () { - var ss = new worker.Worker("workers/worker.js"); - - var zhangsan_times = 0; - ss.addEventListener("zhangsan", ()=>{ - zhangsan_times++; - }) - - var lisi_times = 0; - ss.addEventListener("lisi", ()=>{ - lisi_times++; - }) - - ss.dispatchEvent({type: "zhangsan"}) - ss.dispatchEvent({type: "zhangsan"}) - expect(zhangsan_times).assertEqual(2) - - ss.dispatchEvent({type: "lisi"}) - ss.dispatchEvent({type: "lisi"}) - expect(lisi_times).assertEqual(2) - - ss.removeAllListener() - ss.dispatchEvent({type: "zhangsan"}) - ss.dispatchEvent({type: "zhangsan"}) - expect(zhangsan_times).assertEqual(2) - - ss.dispatchEvent({type: "lisi"}) - ss.dispatchEvent({type: "lisi"}) - expect(lisi_times).assertEqual(2) - }) - - // check parentPort.close is ok - it('worker_parentPortClose_test_001', 0, async function (done) { - var ss = new worker.Worker("workers/worker_008.js"); - var res = 0 - - ss.onmessage = function (e) { - res++; - } - - ss.postMessage("abc") - await promiseCase() - expect(res).assertEqual(0) - done() - }) - - // check parentPort.close is ok - it('worker_parentPortClose_test_002', 0, async function (done) { - var ss1 = new worker.Worker("workers/worker_008.js"); - var ss2 = new worker.Worker("workers/worker_008.js"); - var res = 0 - var times = 0 - - ss1.onmessage = function (e) { - res++; - } - - ss1.postMessage("abc") - await promiseCase() - expect(res).assertEqual(0) - - ss2.postMessage("hello world") - await promiseCase() - expect(res).assertEqual(0) - - done() - }) - - // check parentPort.close is ok - it('worker_parentPortClose_test_003', 0, async function (done) { - var ss1 = new worker.Worker("workers/worker_008.js"); - var ss2 = new worker.Worker("workers/worker_008.js"); - var res = 0 - var times = 0 - - ss1.onmessage = function (e) { - res++; - } - - ss1.postMessage("abc") - await promiseCase() - expect(res).assertEqual(0) - - ss2.addEventListener("zhangsan", ()=>{ - times++; - }) - - ss2.dispatchEvent({type: "zhangsan"}) - expect(times).assertEqual(1) - done() - }) - - // check parentPort.close is ok - it('worker_parentPortClose_test_004', 0, async function (done) { - var ss = new worker.Worker("workers/worker_008.js"); - var res = 0 - var flag = false; - - ss.onexit = function () { - flag = true - } - - ss.onmessage = function (e) { - res++; - } - - ss.postMessage("abc") - while (!flag) { - await promiseCase() - } - - ss.postMessage("hello") - await promiseCase() - expect(res).assertEqual(0) - done() - }) - - // check onmessageerror is ok - it('worker_onmessageerror_test_001', 0, async function (done) { - var ss = new worker.Worker("workers/worker_008.js"); - var res = 0 - var 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 - it('worker_onmessageerror_test_002', 0, async function (done) { - var ss = new worker.Worker("workers/worker_008.js"); - var res = 0 - var flag = false; - - ss.onmessageerror = function (e) { - flag = true; - res++; - } - function foo() { - } - ss.postMessage(foo) - while (!flag) { - await promiseCase() - } - expect(res).assertEqual(1) - done() - }) +/* + * Copyright (C) 2021 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 {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' +import worker from "@ohos.worker"; + +describe('workerTest', 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 + it('worker_constructor_test_001', 0, function () { + var ss = new worker.Worker("workers/worker.js") + expect(ss != null).assertTrue() + }) + + it('worker_constructor_test_002', 0, function () { + var ss = new worker.Worker("workers/worker.js",{name:"second worker",shared:"False"}) + expect(ss != null).assertTrue() + }) + + it('worker_constructor_test_003', 0, function () { + var ss = new worker.Worker("workers/worker.js",{type:"classic",name:"second worker",shared:"false"}) + expect(ss != null).assertTrue() + }) + + it('worker_constructor_test_004', 0, function () { + var ss = new worker.Worker("workers/worker.js",{type:"classic",name:"third worker",shared:"true"}) + expect(ss != null).assertTrue() + }) + + // check postMessage is ok + // main post "hello world", will receive "hello world worker" + it('worker_postMessage_test_001', 0, async function (done) { + var ss = new worker.Worker("workers/worker_002.js"); + + var res = undefined + var flag = false + + ss.onmessage = function (e) { + res = e.data; + flag = true + } + + ss.postMessage("hello world") + while (!flag) { + await promiseCase() + } + expect(res).assertEqual("hello world worker") + done() + }) + + // check postMessage is ok + // main post 12 , will receive 12 * 2 + 1 + it('worker_postMessage_test_002', 0, async function (done) { + var ss = new worker.Worker("workers/worker_003.js"); + + var res = undefined + var flag = false + + ss.onmessage = function (e) { + res = e.data; + flag = true + } + + ss.postMessage(12) + while (!flag) { + await promiseCase() + } + expect(res).assertEqual(25) + done() + }) + + // check postMessage is ok + // main post {message:"hello world"} , will receive {message:"hello world worker"} + it('worker_postMessage_test_003', 0, async function (done) { + var ss = new worker.Worker("workers/worker_004.js"); + + var res = undefined + var flag = false + + ss.onmessage = function (e) { + res = e.data.message; + flag = true + } + + ss.postMessage({"message":"hello world"}) + while (!flag) { + await promiseCase() + } + expect(res).assertEqual("hello world worker") + done() + }) + + // check worker name is ok + it('worker_postMessage_test_004', 0, async function (done) { + var ss = new worker.Worker("workers/worker_005.js", {name: "zhangsan"}); + + var res = undefined + var flag = false + + ss.onmessage = function (e) { + res = e.data; + flag = true + } + + ss.postMessage("hello world") + while (!flag) { + await promiseCase() + } + expect(res).assertEqual("zhangsan") + done() + }) + + // check worker transfer buffer is ok + it('worker_postMessage_test_005', 0, function () { + var ss = new worker.Worker("workers/worker_006.js"); + + const buffer = new ArrayBuffer(8) + expect(buffer.byteLength).assertEqual(8) + ss.postMessage(buffer, [buffer]) + var length = undefined; + var exception = undefined; + try { + length = buffer.byteLength; + } catch (e) { + exception = e.message; + } + + 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") + } + }) + + // check worker handle error is ok + it('worker_postMessage_test_006', 0, async function (done) { + var ss = new worker.Worker("workers/worker_007.js"); + + var res = undefined + var flag = false + + ss.onerror = function (e) { + res = e.message; + flag = true + } + + ss.postMessage("hello world") + while (!flag) { + await promiseCase() + } + expect(res).assertEqual("Error: 123") + done() + }) + + // check worker terminate is ok + it('worker_terminate_test_001', 0, async function (done) { + var ss = new worker.Worker("workers/worker.js"); + var res = 0 + var flag = false + + ss.onexit = function () { + flag = true + res++; + } + ss.terminate() + while (!flag) { + await promiseCase() + } + expect(res).assertEqual(1) + done() + }) + + // check worker terminate is ok + it('worker_terminate_test_002', 0, async function (done) { + var ss = new worker.Worker("workers/worker.js"); + var res = 0 + var 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 + it('worker_terminate_test_003', 0, async function (done) { + var ss = new worker.Worker("workers/worker.js"); + var res = 0 + var flag = false + + ss.onexit = function () { + flag = true + res++; + } + + ss.onmessage = function (e) { + res++; + } + + ss.terminate() + while (!flag) { + await promiseCase() + } + expect(res).assertEqual(1) + + ss.postMessage("hello world") + await promiseCase() + expect(res).assertEqual(1) + done() + }) + + // check worker on function is ok + it('worker_on_test_001', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var times = 0; + ss.on("zhangsan", ()=>{ + times++; + }) + + ss.dispatchEvent({type: "zhangsan"}) + + expect(times).assertEqual(1) + }) + + // check worker on function is ok + it('worker_on_test_002', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var times = 0; + ss.on("zhangsan", ()=>{ + times--; + }) + + ss.on("lisi", ()=>{ + times++; + }) + + ss.dispatchEvent({type: "zhangsan"}) + ss.dispatchEvent({type: "lisi"}) + + expect(times).assertEqual(0) + }) + + // check worker on function is ok + it('worker_on_test_003', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var times = 0; + ss.on("zhangsan", ()=>{ + times++; + }) + + for (var i=0;i<10;i++) + { + ss.dispatchEvent({type: "zhangsan"}) + } + console.log("times " + times); + expect(times).assertEqual(10) + }) + + // check worker once function is ok + it('worker_once_test_001', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var times = 0; + ss.once("zhangsan", ()=>{ + times++; + }) + ss.dispatchEvent({type: "zhangsan"}) + expect(times).assertEqual(1) + }) + + // check worker once function is ok + it('worker_once_test_002', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var times = 0; + ss.once("zhangsan", ()=>{ + times++; + }) + + for (var i=0;i<10;i++) + { + ss.dispatchEvent({type: "zhangsan"}) + } + + expect(times).assertEqual(1) + }) + + it('worker_once_test_003', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var times = 0; + ss.once("zhangsan", ()=>{ + times--; + }) + + ss.once("lisi", ()=>{ + times++; + }) + + ss.dispatchEvent({type: "zhangsan"}) + ss.dispatchEvent({type: "lisi"}) + + expect(times).assertEqual(0) + }) + + // check worker addEventListener function is ok + it('worker_addEventListener_test_001', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var times = 0; + ss.addEventListener("zhangsan", ()=>{ + times++; + }) + + ss.dispatchEvent({type: "zhangsan"}) + ss.dispatchEvent({type: "zhangsan"}) + + expect(times).assertEqual(2) + }) + + // check worker addEventListener function is ok + it('worker_addEventListener_test_002', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var times = 0; + ss.addEventListener("zhangsan", ()=>{ + times++; + }) + + ss.addEventListener("lisi", ()=>{ + times++; + }) + + ss.dispatchEvent({type: "zhangsan"}) + ss.dispatchEvent({type: "lisi"}) + + expect(times).assertEqual(2) + }) + + // check worker addEventListener function is ok + it('worker_addEventListener_test_003', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var times = 0; + ss.addEventListener("zhangsan", ()=>{ + times++; + }) + + for (var i=0;i<10;i++) + { + ss.dispatchEvent({type: "zhangsan"}) + } + + expect(times).assertEqual(10) + }) + + // check worker off function is ok + it('worker_off_test_001', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var zhangsan_times = 0; + ss.on("zhangsan", ()=>{ + zhangsan_times++; + }) + + ss.dispatchEvent({type: "zhangsan"}) + expect(zhangsan_times).assertEqual(1) + + ss.off("zhangsan") + + ss.dispatchEvent({type: "zhangsan"}) + ss.dispatchEvent({type: "zhangsan"}) + expect(zhangsan_times).assertEqual(1) + }) + + // check worker off function is ok + it('worker_off_test_002', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var zhangsan_times = 0; + ss.on("zhangsan", ()=>{ + zhangsan_times++; + }) + + ss.dispatchEvent({type: "zhangsan"}) + ss.dispatchEvent({type: "zhangsan"}) + expect(zhangsan_times).assertEqual(2) + + for (var i=0;i<3;i++) + { + ss.off("zhangsan") + } + + ss.dispatchEvent({type: "zhangsan"}) + expect(zhangsan_times).assertEqual(2) + }) + + // check worker removeEventListener function is ok + it('worker_removeListener_test_001', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var zhangsan_times = 0; + ss.addEventListener("zhangsan", ()=>{ + zhangsan_times++; + }) + + ss.dispatchEvent({type: "zhangsan"}) + expect(zhangsan_times).assertEqual(1) + + ss.removeEventListener("zhangsan") + + ss.dispatchEvent({type: "zhangsan"}) + ss.dispatchEvent({type: "zhangsan"}) + expect(zhangsan_times).assertEqual(1) + }) + + // check worker removeEventListener function is ok + it('worker_removeListener_test_002', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var zhangsan_times = 0; + ss.addEventListener("zhangsan", ()=>{ + zhangsan_times++; + }) + + ss.dispatchEvent({type: "zhangsan"}) + ss.dispatchEvent({type: "zhangsan"}) + expect(zhangsan_times).assertEqual(2) + + for (var i=0;i<3;i++) + { + ss.removeEventListener("zhangsan") + } + + ss.dispatchEvent({type: "zhangsan"}) + expect(zhangsan_times).assertEqual(2) + + }) + + // check worker removeAllListener function is ok + it('worker_removeListener_test_003', 0, function () { + var ss = new worker.Worker("workers/worker.js"); + + var zhangsan_times = 0; + ss.addEventListener("zhangsan", ()=>{ + zhangsan_times++; + }) + + var lisi_times = 0; + ss.addEventListener("lisi", ()=>{ + lisi_times++; + }) + + ss.dispatchEvent({type: "zhangsan"}) + ss.dispatchEvent({type: "zhangsan"}) + expect(zhangsan_times).assertEqual(2) + + ss.dispatchEvent({type: "lisi"}) + ss.dispatchEvent({type: "lisi"}) + expect(lisi_times).assertEqual(2) + + ss.removeAllListener() + ss.dispatchEvent({type: "zhangsan"}) + ss.dispatchEvent({type: "zhangsan"}) + expect(zhangsan_times).assertEqual(2) + + ss.dispatchEvent({type: "lisi"}) + ss.dispatchEvent({type: "lisi"}) + expect(lisi_times).assertEqual(2) + }) + + // check parentPort.close is ok + it('worker_parentPortClose_test_001', 0, async function (done) { + var ss = new worker.Worker("workers/worker_008.js"); + var res = 0 + var 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 + it('worker_parentPortClose_test_002', 0, async function (done) { + var ss = new worker.Worker("workers/worker_008.js"); + var res = 0 + var flag = false; + + ss.onexit = function () { + flag = true + } + + ss.onmessage = function (e) { + res++; + } + + ss.postMessage("abc") + while (!flag) { + await promiseCase() + } + + ss.postMessage("hello") + await promiseCase() + expect(res).assertEqual(0) + done() + }) + + // check onmessageerror is ok + it('worker_onmessageerror_test_001', 0, async function (done) { + var ss = new worker.Worker("workers/worker_008.js"); + var res = 0 + var 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 + it('worker_onmessageerror_test_002', 0, async function (done) { + var ss = new worker.Worker("workers/worker_008.js"); + var res = 0 + var flag = false; + + ss.onmessageerror = function (e) { + flag = true; + res++; + } + function foo() { + } + ss.postMessage(foo) + while (!flag) { + await promiseCase() + } + expect(res).assertEqual(1) + done() + }) + + // check new second worker is ok + it('worker_new_second_worker_test_001', 0, async function (done) { + var ss = new worker.Worker("workers/worker_009.js"); + var flag = false; + var res = undefined; + + 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(); + done(); + }) + + // check new third worker is ok + it('worker_new_second_worker_test_002', 0, async function (done) { + var ss = new worker.Worker("workers/worker_012.js"); + var flag = false; + var res = undefined; + + 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) { + console.log("worker:: worker_second_worker_postMessage_test_001 wait"); + ss.postMessage({type: "wait"}); + await promiseCase(); + } + expect(res).assertTrue(); + done(); + }) + + // check second worker postMessage number is ok + it('worker_second_worker_postMessage_test_001', 0, async function (done) { + var ss = new worker.Worker("workers/worker_010.js"); + var flag = false; + var res = undefined; + + 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) { + console.log("worker:: worker_second_worker_postMessage_test_001 wait"); + ss.postMessage({type: "wait"}); + await promiseCase(); + } + expect(res).assertEqual(25); + done(); + }) + + // check second worker postMessage string is ok + it('worker_second_worker_postMessage_test_002', 0, async function (done) { + var ss = new worker.Worker("workers/worker_013.js"); + var flag = false; + var res = undefined; + + 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) { + console.log("worker:: worker_second_worker_postMessage_test_002 wait") + ss.postMessage({type: "wait"}); + await promiseCase(); + } + expect(res).assertEqual("hello world worker"); + done(); + }) + + // check second worker postMessage array is ok + it('worker_second_worker_postMessage_test_003', 0, async function (done) { + var ss = new worker.Worker("workers/worker_014.js"); + var flag = false; + var res = undefined; + + 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) { + console.log("worker:: worker_second_worker_postMessage_test_003 wait") + ss.postMessage({type: "wait"}); + await promiseCase(); + } + expect(res[0]).assertEqual(2); + expect(res[1]).assertEqual(2); + done(); + }) + + // check third worker postMessage is ok + it('worker_second_worker_postMessage_test_004', 0, async function (done) { + var ss = new worker.Worker("workers/worker_015.js"); + var flag = false; + var res = undefined; + + 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) { + console.log("worker:: worker_second_worker_postMessage_test_003 wait") + ss.postMessage({type: "wait"}); + await promiseCase(); + } + expect(res).assertEqual(16); + done(); + }) + + // check second worker terminate is ok + it('worker_second_worker_terminate_test_001', 0, async function (done) { + var ss = new worker.Worker("workers/worker_011.js"); + var flag = false; + var res = undefined; + + 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(); + } + + expect(res).assertEqual("terminate"); + done(); + }) }) \ No newline at end of file