1/* 2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16function return_value(i: int): String { 17 return "value " + i; 18} 19 20function n_cor_launch(n_cor: int): int { 21 let p: NullablePromise<String>[] = new NullablePromise<String>[n_cor]; 22 for (let i = 0; i < n_cor; ++i) { 23 p[i] = launch return_value(i); 24 } 25 for (let i = 0; i < n_cor; ++i) { 26 let val = await p[i]!; 27 if(val != return_value(i)) { 28 console.println("p[" + i + "] = \"" + val + "\" instead of \"" + return_value(i) + "\""); 29 return 1; 30 } 31 } 32 return 0; 33} 34 35function throw_exception(): Object { 36 throw new Error(); 37} 38 39function n_cor_exection_launch(n_cor: int): int { 40 let p: NullablePromise<Object>[] = new NullablePromise<Object>[n_cor]; 41 for (let i = 0; i < n_cor; ++i) { 42 p[i] = launch throw_exception(); 43 } 44 for (let i = 0; i < n_cor; ++i) { 45 try { 46 await p[i]! 47 } catch (e) { 48 if (!(e instanceof Error)) { 49 console.println("Expected Error but another exception has been thrown!: " + e); 50 return 1; 51 } 52 continue; 53 } 54 console.println("Was no error throwing"); 55 return 1; 56 } 57 return 0; 58} 59 60// !1581, molotkovmikhail - test should be start with coroutine pool to test it 61export function main(): int { 62 // Create 5 coroutines and save 5 coroutines 63 if(n_cor_launch(5) != 0) { 64 console.println("First launch of 5 coroutines failed!!"); 65 return -1; 66 } 67 // Reuse 5 coroutines 68 if(n_cor_launch(5) != 0) { 69 console.println("Second launch of 5 coroutines failed!!"); 70 return -1; 71 } 72 // Create 10 coroutines: reuse 5 coroutines and create new 5 ones. Save 10 coroutines 73 if(n_cor_launch(10) != 0) { 74 console.println("First launch of 10 coroutines failed!!"); 75 return -1; 76 } 77 // Reuse 10 coroutines 78 if(n_cor_launch(10) != 0) { 79 console.println("Second launch of 10 coroutines failed!!"); 80 return -1; 81 } 82 // Reuse 5 coroutines and throw exception in them 83 if(n_cor_exection_launch(5) != 0) { 84 console.println("First launch of 5 throw-error coroutines failed!!"); 85 return -1; 86 } 87 // Reuse 10 coroutines 88 if(n_cor_launch(10) != 0) { 89 console.println("First launch of 10 coroutines after 5 throw-error ones failed!!"); 90 return -1; 91 } 92 return 0; 93} 94