1 /*
2  * Copyright (c) 2023 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 #include <thread>
17 #include <chrono>
18 #include <gtest/gtest.h>
19 #include "ffrt_inner.h"
20 #include "c/loop.h"
21 #include "../common.h"
22 
23 using namespace std;
24 using namespace ffrt;
25 using namespace testing;
26 #ifdef HWTEST_TESTING_EXT_ENABLE
27 using namespace testing::ext;
28 #endif
29 
30 class LoopTest : public testing::Test {
31 protected:
SetUpTestCase()32     static void SetUpTestCase()
33     {
34     }
35 
TearDownTestCase()36     static void TearDownTestCase()
37     {
38     }
39 
SetUp()40     virtual void SetUp()
41     {
42     }
43 
TearDown()44     virtual void TearDown()
45     {
46     }
47 };
48 
ThreadFunc(void * p)49 void* ThreadFunc(void* p)
50 {
51     int ret = ffrt_loop_run(p);
52     EXPECT_EQ(ret, 0);
53     return nullptr;
54 }
55 
56 /*
57  * 测试用例名称:loop_null_queue_create_fail
58  * 测试用例描述:非法队列创建loop失败
59  * 预置条件    :无
60  * 操作步骤    :1、创建loop失败
61  *
62  * 预期结果    :创建失败
63  */
64 HWTEST_F(LoopTest, loop_null_queue_create_fail, TestSize.Level1)
65 {
66     auto loop = ffrt_loop_create(nullptr);
67     EXPECT_EQ(loop, nullptr);
68 }
69 
70 /*
71  * 测试用例名称:loop_serial_queue_create_succ
72  * 测试用例描述:serial队列创建loop失败
73  * 预置条件    :1、调用串行队列创建接口创建serial队列
74  * 操作步骤    :1、创建loop
75  *
76  * 预期结果    :创建失败
77  */
78 HWTEST_F(LoopTest, loop_serial_queue_create_succ, TestSize.Level1)
79 {
80     ffrt_queue_attr_t queue_attr;
81     (void)ffrt_queue_attr_init(&queue_attr);
82     ffrt_queue_t queue_handle = ffrt_queue_create(ffrt_queue_serial, "test_queue", &queue_attr);
83 
84     auto loop = ffrt_loop_create(queue_handle);
85     EXPECT_EQ(loop, nullptr);
86 
87     ffrt_queue_attr_destroy(&queue_attr);
88     ffrt_queue_destroy(queue_handle);
89 }
90 
91 /*
92  * 测试用例名称:loop_concurrent_queue_create_succ
93  * 测试用例描述:无任务concurrent队列创建loop成功
94  * 预置条件    :1、调用串行队列创建接口创建concurrent队列
95  * 操作步骤    :1、创建loop
96  *
97  * 预期结果    :执行成功
98  */
99 HWTEST_F(LoopTest, loop_concurrent_queue_create_succ, TestSize.Level1)
100 {
101     ffrt_queue_attr_t queue_attr;
102     (void)ffrt_queue_attr_init(&queue_attr);
103     ffrt_queue_t queue_handle = ffrt_queue_create(ffrt_queue_concurrent, "test_queue", &queue_attr);
104 
105     auto loop = ffrt_loop_create(queue_handle);
106     EXPECT_NE(loop, nullptr);
107 
108     int ret = ffrt_loop_destroy(loop);
109     EXPECT_EQ(ret, 0);
110 
111     ffrt_queue_attr_destroy(&queue_attr);
112     ffrt_queue_destroy(queue_handle);
113 }
114 
115 /*
116  * 测试用例名称:loop_concurrent_queue_create_fail
117  * 测试用例描述:有任务队列创建loop失败
118  * 预置条件    :1、调用串行队列创建接口创建concurrent队列
119  *            2、创建loop前向队列提交任务
120  * 操作步骤    :1、创建loop
121  *
122  * 预期结果    :创建失败
123  */
124 HWTEST_F(LoopTest, loop_concurrent_queue_create_fail, TestSize.Level1)
125 {
126     ffrt_queue_attr_t queue_attr;
127     (void)ffrt_queue_attr_init(&queue_attr);
128     ffrt_queue_t queue_handle = ffrt_queue_create(ffrt_queue_concurrent, "test_queue", &queue_attr);
129 
130     int result1 = 0;
__anon6e0d45510102() 131     std::function<void()>&& basicFunc1 = [&result1]() { result1 += 10; };
132     ffrt_queue_submit(queue_handle, create_function_wrapper(basicFunc1, ffrt_function_kind_queue), nullptr);
133 
134     auto loop = ffrt_loop_create(queue_handle);
135     EXPECT_EQ(loop, nullptr);
136 
137     ffrt_queue_attr_destroy(&queue_attr);
138     ffrt_queue_destroy(queue_handle);
139 }
140 
141 /*
142  * 测试用例名称:loop_run_fail
143  * 测试用例描述:非法loop run失败
144  * 操作步骤    :1、执行loop run
145  *
146  * 预期结果    :执行失败
147  */
148 HWTEST_F(LoopTest, loop_run_fail, TestSize.Level1)
149 {
150     int ret = ffrt_loop_run(nullptr);
151     EXPECT_NE(ret, 0);
152 }
153 
154 /*
155  * 测试用例名称:loop_destroy_fail
156  * 测试用例描述:非法loop destroy失败
157  * 操作步骤    :1、执行loop run
158  *
159  * 预期结果    :执行失败
160  */
161 HWTEST_F(LoopTest, loop_destroy_fail, TestSize.Level1)
162 {
163     int ret = ffrt_loop_destroy(nullptr);
164     EXPECT_NE(ret, 0);
165 }
166 
167 /*
168  * 测试用例名称:loop_run_destroy_success
169  * 测试用例描述:正常loop run成功、destroy
170  * 预置条件    :1、调用串行队列创建接口创建concurrent队列
171  *            2、用队列创建loop
172  * 操作步骤    :1、启动线程执行loop run
173  *            2、销毁loop成功
174  * 预期结果    :执行成功
175  */
176 HWTEST_F(LoopTest, loop_run_destroy_success, TestSize.Level1)
177 {
178     ffrt_queue_attr_t queue_attr;
179     (void)ffrt_queue_attr_init(&queue_attr);
180     ffrt_queue_t queue_handle = ffrt_queue_create(ffrt_queue_concurrent, "test_queue", &queue_attr);
181 
182     auto loop = ffrt_loop_create(queue_handle);
183     EXPECT_NE(loop, nullptr);
184 
185     pthread_t thread;
186     pthread_create(&thread, 0, ThreadFunc, loop);
187 
188     ffrt_loop_stop(loop);
189     int ret = ffrt_loop_destroy(loop);
190     EXPECT_EQ(ret, 0);
191 
192     ffrt_queue_attr_destroy(&queue_attr);
193     ffrt_queue_destroy(queue_handle);
194 }