• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-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
16
17import {StdDebug} from "std/debug"
18import {CoroutineExtras} from "std/debug/concurrency"
19import { launch } from "std/concurrency"
20
21type L = StdDebug.Logger
22async function async_chain_element(caller_wid: int, counter: int): Promise<int> {
23    let id_worker: int = CoroutineExtras.getWorkerId();
24    if (caller_wid != id_worker) {
25        return 1;
26    }
27    if (counter <= 0) {
28        return 0;
29    }
30    return await async_chain_element(caller_wid, counter - 1);
31}
32
33function async_function_chain(caller_wid: int, chain_len: int): int {
34    return await async_chain_element(caller_wid, chain_len);
35}
36
37function compare_worker_ids_async(chain_len: int): int {
38    L.log("Testing async function chain");
39
40    let id_main: int = CoroutineExtras.getWorkerId();
41    let result = async_function_chain(id_main, chain_len);
42    if (result == 0) {
43        L.log("Successfully ran the async function chain")
44    } else {
45        L.log("Failed to run the async function chain")
46    }
47    return result;
48}
49
50function return_worker_id(): Int {
51    return CoroutineExtras.getWorkerId();
52}
53
54function compare_worker_ids(): int {
55    L.log("Testing launch in a separate worker");
56
57    let id_main: int = CoroutineExtras.getWorkerId();
58    let id_coro: int = (launch<Int, () => Int>(return_worker_id).Await()) as int;
59    if (id_main != id_coro) {
60        L.log("Successfully ran coro in a separate worker. Main WID: " + id_main + ", Coro WID: " + id_coro);
61        return 0;
62    } else {
63        L.logError("Failed to run coro in a separate worker. Main WID: " + id_main + ", Coro WID: " + id_coro);
64        return 1;
65    }
66}
67
68function job(n: int): NullableType {
69    L.log("Job with " + n + " iterations in a loop started");
70    for (let i = 0; i < n; ++i) {
71        //
72    }
73    return null;
74}
75
76function run_batch_launch(batch_size: int, iters: int): int {
77    L.log("Testing batch launch of " + batch_size + " coroutines");
78    for (let i = 0; i < batch_size; ++i) {
79        launch<NullableType, (i: int) => NullableType>(job, iters);
80    }
81    return 0;
82}
83
84function await_chain(n: int): NullableType {
85    if (n > 0) {
86        launch<NullableType, (i: int) => NullableType>(await_chain, n-1).Await();
87    }
88    return null;
89}
90
91function run_await_chain(len: int): int {
92    L.log("Testing await chain of " + len + " items");
93    launch<NullableType, (i: int) => NullableType>(await_chain, len);
94    return 0;
95}
96
97function simple(): NullableType {
98    return null;
99}
100
101function run_batch_await(batch_size: int): int {
102    L.log("Testing batch await of " + batch_size + " coroutines");
103    for (let i = 0; i < batch_size; ++i) {
104        let j = launch<NullableType, () => NullableType>(simple);
105        j.Await();
106    }
107    return 0;
108}
109
110export function main(): int {
111    // Test 1. Run an async function chain and check that all worker IDs are equal to MAIN's
112    if (compare_worker_ids_async(5) != 0) {
113        L.logError("compare async function worker IDs test failed");
114        return 1;
115    }
116
117    // Test 2. Run a coroutine and make sure that its worker ID is different from MAIN
118    if (compare_worker_ids() != 0) {
119        L.logError("compare worker IDs test failed");
120        return 1;
121    }
122
123    // Test 3. Run a batch of coroutines without await so they would be distributed to different workers
124    if (run_batch_launch(100, 500) != 0) {
125        L.logError("batch launch test failed");
126        return 1;
127    }
128
129    // Test 4. Run a chain of several coroutines that await each other. Coroutines will run on different workers.
130    if (run_await_chain(50) != 0) {
131        L.logError("await chain test failed");
132        return 1;
133    }
134
135    // Test 5. Run coroutines on different workers and await them.
136    if (run_batch_await(100) != 0) {
137        L.logError("batch await test failed");
138        return 1;
139    }
140
141    return 0;
142}