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 
14 #![cfg(feature = "multi_instance_runtime")]
15 
16 #[cfg(feature = "ffrt")]
17 mod ffrt_test {
18     use std::thread::sleep;
19     use std::time;
20 
21     use ylong_runtime::task::TaskBuilder;
22 
23     #[test]
ffrt_spawn_blocking_test()24     fn ffrt_spawn_blocking_test() {
25         let num = 1000;
26 
27         let mut handles = Vec::with_capacity(num);
28         for i in 0..num {
29             handles.push(ylong_runtime::spawn_blocking(move || {
30                 sleep(time::Duration::from_millis(1));
31                 i
32             }));
33         }
34 
35         for (times, handle) in handles.into_iter().enumerate() {
36             let ret = runtime.block_on(handle);
37             assert_eq!(ret.unwrap(), times);
38         }
39 
40         let mut handles = Vec::with_capacity(num);
41         let task_builder = TaskBuilder::new();
42         for i in 0..num {
43             handles.push(task_builder.spawn_blocking(move || {
44                 sleep(time::Duration::from_millis(1));
45                 i
46             }));
47         }
48 
49         for (times, handle) in handles.into_iter().enumerate() {
50             let ret = runtime.block_on(handle);
51             assert_eq!(ret.unwrap(), times);
52         }
53     }
54 }
55 
56 #[cfg(not(feature = "ffrt"))]
57 mod not_ffrt_test {
58     use std::thread::sleep;
59     use std::time;
60 
61     use ylong_runtime::builder::RuntimeBuilder;
62     use ylong_runtime::executor::Runtime;
63     use ylong_runtime::task::TaskBuilder;
64 
test_spawn(runtime: &Runtime)65     fn test_spawn(runtime: &Runtime) {
66         let num = 1000;
67 
68         let mut handles = Vec::with_capacity(num);
69         for i in 0..num {
70             handles.push(runtime.spawn_blocking(move || {
71                 sleep(time::Duration::from_millis(1));
72                 i
73             }));
74         }
75         for (times, handle) in handles.into_iter().enumerate() {
76             let ret = runtime.block_on(handle);
77             assert_eq!(ret.unwrap(), times);
78         }
79 
80         let mut handles = Vec::with_capacity(num);
81         for i in 0..num {
82             handles.push(ylong_runtime::spawn_blocking(move || {
83                 sleep(time::Duration::from_millis(1));
84                 i
85             }));
86         }
87 
88         for (times, handle) in handles.into_iter().enumerate() {
89             let ret = runtime.block_on(handle);
90             assert_eq!(ret.unwrap(), times);
91         }
92 
93         let mut handles = Vec::with_capacity(num);
94         let task_builder = TaskBuilder::new();
95         for i in 0..num {
96             handles.push(task_builder.spawn_blocking(move || {
97                 sleep(time::Duration::from_millis(1));
98                 i
99             }));
100         }
101 
102         for (times, handle) in handles.into_iter().enumerate() {
103             let ret = runtime.block_on(handle);
104             assert_eq!(ret.unwrap(), times);
105         }
106     }
107 
108     // One Core Test
109     #[test]
sdv_one_core_test()110     fn sdv_one_core_test() {
111         let max_blocking_pool_size = 1;
112         let blocking_permanent_thread_num = 1;
113 
114         let runtime = RuntimeBuilder::new_multi_thread()
115             .max_blocking_pool_size(max_blocking_pool_size)
116             .blocking_permanent_thread_num(blocking_permanent_thread_num)
117             .build()
118             .unwrap();
119 
120         test_spawn(&runtime);
121     }
122 
123     // Second Core Test
124     #[test]
sdv_two_core_test()125     fn sdv_two_core_test() {
126         let max_blocking_pool_size = 2;
127         let blocking_permanent_thread_num = 2;
128 
129         let runtime = RuntimeBuilder::new_multi_thread()
130             .max_blocking_pool_size(max_blocking_pool_size)
131             .blocking_permanent_thread_num(blocking_permanent_thread_num)
132             .build()
133             .unwrap();
134 
135         test_spawn(&runtime);
136     }
137 
138     // Three Core Test
139     #[test]
sdv_three_core_test()140     fn sdv_three_core_test() {
141         let max_blocking_pool_size = 3;
142         let blocking_permanent_thread_num = 3;
143 
144         let runtime = RuntimeBuilder::new_multi_thread()
145             .max_blocking_pool_size(max_blocking_pool_size)
146             .blocking_permanent_thread_num(blocking_permanent_thread_num)
147             .build()
148             .unwrap();
149 
150         test_spawn(&runtime);
151     }
152 
153     // Four resident threads test
154     #[test]
sdv_four_core_test()155     fn sdv_four_core_test() {
156         let max_blocking_pool_size = 4;
157 
158         let runtime = RuntimeBuilder::new_multi_thread()
159             .max_blocking_pool_size(max_blocking_pool_size)
160             .build()
161             .unwrap();
162 
163         test_spawn(&runtime);
164     }
165 
166     // Eight Core Test
167     #[test]
sdv_eight_core_test()168     fn sdv_eight_core_test() {
169         let max_blocking_pool_size = 8;
170 
171         let runtime = RuntimeBuilder::new_multi_thread()
172             .max_blocking_pool_size(max_blocking_pool_size)
173             .build()
174             .unwrap();
175 
176         test_spawn(&runtime);
177     }
178 
179     // 64-core test, which is also the maximum number of cores supported
180     #[test]
sdv_max_core_test()181     fn sdv_max_core_test() {
182         let max_blocking_pool_size = 64;
183 
184         let runtime = RuntimeBuilder::new_multi_thread()
185             .max_blocking_pool_size(max_blocking_pool_size)
186             .build()
187             .unwrap();
188 
189         test_spawn(&runtime);
190     }
191 
192     #[test]
sdv_complex_task_test()193     fn sdv_complex_task_test() {
194         let max_blocking_pool_size = 4;
195 
196         let runtime = RuntimeBuilder::new_multi_thread()
197             .max_blocking_pool_size(max_blocking_pool_size)
198             .build()
199             .unwrap();
200 
201         test_spawn(&runtime);
202     }
203 }
204