• 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
16import { launch } from "std/concurrency"
17import { Job } from "std/core"
18
19function return_value(i: int): String {
20    return "value " + i;
21}
22
23function n_cor_launch(n_cor: int): int {
24    let p: (Job<String> | undefined)[] = new (Job<String> | undefined)[n_cor];
25    for (let i = 0; i < n_cor; ++i) {
26        p[i] = launch<String, (i: int) => String>(return_value, i);
27    }
28    for (let i = 0; i < n_cor; ++i) {
29        let val = (p[i]!).Await();
30        if(val != return_value(i)) {
31            console.println("p[" + i + "] = \"" + val + "\" instead of \"" + return_value(i) + "\"");
32            return 1;
33        }
34    }
35    return 0;
36}
37
38function throw_exception(): Object {
39    throw new Error();
40}
41
42function n_cor_exection_launch(n_cor: int): int {
43    let p: (Job<Object> | undefined)[] = new (Job<Object> | undefined)[n_cor];
44    for (let i = 0; i < n_cor; ++i) {
45        p[i] = launch<Object, () => Object>(throw_exception);
46    }
47    for (let i = 0; i < n_cor; ++i) {
48        try {
49            (p[i]!).Await();
50        } catch (e) {
51            if (!(e instanceof Error)) {
52              console.println("Expected Error but another exception has been thrown!: " + e);
53              return 1;
54            }
55            continue;
56        }
57        console.println("Was no error throwing");
58        return 1;
59    }
60    return 0;
61}
62
63// !1581, molotkovmikhail - test should be start with coroutine pool to test it
64export function main(): int {
65    // Create 5 coroutines and save 5 coroutines
66    if(n_cor_launch(5) != 0) {
67        console.println("First launch of 5 coroutines failed!!");
68        return -1;
69    }
70    // Reuse 5 coroutines
71    if(n_cor_launch(5) != 0) {
72        console.println("Second launch of 5 coroutines failed!!");
73        return -1;
74    }
75    // Create 10 coroutines: reuse 5 coroutines and create new 5 ones. Save 10 coroutines
76    if(n_cor_launch(10) != 0) {
77        console.println("First launch of 10 coroutines failed!!");
78        return -1;
79    }
80    // Reuse 10 coroutines
81    if(n_cor_launch(10) != 0) {
82        console.println("Second launch of 10 coroutines failed!!");
83        return -1;
84    }
85    // Reuse 5 coroutines and throw exception in them
86    if(n_cor_exection_launch(5) != 0) {
87        console.println("First launch of 5 throw-error coroutines failed!!");
88        return -1;
89    }
90    // Reuse 10 coroutines
91    if(n_cor_launch(10) != 0) {
92        console.println("First launch of 10 coroutines after 5 throw-error ones failed!!");
93        return -1;
94    }
95    return 0;
96}
97