1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
test_future(num: usize) -> usize14 async fn test_future(num: usize) -> usize {
15 num
16 }
17
test_multi_future_in_async(i: usize, j: usize) -> (usize, usize)18 async fn test_multi_future_in_async(i: usize, j: usize) -> (usize, usize) {
19 let result_one = test_future(i).await;
20 let result_two = test_future(j).await;
21
22 (result_one, result_two)
23 }
24
test_async_in_async(i: usize, j: usize) -> (usize, usize)25 async fn test_async_in_async(i: usize, j: usize) -> (usize, usize) {
26 test_multi_future_in_async(i, j).await
27 }
28 // One Core Test
29 #[test]
sdv_one_core_test()30 fn sdv_one_core_test() {
31 let num = 1000;
32
33 let mut handles = Vec::with_capacity(num);
34
35 for i in 0..num {
36 handles.push(ylong_runtime::spawn(test_future(i)));
37 }
38
39 for (times, handle) in handles.into_iter().enumerate() {
40 let ret = ylong_runtime::block_on(handle);
41 assert_eq!(ret.unwrap(), times);
42 }
43 }
44
45 // Two-core test
46 #[test]
sdv_two_core_test()47 fn sdv_two_core_test() {
48 let num = 1000;
49
50 let mut handles = Vec::with_capacity(num);
51
52 for i in 0..num {
53 handles.push(ylong_runtime::spawn(test_future(i)));
54 }
55
56 for (times, handle) in handles.into_iter().enumerate() {
57 let ret = ylong_runtime::block_on(handle);
58 assert_eq!(ret.unwrap(), times);
59 }
60 }
61
62 // Three Core Test
63 #[test]
sdv_three_core_test()64 fn sdv_three_core_test() {
65 let num = 1000;
66
67 let mut handles = Vec::with_capacity(num);
68
69 for i in 0..num {
70 handles.push(ylong_runtime::spawn(test_future(i)));
71 }
72
73 for (times, handle) in handles.into_iter().enumerate() {
74 let ret = ylong_runtime::block_on(handle);
75 assert_eq!(ret.unwrap(), times);
76 }
77 }
78
79 // Four Core Test
80 #[test]
sdv_four_core_test()81 fn sdv_four_core_test() {
82 let num = 1000;
83
84 let mut handles = Vec::with_capacity(num);
85
86 for i in 0..num {
87 handles.push(ylong_runtime::spawn(test_future(i)));
88 }
89
90 for (times, handle) in handles.into_iter().enumerate() {
91 let ret = ylong_runtime::block_on(handle);
92 assert_eq!(ret.unwrap(), times);
93 }
94 }
95
96 // Eight Core Test
97 #[test]
sdv_eight_core_test()98 fn sdv_eight_core_test() {
99 let num = 1000;
100
101 let mut handles = Vec::with_capacity(num);
102
103 for i in 0..num {
104 handles.push(ylong_runtime::spawn(test_future(i)));
105 }
106
107 for (times, handle) in handles.into_iter().enumerate() {
108 let ret = ylong_runtime::block_on(handle);
109 assert_eq!(ret.unwrap(), times);
110 }
111 }
112
113 // 64 Core Test, It is also the largest number of cores supported
114 #[test]
sdv_max_core_test()115 fn sdv_max_core_test() {
116 let num = 1000;
117
118 let mut handles = Vec::with_capacity(num);
119
120 for i in 0..num {
121 handles.push(ylong_runtime::spawn(test_future(i)));
122 }
123
124 for (times, handle) in handles.into_iter().enumerate() {
125 let ret = ylong_runtime::block_on(handle);
126 assert_eq!(ret.unwrap(), times);
127 }
128 }
129
130 // Having multiple tasks in one `async` block
131 #[test]
sdv_multi_future_in_async()132 fn sdv_multi_future_in_async() {
133 let num = 1000;
134
135 let mut handles = Vec::with_capacity(num);
136
137 for i in 0..num {
138 handles.push(ylong_runtime::spawn(test_multi_future_in_async(i, i + 1)));
139 }
140
141 for (times, handle) in handles.into_iter().enumerate() {
142 let ret = ylong_runtime::block_on(handle);
143 assert_eq!(ret.unwrap(), (times, times + 1));
144 }
145 }
146
147 // Calling other `async` blocks within an `async` block has a multiple call
148 // relationship
149 #[test]
sdv_multi_async_in_async()150 fn sdv_multi_async_in_async() {
151 let num = 1000;
152
153 let mut handles = Vec::with_capacity(num);
154
155 for i in 0..num {
156 handles.push(ylong_runtime::spawn(test_async_in_async(i, i + 1)));
157 }
158
159 for (times, handle) in handles.into_iter().enumerate() {
160 let ret = ylong_runtime::block_on(handle);
161 assert_eq!(ret.unwrap(), (times, times + 1));
162 }
163 }
164