1 /* 2 * Copyright (c) 2022 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 "task_scheduler.h" 17 18 #include <gtest/gtest.h> 19 20 #include "block_data.h" 21 22 using namespace testing::ext; 23 using namespace OHOS; 24 using duration = std::chrono::steady_clock::duration; 25 class TaskSchedulerTest : public testing::Test { 26 public: 27 static constexpr uint32_t SHORT_INTERVAL = 100; 28 static constexpr uint32_t LONG_INTERVAL = 300; SetUpTestCase(void)29 static void SetUpTestCase(void){}; TearDownTestCase(void)30 static void TearDownTestCase(void){}; SetUp()31 void SetUp(){}; TearDown()32 void TearDown() {} 33 }; 34 35 /** 36 * @tc.name: At 37 * @tc.desc: 38 * @tc.type: FUNC 39 * @tc.require: 40 * @tc.author: ht 41 */ 42 HWTEST_F(TaskSchedulerTest, At, TestSize.Level0) 43 { 44 TaskScheduler taskScheduler("atTest"); 45 auto expiredTime = std::chrono::steady_clock::now() + std::chrono::milliseconds(SHORT_INTERVAL); 46 int testData = 10; 47 auto blockData = std::make_shared<BlockData<int>>(LONG_INTERVAL, testData); __anon48d559560102() 48 auto atTaskId1 = taskScheduler.At(expiredTime, [blockData]() { 49 int testData = 11; 50 blockData->SetValue(testData); 51 }); 52 ASSERT_EQ(blockData->GetValue(), 11); 53 blockData->Clear(); 54 expiredTime = std::chrono::steady_clock::now() + std::chrono::milliseconds(SHORT_INTERVAL); __anon48d559560202() 55 auto atTaskId2 = taskScheduler.At(expiredTime, [blockData]() { 56 int testData = 12; 57 blockData->SetValue(testData); 58 }); 59 ASSERT_EQ(blockData->GetValue(), 12); 60 ASSERT_NE(atTaskId1, atTaskId2); 61 } 62 63 /** 64 * @tc.name: Every 65 * @tc.desc: 66 * @tc.type: FUNC 67 * @tc.require: 68 * @tc.author: ht 69 */ 70 HWTEST_F(TaskSchedulerTest, Every, TestSize.Level0) 71 { 72 TaskScheduler taskScheduler("everyTest"); 73 auto blockData = std::make_shared<BlockData<int>>(LONG_INTERVAL, 0); __anon48d559560302() 74 taskScheduler.Every(std::chrono::milliseconds(SHORT_INTERVAL), [blockData]() { 75 int testData = 1; 76 blockData->SetValue(testData); 77 }); 78 for (int i = 1; i < 5; ++i) { 79 ASSERT_EQ(blockData->GetValue(), 1); 80 } 81 } 82 83 /** 84 * @tc.name: Reset 85 * @tc.desc: Reset before task execution and the task is tasks_.begin() or not 86 * @tc.type: FUNC 87 * @tc.require: 88 * @tc.author: ht 89 */ 90 HWTEST_F(TaskSchedulerTest, Reset1, TestSize.Level0) 91 { 92 TaskScheduler taskScheduler("reset1Test"); 93 auto expiredTime = std::chrono::steady_clock::now() + std::chrono::milliseconds(SHORT_INTERVAL); __anon48d559560402() 94 auto atTaskId1 = taskScheduler.At(expiredTime, []() {}); 95 expiredTime += std::chrono::milliseconds(LONG_INTERVAL); __anon48d559560502() 96 auto atTaskId2 = taskScheduler.At(expiredTime, []() {}); 97 98 auto resetTaskId1 = taskScheduler.Reset(atTaskId1, std::chrono::milliseconds(SHORT_INTERVAL)); 99 ASSERT_EQ(resetTaskId1, atTaskId1); 100 101 auto resetTaskId2 = taskScheduler.Reset(atTaskId2, std::chrono::milliseconds(SHORT_INTERVAL)); 102 ASSERT_EQ(resetTaskId2, atTaskId2); 103 } 104 105 /** 106 * @tc.name: Reset 107 * @tc.desc: Reset during task execution 108 * @tc.type: FUNC 109 * @tc.require: 110 * @tc.author: ht 111 */ 112 HWTEST_F(TaskSchedulerTest, Reset2, TestSize.Level0) 113 { 114 TaskScheduler taskScheduler("reset2Test"); 115 int testData = 10; 116 auto blockData = std::make_shared<BlockData<int>>(LONG_INTERVAL, testData); 117 auto expiredTime = std::chrono::steady_clock::now(); __anon48d559560602() 118 auto atTaskId = taskScheduler.At(expiredTime, [blockData]() { 119 blockData->GetValue(); 120 }); 121 std::this_thread::sleep_for(std::chrono::milliseconds(SHORT_INTERVAL)); 122 auto resetTaskId = taskScheduler.Reset(atTaskId, std::chrono::milliseconds(0)); 123 ASSERT_EQ(resetTaskId, TaskScheduler::INVALID_TASK_ID); 124 blockData->SetValue(testData); 125 } 126 127 /** 128 * @tc.name: Reset 129 * @tc.desc: Reset after task execution 130 * @tc.type: FUNC 131 * @tc.require: 132 * @tc.author: ht 133 */ 134 HWTEST_F(TaskSchedulerTest, Reset3, TestSize.Level0) 135 { 136 TaskScheduler taskScheduler("reset3Test"); 137 auto expiredTime = std::chrono::steady_clock::now(); 138 int testData = 10; 139 auto blockData = std::make_shared<BlockData<int>>(LONG_INTERVAL, testData); __anon48d559560702() 140 auto atTaskId = taskScheduler.At(expiredTime, [blockData]() { 141 blockData->GetValue(); 142 }); 143 blockData->SetValue(testData); 144 blockData->SetValue(testData); 145 ASSERT_EQ(blockData->GetValue(), testData); 146 std::this_thread::sleep_for(std::chrono::milliseconds(SHORT_INTERVAL)); 147 auto resetTaskId = taskScheduler.Reset(atTaskId, std::chrono::milliseconds(0)); 148 ASSERT_EQ(resetTaskId, TaskScheduler::INVALID_TASK_ID); 149 } 150