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 #include "ffrt_utils_test.h" 16 17 #include "ffrt_utils.h" 18 19 namespace OHOS { 20 namespace PowerMgr { 21 namespace Test { 22 using namespace testing::ext; 23 /** 24 * @tc.name: FFRTUtilsTest001 25 * @tc.desc: test submit task 26 * @tc.type: FUNC 27 */ 28 HWTEST_F(FFRTUtilsTest, FFRTUtilsTest001, TestSize.Level1) 29 { 30 int32_t x = 0; __anon8aecb0b20102() 31 FFRTTask task = [&]() { 32 x = 2; 33 }; 34 FFRTUtils::SubmitTask(task); // submit an async task 35 ffrt::wait(); // wait async task finish 36 EXPECT_EQ(x, 2); 37 } 38 39 /** 40 * @tc.name: FFRTUtilsTest002 41 * @tc.desc: test submit task sync 42 * @tc.type: FUNC 43 */ 44 HWTEST_F(FFRTUtilsTest, FFRTUtilsTest002, TestSize.Level1) 45 { 46 int32_t x = 0; __anon8aecb0b20202() 47 FFRTTask task = [&]() { 48 x = 2; 49 }; 50 FFRTUtils::SubmitTaskSync(task); // submit a sync task 51 EXPECT_EQ(x, 2); 52 } 53 54 /** 55 * @tc.name: FFRTUtilsTest003 56 * @tc.desc: test submit queue tasks 57 * @tc.type: FUNC 58 */ 59 HWTEST_F(FFRTUtilsTest, FFRTUtilsTest003, TestSize.Level1) 60 { 61 int x = 0; __anon8aecb0b20302() 62 FFRTTask task1 = [&]() { 63 ffrt::this_task::sleep_for(std::chrono::milliseconds(1)); 64 x = 2; 65 }; __anon8aecb0b20402() 66 FFRTTask task2 = [&]() { 67 ffrt::this_task::sleep_for(std::chrono::milliseconds(30)); 68 x += 2; 69 }; __anon8aecb0b20502() 70 FFRTTask task3 = [&]() { 71 ffrt::this_task::sleep_for(std::chrono::milliseconds(50)); 72 x += 2; 73 }; 74 75 FFRTQueue queue("test_power_ffrt_queue"); 76 FFRTUtils::SubmitQueueTasks({task1, task2, task3}, queue); // submit batch tasks to a queue 77 78 ffrt::this_task::sleep_for(std::chrono::milliseconds(10)); 79 EXPECT_EQ(x, 2); // task1 finished 80 81 ffrt::this_task::sleep_for(std::chrono::milliseconds(50)); 82 EXPECT_EQ(x, 4); // task2 finished 83 84 ffrt::this_task::sleep_for(std::chrono::milliseconds(80)); 85 EXPECT_EQ(x, 6); // task3 finished 86 } 87 88 /** 89 * @tc.name: FFRTUtilsTest004 90 * @tc.desc: test submit delay task 91 * @tc.type: FUNC 92 */ 93 HWTEST_F(FFRTUtilsTest, FFRTUtilsTest004, TestSize.Level1) 94 { 95 int x = 0; __anon8aecb0b20602() 96 FFRTTask task = [&]() { 97 x = 2; 98 }; 99 100 FFRTQueue queue("test_power_ffrt_queue"); 101 FFRTUtils::SubmitDelayTask(task, 10, queue); // submit delay task to a queue 102 103 ffrt::this_task::sleep_for(std::chrono::milliseconds(5)); 104 EXPECT_EQ(x, 0); // task not executed 105 106 ffrt::this_task::sleep_for(std::chrono::milliseconds(7)); 107 EXPECT_EQ(x, 2); // task finished 108 } 109 110 /** 111 * @tc.name: FFRTUtilsTest005 112 * @tc.desc: test cancel delay task 113 * @tc.type: FUNC 114 */ 115 HWTEST_F(FFRTUtilsTest, FFRTUtilsTest005, TestSize.Level1) 116 { 117 int x = 0; __anon8aecb0b20702() 118 FFRTTask task = [&]() { 119 x = 2; 120 }; 121 122 FFRTQueue queue("test_power_ffrt_queue"); 123 auto handle = FFRTUtils::SubmitDelayTask(task, 10, queue); // submit delay task to a queue 124 125 ffrt::this_task::sleep_for(std::chrono::milliseconds(5)); 126 EXPECT_EQ(x, 0); // task not executed 127 128 FFRTUtils::CancelTask(handle, queue); // cancel the delay task from the queue 129 EXPECT_EQ(x, 0); // task not executed 130 131 ffrt::this_task::sleep_for(std::chrono::milliseconds(10)); 132 EXPECT_EQ(x, 0); // task not executed, because it is already canceled 133 } 134 135 /** 136 * @tc.name: FFRTUtilsTest006 137 * @tc.desc: test submit timeout task and the task is executed successfully 138 * @tc.type: FUNC 139 */ 140 HWTEST_F(FFRTUtilsTest, FFRTUtilsTest006, TestSize.Level1) 141 { 142 int x = 0; __anon8aecb0b20802() 143 FFRTTask task = [&]() { 144 ffrt::this_task::sleep_for(std::chrono::milliseconds(5)); // task sleep 5ms 145 x = 2; 146 }; 147 bool ret = FFRTUtils::SubmitTimeoutTask(task, 10); // task time out is 10ms 148 EXPECT_TRUE(ret); // task will not timeout 149 EXPECT_EQ(x, 2); // task finished 150 } 151 152 /** 153 * @tc.name: FFRTUtilsTest007 154 * @tc.desc: test submit timeout task and the task execution times out 155 * @tc.type: FUNC 156 */ 157 HWTEST_F(FFRTUtilsTest, FFRTUtilsTest007, TestSize.Level1) 158 { 159 int x = 0; __anon8aecb0b20902() 160 FFRTTask task = [&]() { 161 ffrt::this_task::sleep_for(std::chrono::milliseconds(10)); // task sleep 10ms 162 x = 2; 163 }; 164 bool ret = FFRTUtils::SubmitTimeoutTask(task, 5); // task time out is 5ms 165 EXPECT_FALSE(ret); // task will timeout 166 EXPECT_EQ(x, 0); // task not finished 167 } 168 169 /** 170 * @tc.name: FFRTUtilsMutexTest001 171 * @tc.desc: test submit tasks with mutex 172 * @tc.type: FUNC 173 */ 174 HWTEST_F(FFRTUtilsTest, FFRTUtilsMutexTest001, TestSize.Level1) 175 { 176 int x = 0; 177 auto mutex = FFRTUtils::Mutex(); __anon8aecb0b20a02() 178 FFRTTask task1 = [&]() { 179 mutex.Lock(); // mutex lock 180 ffrt::this_task::sleep_for(std::chrono::milliseconds(10)); // task sleep 10ms 181 x += 2; 182 mutex.Unlock(); // mutex unlock 183 }; __anon8aecb0b20b02() 184 FFRTTask task2 = [&]() { 185 mutex.Lock(); 186 ffrt::this_task::sleep_for(std::chrono::milliseconds(5)); // task sleep 5ms 187 x += 3; 188 mutex.Unlock(); 189 }; 190 FFRTUtils::SubmitTask(task1); // submit task async 191 ffrt::this_task::sleep_for(std::chrono::milliseconds(2)); // task sleep 2ms 192 FFRTUtils::SubmitTask(task2); // submit task async 193 194 ffrt::this_task::sleep_for(std::chrono::milliseconds(5)); 195 EXPECT_EQ(x, 0); // task1 not finished, and task2 cannot access mutex block 196 EXPECT_FALSE(mutex.TryLock()); // mutex is locked by task1, try lock returns false 197 198 ffrt::this_task::sleep_for(std::chrono::milliseconds(7)); 199 EXPECT_EQ(x, 2); // task1 finished 200 201 ffrt::this_task::sleep_for(std::chrono::milliseconds(5)); 202 EXPECT_EQ(x, 5); // task2 finished 203 EXPECT_TRUE(mutex.TryLock()); // mutex is unlocked, try lock returns true 204 } 205 } // namespace Test 206 } // namespace PowerMgr 207 } // namespace OHOS 208