Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <chrono> |
| 18 | #include <thread> |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <mediautils/TimerThread.h> |
| 21 | |
| 22 | using namespace std::chrono_literals; |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 23 | using namespace android::mediautils; |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 24 | |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | constexpr auto kJitter = 10ms; |
| 28 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 29 | // Each task written by *ToString() will start with a left brace. |
| 30 | constexpr char REQUEST_START = '{'; |
| 31 | |
| 32 | inline size_t countChars(std::string_view s, char c) { |
| 33 | return std::count(s.begin(), s.end(), c); |
| 34 | } |
| 35 | |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 36 | |
| 37 | // Split msec time between timeout and second chance time |
| 38 | // This tests expiration times weighted between timeout and the second chance time. |
| 39 | #define DISTRIBUTE_TIMEOUT_SECONDCHANCE_MS_FRAC(msec, frac) \ |
| 40 | std::chrono::milliseconds(int((msec) * (frac)) + 1), \ |
| 41 | std::chrono::milliseconds(int((msec) * (1.f - (frac)))) |
| 42 | |
| 43 | // The TimerThreadTest is parameterized on a fraction between 0.f and 1.f which |
| 44 | // is how the total timeout time is split between the first timeout and the second chance time. |
| 45 | // |
| 46 | class TimerThreadTest : public ::testing::TestWithParam<float> { |
| 47 | protected: |
| 48 | |
| 49 | static void testBasic() { |
| 50 | const auto frac = GetParam(); |
| 51 | |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 52 | std::atomic<bool> taskRan = false; |
| 53 | TimerThread thread; |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 54 | TimerThread::Handle handle = |
| 55 | thread.scheduleTask("Basic", [&taskRan](TimerThread::Handle handle __unused) { |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 56 | taskRan = true; }, DISTRIBUTE_TIMEOUT_SECONDCHANCE_MS_FRAC(100, frac)); |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 57 | ASSERT_TRUE(TimerThread::isTimeoutHandle(handle)); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 58 | std::this_thread::sleep_for(100ms - kJitter); |
| 59 | ASSERT_FALSE(taskRan); |
| 60 | std::this_thread::sleep_for(2 * kJitter); |
| 61 | ASSERT_TRUE(taskRan); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 62 | ASSERT_EQ(1, countChars(thread.retiredToString(), REQUEST_START)); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 65 | static void testCancel() { |
| 66 | const auto frac = GetParam(); |
| 67 | |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 68 | std::atomic<bool> taskRan = false; |
| 69 | TimerThread thread; |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 70 | TimerThread::Handle handle = |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 71 | thread.scheduleTask("Cancel", [&taskRan](TimerThread::Handle handle __unused) { |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 72 | taskRan = true; }, DISTRIBUTE_TIMEOUT_SECONDCHANCE_MS_FRAC(100, frac)); |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 73 | ASSERT_TRUE(TimerThread::isTimeoutHandle(handle)); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 74 | std::this_thread::sleep_for(100ms - kJitter); |
| 75 | ASSERT_FALSE(taskRan); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 76 | ASSERT_TRUE(thread.cancelTask(handle)); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 77 | std::this_thread::sleep_for(2 * kJitter); |
| 78 | ASSERT_FALSE(taskRan); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 79 | ASSERT_EQ(1, countChars(thread.retiredToString(), REQUEST_START)); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 82 | static void testCancelAfterRun() { |
| 83 | const auto frac = GetParam(); |
| 84 | |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 85 | std::atomic<bool> taskRan = false; |
| 86 | TimerThread thread; |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 87 | TimerThread::Handle handle = |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 88 | thread.scheduleTask("CancelAfterRun", |
| 89 | [&taskRan](TimerThread::Handle handle __unused) { |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 90 | taskRan = true; }, |
| 91 | DISTRIBUTE_TIMEOUT_SECONDCHANCE_MS_FRAC(100, frac)); |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 92 | ASSERT_TRUE(TimerThread::isTimeoutHandle(handle)); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 93 | std::this_thread::sleep_for(100ms + kJitter); |
| 94 | ASSERT_TRUE(taskRan); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 95 | ASSERT_FALSE(thread.cancelTask(handle)); |
| 96 | ASSERT_EQ(1, countChars(thread.retiredToString(), REQUEST_START)); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 99 | static void testMultipleTasks() { |
| 100 | const auto frac = GetParam(); |
| 101 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 102 | std::array<std::atomic<bool>, 6> taskRan{}; |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 103 | TimerThread thread; |
| 104 | |
| 105 | auto startTime = std::chrono::steady_clock::now(); |
| 106 | |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 107 | thread.scheduleTask("0", [&taskRan](TimerThread::Handle handle __unused) { |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 108 | taskRan[0] = true; }, DISTRIBUTE_TIMEOUT_SECONDCHANCE_MS_FRAC(300, frac)); |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 109 | thread.scheduleTask("1", [&taskRan](TimerThread::Handle handle __unused) { |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 110 | taskRan[1] = true; }, DISTRIBUTE_TIMEOUT_SECONDCHANCE_MS_FRAC(100, frac)); |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 111 | thread.scheduleTask("2", [&taskRan](TimerThread::Handle handle __unused) { |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 112 | taskRan[2] = true; }, DISTRIBUTE_TIMEOUT_SECONDCHANCE_MS_FRAC(200, frac)); |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 113 | thread.scheduleTask("3", [&taskRan](TimerThread::Handle handle __unused) { |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 114 | taskRan[3] = true; }, DISTRIBUTE_TIMEOUT_SECONDCHANCE_MS_FRAC(400, frac)); |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 115 | auto handle4 = thread.scheduleTask("4", [&taskRan](TimerThread::Handle handle __unused) { |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 116 | taskRan[4] = true; }, DISTRIBUTE_TIMEOUT_SECONDCHANCE_MS_FRAC(200, frac)); |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 117 | thread.scheduleTask("5", [&taskRan](TimerThread::Handle handle __unused) { |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 118 | taskRan[5] = true; }, DISTRIBUTE_TIMEOUT_SECONDCHANCE_MS_FRAC(200, frac)); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 119 | |
| 120 | // 6 tasks pending |
| 121 | ASSERT_EQ(6, countChars(thread.pendingToString(), REQUEST_START)); |
| 122 | // 0 tasks completed |
| 123 | ASSERT_EQ(0, countChars(thread.retiredToString(), REQUEST_START)); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 124 | |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 125 | // None of the tasks are expected to have finished at the start. |
| 126 | std::array<std::atomic<bool>, 6> expected{}; |
| 127 | |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 128 | // Task 1 should trigger around 100ms. |
| 129 | std::this_thread::sleep_until(startTime + 100ms - kJitter); |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 130 | |
| 131 | ASSERT_EQ(expected, taskRan); |
| 132 | |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 133 | |
| 134 | std::this_thread::sleep_until(startTime + 100ms + kJitter); |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 135 | |
| 136 | expected[1] = true; |
| 137 | ASSERT_EQ(expected, taskRan); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 138 | |
| 139 | // Cancel task 4 before it gets a chance to run. |
| 140 | thread.cancelTask(handle4); |
| 141 | |
| 142 | // Tasks 2 and 5 should trigger around 200ms. |
| 143 | std::this_thread::sleep_until(startTime + 200ms - kJitter); |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 144 | |
| 145 | ASSERT_EQ(expected, taskRan); |
| 146 | |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 147 | |
| 148 | std::this_thread::sleep_until(startTime + 200ms + kJitter); |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 149 | |
| 150 | expected[2] = true; |
| 151 | expected[5] = true; |
| 152 | ASSERT_EQ(expected, taskRan); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 153 | |
| 154 | // Task 0 should trigger around 300ms. |
| 155 | std::this_thread::sleep_until(startTime + 300ms - kJitter); |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 156 | |
| 157 | ASSERT_EQ(expected, taskRan); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 158 | |
| 159 | std::this_thread::sleep_until(startTime + 300ms + kJitter); |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 160 | |
| 161 | expected[0] = true; |
| 162 | ASSERT_EQ(expected, taskRan); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 163 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 164 | // 1 task pending |
| 165 | ASSERT_EQ(1, countChars(thread.pendingToString(), REQUEST_START)); |
| 166 | // 4 tasks ran and 1 cancelled |
| 167 | ASSERT_EQ(4 + 1, countChars(thread.retiredToString(), REQUEST_START)); |
| 168 | |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 169 | // Task 3 should trigger around 400ms. |
| 170 | std::this_thread::sleep_until(startTime + 400ms - kJitter); |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 171 | |
| 172 | ASSERT_EQ(expected, taskRan); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 173 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 174 | // 4 tasks ran and 1 cancelled |
| 175 | ASSERT_EQ(4 + 1, countChars(thread.retiredToString(), REQUEST_START)); |
| 176 | |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 177 | std::this_thread::sleep_until(startTime + 400ms + kJitter); |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 178 | |
| 179 | expected[3] = true; |
| 180 | ASSERT_EQ(expected, taskRan); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 181 | |
| 182 | // 0 tasks pending |
| 183 | ASSERT_EQ(0, countChars(thread.pendingToString(), REQUEST_START)); |
| 184 | // 5 tasks ran and 1 cancelled |
| 185 | ASSERT_EQ(5 + 1, countChars(thread.retiredToString(), REQUEST_START)); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 188 | }; // class TimerThreadTest |
| 189 | |
| 190 | TEST_P(TimerThreadTest, Basic) { |
| 191 | testBasic(); |
| 192 | } |
| 193 | |
| 194 | TEST_P(TimerThreadTest, Cancel) { |
| 195 | testCancel(); |
| 196 | } |
| 197 | |
| 198 | TEST_P(TimerThreadTest, CancelAfterRun) { |
| 199 | testCancelAfterRun(); |
| 200 | } |
| 201 | |
| 202 | TEST_P(TimerThreadTest, MultipleTasks) { |
| 203 | testMultipleTasks(); |
| 204 | } |
| 205 | |
| 206 | INSTANTIATE_TEST_CASE_P( |
| 207 | TimerThread, |
| 208 | TimerThreadTest, |
| 209 | ::testing::Values(0.f, 0.5f, 1.f) |
| 210 | ); |
| 211 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 212 | TEST(TimerThread, TrackedTasks) { |
| 213 | TimerThread thread; |
| 214 | |
| 215 | auto handle0 = thread.trackTask("0"); |
| 216 | auto handle1 = thread.trackTask("1"); |
| 217 | auto handle2 = thread.trackTask("2"); |
| 218 | |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 219 | ASSERT_TRUE(TimerThread::isNoTimeoutHandle(handle0)); |
| 220 | ASSERT_TRUE(TimerThread::isNoTimeoutHandle(handle1)); |
| 221 | ASSERT_TRUE(TimerThread::isNoTimeoutHandle(handle2)); |
| 222 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 223 | // 3 tasks pending |
| 224 | ASSERT_EQ(3, countChars(thread.pendingToString(), REQUEST_START)); |
| 225 | // 0 tasks retired |
| 226 | ASSERT_EQ(0, countChars(thread.retiredToString(), REQUEST_START)); |
| 227 | |
| 228 | ASSERT_TRUE(thread.cancelTask(handle0)); |
| 229 | ASSERT_TRUE(thread.cancelTask(handle1)); |
| 230 | |
| 231 | // 1 task pending |
| 232 | ASSERT_EQ(1, countChars(thread.pendingToString(), REQUEST_START)); |
| 233 | // 2 tasks retired |
| 234 | ASSERT_EQ(2, countChars(thread.retiredToString(), REQUEST_START)); |
| 235 | |
| 236 | // handle1 is stale, cancel returns false. |
| 237 | ASSERT_FALSE(thread.cancelTask(handle1)); |
| 238 | |
| 239 | // 1 task pending |
| 240 | ASSERT_EQ(1, countChars(thread.pendingToString(), REQUEST_START)); |
| 241 | // 2 tasks retired |
| 242 | ASSERT_EQ(2, countChars(thread.retiredToString(), REQUEST_START)); |
| 243 | |
| 244 | // Add another tracked task. |
| 245 | auto handle3 = thread.trackTask("3"); |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 246 | ASSERT_TRUE(TimerThread::isNoTimeoutHandle(handle3)); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 247 | |
| 248 | // 2 tasks pending |
| 249 | ASSERT_EQ(2, countChars(thread.pendingToString(), REQUEST_START)); |
| 250 | // 2 tasks retired |
| 251 | ASSERT_EQ(2, countChars(thread.retiredToString(), REQUEST_START)); |
| 252 | |
| 253 | ASSERT_TRUE(thread.cancelTask(handle2)); |
| 254 | |
| 255 | // 1 tasks pending |
| 256 | ASSERT_EQ(1, countChars(thread.pendingToString(), REQUEST_START)); |
| 257 | // 3 tasks retired |
| 258 | ASSERT_EQ(3, countChars(thread.retiredToString(), REQUEST_START)); |
| 259 | |
| 260 | ASSERT_TRUE(thread.cancelTask(handle3)); |
| 261 | |
| 262 | // 0 tasks pending |
| 263 | ASSERT_EQ(0, countChars(thread.pendingToString(), REQUEST_START)); |
| 264 | // 4 tasks retired |
| 265 | ASSERT_EQ(4, countChars(thread.retiredToString(), REQUEST_START)); |
| 266 | } |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 267 | |
| 268 | } // namespace |