1/** 2 * Copyright (c) 2025 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 16import {AtomicFlag} from "std/debug/concurrency" 17 18class Event { 19 constructor() { 20 this.promise = new Promise<Number>((resolve: (val: Number) => void) => { 21 this.resolveFn = resolve; 22 }) 23 } 24 25 public Wait() { 26 await this.promise; 27 } 28 29 public Fire() { 30 this.resolveFn!(0); 31 } 32 33 private promise: Promise<Number>; 34 private resolveFn: ((val: Number) => void) | null = null; 35} 36 37 38const kTotal = 5; 39let current = 0; 40let event = new Event(); 41 42async function AsyncFailing(): Promise<string> { 43 throw Error("AsyncFailing"); 44} 45 46function Failing(): string { 47 throw Error("Failing"); 48} 49 50function WaitAll(): null { 51 event.Wait(); 52 return null; 53} 54 55function Handler(obj: Object): void { 56 if (current == 0) { 57 launch<null, () => null>(WaitAll); 58 } 59 current++; 60 if (current == kTotal) { 61 event.Fire(); 62 } else { 63 assertTrue(current < kTotal, "More rejections that expected"); 64 } 65} 66 67function main(): void { 68 StdProcess.on("unhandledJobRejection", (obj: Object): void => { 69 assertTrue(false, "Job handler didn't switch"); 70 }); 71 StdProcess.on("unhandledPromiseRejection", (obj: Object): void => { 72 assertTrue(false, "Promise handler didn't switch"); 73 }); 74 75 StdProcess.on("unhandledJobRejection", Handler); 76 StdProcess.on("unhandledPromiseRejection", Handler); 77 78 let g = launch<string, () => string>(Failing); 79 launch<Promise<string>, () => Promise<string>>(AsyncFailing); 80 AsyncFailing(); 81 let p = Promise.reject(new Error("promise1")); 82 let q = p.then(() => { 83 console.println("Test failed: Unreachable code"); 84 let procManager = new StdProcess.ProcessManager(); 85 procManager.exit(1); 86 }) 87 Promise.reject(new Error("promise2")); 88} 89