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 { launch } from "std/concurrency" 17import { Job } from "std/core" 18 19let isLaunchMigrated: AtomicFlag = new AtomicFlag(false); 20 21function durationFunc(duration: int): void { 22 let t = Date.now(); 23 while ((Date.now() - t) < duration) { 24 continue; 25 } 26} 27 28function delayWithYield(duration: int): void { 29 durationFunc(duration); 30 Coroutine.Schedule(); 31} 32 33async function awaitee(): Promise<boolean> { 34 return new Promise<boolean>((resolve: (v: boolean) => void) => { 35 setTimeout(() => { resolve(true); }, 10); 36 }); 37} 38 39function testLaunch(): void { 40 let wId = CoroutineExtras.getWorkerId(); 41 await awaitee(); 42 // migrate to the worker with the least load after await 43 if (wId != CoroutineExtras.getWorkerId()) { 44 isLaunchMigrated.set(true); 45 } 46} 47 48function checkLaunchMigration(): void { 49 launch<void, () => void>(testLaunch).Await(); 50} 51 52function main() { 53 CoroutineExtras.setSchedulingPolicy(CoroutineExtras.POLICY_NON_MAIN); 54 checkLaunchMigration(); 55 let res: boolean = isLaunchMigrated.get(); 56 assertEQ(res, true); 57 CoroutineExtras.setSchedulingPolicy(CoroutineExtras.POLICY_ANY); 58} 59