106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci#include <atomic> 1606f6ba60Sopenharmony_ci#include <chrono> 1706f6ba60Sopenharmony_ci#include <gtest/gtest.h> 1806f6ba60Sopenharmony_ci#include <sys/time.h> 1906f6ba60Sopenharmony_ci#include <thread> 2006f6ba60Sopenharmony_ci 2106f6ba60Sopenharmony_ci#include "schedule_task_manager.h" 2206f6ba60Sopenharmony_ci 2306f6ba60Sopenharmony_ciusing namespace testing::ext; 2406f6ba60Sopenharmony_ci 2506f6ba60Sopenharmony_cinamespace { 2606f6ba60Sopenharmony_ciclass ScheduleTaskManagerTest : public testing::Test { 2706f6ba60Sopenharmony_ciprotected: 2806f6ba60Sopenharmony_ci static void SetUpTestCase() {} 2906f6ba60Sopenharmony_ci static void TearDownTestCase() {} 3006f6ba60Sopenharmony_ci}; 3106f6ba60Sopenharmony_ci 3206f6ba60Sopenharmony_ci/** 3306f6ba60Sopenharmony_ci * @tc.name: base 3406f6ba60Sopenharmony_ci * @tc.desc: Single task processing. 3506f6ba60Sopenharmony_ci * @tc.type: FUNC 3606f6ba60Sopenharmony_ci */ 3706f6ba60Sopenharmony_ciHWTEST_F(ScheduleTaskManagerTest, ScheduleTaskOneshot, TestSize.Level1) 3806f6ba60Sopenharmony_ci{ 3906f6ba60Sopenharmony_ci std::atomic<int> count = 0; 4006f6ba60Sopenharmony_ci uint64_t initalDelay = 10; // 10ms 4106f6ba60Sopenharmony_ci 4206f6ba60Sopenharmony_ci ScheduleTaskManager scheduleTaskManager; 4306f6ba60Sopenharmony_ci EXPECT_NE(scheduleTaskManager.ScheduleTask([&]() { count++; }, initalDelay, true), -1); 4406f6ba60Sopenharmony_ci 4506f6ba60Sopenharmony_ci std::this_thread::sleep_for(std::chrono::milliseconds(initalDelay + initalDelay)); 4606f6ba60Sopenharmony_ci EXPECT_EQ(count.load(), 1); 4706f6ba60Sopenharmony_ci} 4806f6ba60Sopenharmony_ci 4906f6ba60Sopenharmony_ci/** 5006f6ba60Sopenharmony_ci * @tc.name: base 5106f6ba60Sopenharmony_ci * @tc.desc: Repetitive task processing. 5206f6ba60Sopenharmony_ci * @tc.type: FUNC 5306f6ba60Sopenharmony_ci */ 5406f6ba60Sopenharmony_ciHWTEST_F(ScheduleTaskManagerTest, ScheduleTaskRepeated, TestSize.Level1) 5506f6ba60Sopenharmony_ci{ 5606f6ba60Sopenharmony_ci std::atomic<int> count = 0; 5706f6ba60Sopenharmony_ci constexpr int cnt = 5; 5806f6ba60Sopenharmony_ci constexpr int thresh = 1; 5906f6ba60Sopenharmony_ci 6006f6ba60Sopenharmony_ci uint64_t repeatInterval = 100; 6106f6ba60Sopenharmony_ci 6206f6ba60Sopenharmony_ci ScheduleTaskManager scheduleTaskManager; 6306f6ba60Sopenharmony_ci EXPECT_NE(scheduleTaskManager.ScheduleTask( 6406f6ba60Sopenharmony_ci [&]() { 6506f6ba60Sopenharmony_ci count++; 6606f6ba60Sopenharmony_ci struct timeval tv; 6706f6ba60Sopenharmony_ci gettimeofday(&tv, nullptr); 6806f6ba60Sopenharmony_ci }, 6906f6ba60Sopenharmony_ci repeatInterval), -1); 7006f6ba60Sopenharmony_ci 7106f6ba60Sopenharmony_ci int expected = 0; 7206f6ba60Sopenharmony_ci std::this_thread::sleep_for(std::chrono::milliseconds(1)); 7306f6ba60Sopenharmony_ci for (int i = 0; i < cnt; i++) { 7406f6ba60Sopenharmony_ci expected++; 7506f6ba60Sopenharmony_ci struct timeval tv = { 0, 0 }; 7606f6ba60Sopenharmony_ci gettimeofday(&tv, nullptr); 7706f6ba60Sopenharmony_ci std::this_thread::sleep_for(std::chrono::milliseconds(repeatInterval)); 7806f6ba60Sopenharmony_ci } 7906f6ba60Sopenharmony_ci EXPECT_LE(abs(count.load() - expected), thresh); 8006f6ba60Sopenharmony_ci} 8106f6ba60Sopenharmony_ci 8206f6ba60Sopenharmony_ci/** 8306f6ba60Sopenharmony_ci * @tc.name: base 8406f6ba60Sopenharmony_ci * @tc.desc: Unschedule Task. 8506f6ba60Sopenharmony_ci * @tc.type: FUNC 8606f6ba60Sopenharmony_ci */ 8706f6ba60Sopenharmony_ciHWTEST_F(ScheduleTaskManagerTest, UnscheduleTask, TestSize.Level1) 8806f6ba60Sopenharmony_ci{ 8906f6ba60Sopenharmony_ci std::atomic<int> count = 0; 9006f6ba60Sopenharmony_ci constexpr int cnt = 5; 9106f6ba60Sopenharmony_ci constexpr int thresh = 1; 9206f6ba60Sopenharmony_ci int32_t taskFd = -1; 9306f6ba60Sopenharmony_ci uint64_t repeatInterval = 100; 9406f6ba60Sopenharmony_ci uint64_t initalDelay = 10; 9506f6ba60Sopenharmony_ci 9606f6ba60Sopenharmony_ci ScheduleTaskManager scheduleTaskManager; 9706f6ba60Sopenharmony_ci taskFd = scheduleTaskManager.ScheduleTask( 9806f6ba60Sopenharmony_ci [&]() { 9906f6ba60Sopenharmony_ci count++; 10006f6ba60Sopenharmony_ci struct timeval tv; 10106f6ba60Sopenharmony_ci gettimeofday(&tv, nullptr); 10206f6ba60Sopenharmony_ci }, 10306f6ba60Sopenharmony_ci repeatInterval); 10406f6ba60Sopenharmony_ci EXPECT_NE(taskFd, -1); 10506f6ba60Sopenharmony_ci 10606f6ba60Sopenharmony_ci int expected = 0; 10706f6ba60Sopenharmony_ci std::this_thread::sleep_for(std::chrono::milliseconds(initalDelay)); 10806f6ba60Sopenharmony_ci for (int i = 0; i < cnt; i++) { 10906f6ba60Sopenharmony_ci expected++; 11006f6ba60Sopenharmony_ci struct timeval tv = { 0, 0 }; 11106f6ba60Sopenharmony_ci gettimeofday(&tv, nullptr); 11206f6ba60Sopenharmony_ci std::this_thread::sleep_for(std::chrono::milliseconds(repeatInterval)); 11306f6ba60Sopenharmony_ci } 11406f6ba60Sopenharmony_ci EXPECT_LE(abs(count.load() - expected), thresh); 11506f6ba60Sopenharmony_ci EXPECT_TRUE(scheduleTaskManager.UnscheduleTask(taskFd)); 11606f6ba60Sopenharmony_ci} 11706f6ba60Sopenharmony_ci} // namespace