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 16import {Logger as L} from "std/debug" 17import {CoroutineExtras, AtomicFlag} from "std/debug/concurrency" 18 19let continue_execution: AtomicFlag = new AtomicFlag(false); 20 21async function async_coro(): Promise<Int> { 22 return CoroutineExtras.getWorkerId(); 23} 24 25function check_async_id(): int { 26 let id_current: int = CoroutineExtras.getWorkerId(); 27 let id_coro: int = (await async_coro()) as int; 28 if (id_current != id_coro) { 29 return 1; 30 } else { 31 return 0; 32 } 33} 34 35function checker(id_main: int): Int { 36 let id_current: int = CoroutineExtras.getWorkerId(); 37 while (continue_execution.get() != true) { 38 // spin 39 } 40 return (id_main != id_current) ? 0 : 1; 41} 42 43function check_sync_id_nonmain(number_of_coros: int): int { 44 let id_current: int = CoroutineExtras.getWorkerId(); 45 let promises: NullablePromise<Int>[] = new NullablePromise<Int>[number_of_coros]; 46 47 for (let i = 0; i < number_of_coros; ++i) { 48 promises[i] = launch checker(id_current); 49 } 50 continue_execution.set(true); 51 let result: int = 0; 52 for (let i = 0; i < number_of_coros; ++i) { 53 result += await promises[i]!; 54 } 55 return result == 0 ? result : 1; 56} 57 58function check_default_policy(): int { 59 L.log("Testing the DEFAULT affinity type"); 60 61 if (check_async_id() != 0) { 62 L.logError("async function's worker ID is different from launcher function's worker ID"); 63 return 1; 64 } 65 return 0; 66} 67 68function check_nonmain_policy(): int { 69 L.log("Testing the NON_MAIN affinity type"); 70 CoroutineExtras.setSchedulingPolicy(CoroutineExtras.POLICY_NON_MAIN); 71 72 if (check_async_id() != 0) { 73 L.logError("async function's worker ID is different from launcher function's worker ID"); 74 return 1; 75 } 76 if (check_sync_id_nonmain(15) != 0) { 77 L.logError("regular coroutine's worker ID is the same as MAIN worker ID"); 78 return 1; 79 } 80 return 0; 81} 82 83export function main(): int { 84 // Call L's enableLogging method to enable logs. 85 86 // Test 1. Check that the async function will execute on the same worker 87 if (check_default_policy() != 0) { 88 L.logError("test for the DEFAULT affinity type failed"); 89 return 1; 90 } 91 92 /** 93 * Test 2. Check that POLICY_NON_MAIN does not affect async functions, 94 * while regular coroutines will execute on non-MAIN workers. 95 */ 96 if (check_nonmain_policy() != 0) { 97 L.logError("test for the NON_MAIN affinity type failed"); 98 return 1; 99 } 100 101 /** 102 * Test 3. Check that EXCLUSIVE launch flag works 103 */ 104 // NOTE(konstanting): will be added later (see #14697) 105 106 return 0; 107} 108 109