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