Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -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 <algorithm> |
| 18 | #include <chrono> |
| 19 | #include <future> |
| 20 | #include <thread> |
| 21 | |
| 22 | #include <gtest/gtest.h> |
| 23 | |
Joshua McCloskey | c8c0bad | 2022-05-10 05:17:44 +0000 | [diff] [blame] | 24 | #include "thread/WorkerThread.h" |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | |
Joshua McCloskey | c8c0bad | 2022-05-10 05:17:44 +0000 | [diff] [blame] | 28 | using namespace aidl::android::hardware::biometrics; |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 29 | using namespace std::chrono_literals; |
| 30 | |
| 31 | TEST(WorkerThreadTest, ScheduleReturnsTrueWhenQueueHasSpace) { |
| 32 | WorkerThread worker(1 /*maxQueueSize*/); |
| 33 | for (int i = 0; i < 100; ++i) { |
Ilya Matyukhin | 1d998ec | 2021-03-22 22:05:27 +0000 | [diff] [blame] | 34 | std::promise<void> promise; |
| 35 | auto future = promise.get_future(); |
| 36 | |
| 37 | ASSERT_TRUE(worker.schedule(Callable::from([promise = std::move(promise)]() mutable { |
| 38 | // Notify that the task has started. |
| 39 | promise.set_value(); |
| 40 | }))); |
| 41 | |
Ilya Matyukhin | 77160fa | 2021-05-10 10:07:34 -0700 | [diff] [blame] | 42 | future.wait(); |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | TEST(WorkerThreadTest, ScheduleReturnsFalseWhenQueueIsFull) { |
| 47 | WorkerThread worker(2 /*maxQueueSize*/); |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 48 | |
Ilya Matyukhin | 1d998ec | 2021-03-22 22:05:27 +0000 | [diff] [blame] | 49 | std::promise<void> promise; |
| 50 | auto future = promise.get_future(); |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 51 | |
Ilya Matyukhin | 1d998ec | 2021-03-22 22:05:27 +0000 | [diff] [blame] | 52 | // Schedule a long-running task. |
| 53 | ASSERT_TRUE(worker.schedule(Callable::from([promise = std::move(promise)]() mutable { |
| 54 | // Notify that the task has started. |
| 55 | promise.set_value(); |
| 56 | // Block for a "very long" time. |
Ilya Matyukhin | 77160fa | 2021-05-10 10:07:34 -0700 | [diff] [blame] | 57 | std::this_thread::sleep_for(1s); |
Ilya Matyukhin | 1d998ec | 2021-03-22 22:05:27 +0000 | [diff] [blame] | 58 | }))); |
| 59 | |
| 60 | // Make sure the long-running task began executing. |
Ilya Matyukhin | 77160fa | 2021-05-10 10:07:34 -0700 | [diff] [blame] | 61 | future.wait(); |
Ilya Matyukhin | 1d998ec | 2021-03-22 22:05:27 +0000 | [diff] [blame] | 62 | |
| 63 | // The first task is already being worked on, which means the queue must be empty. |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 64 | // Fill the worker's queue to the maximum. |
Ilya Matyukhin | 1d998ec | 2021-03-22 22:05:27 +0000 | [diff] [blame] | 65 | ASSERT_TRUE(worker.schedule(Callable::from([] {}))); |
| 66 | ASSERT_TRUE(worker.schedule(Callable::from([] {}))); |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 67 | |
Ilya Matyukhin | 4f5d680 | 2021-02-22 13:10:55 -0800 | [diff] [blame] | 68 | EXPECT_FALSE(worker.schedule(Callable::from([] {}))); |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | TEST(WorkerThreadTest, TasksExecuteInOrder) { |
| 72 | constexpr int NUM_TASKS = 10000; |
Tim Murray | 712f48c | 2022-04-19 17:36:33 -0700 | [diff] [blame] | 73 | WorkerThread worker(NUM_TASKS + 1); |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 74 | |
Ilya Matyukhin | 916410f | 2021-06-09 17:52:35 -0700 | [diff] [blame] | 75 | std::mutex mut; |
| 76 | std::condition_variable cv; |
| 77 | bool finished = false; |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 78 | std::vector<int> results; |
Ilya Matyukhin | 916410f | 2021-06-09 17:52:35 -0700 | [diff] [blame] | 79 | |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 80 | for (int i = 0; i < NUM_TASKS; ++i) { |
Ilya Matyukhin | 916410f | 2021-06-09 17:52:35 -0700 | [diff] [blame] | 81 | worker.schedule(Callable::from([&mut, &results, i] { |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 82 | // Delay tasks differently to provoke races. |
| 83 | std::this_thread::sleep_for(std::chrono::nanoseconds(100 - i % 100)); |
Ilya Matyukhin | 916410f | 2021-06-09 17:52:35 -0700 | [diff] [blame] | 84 | auto lock = std::lock_guard(mut); |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 85 | results.push_back(i); |
Ilya Matyukhin | 4f5d680 | 2021-02-22 13:10:55 -0800 | [diff] [blame] | 86 | })); |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 89 | // Schedule a special task to signal when all of the tasks are finished. |
Ilya Matyukhin | 916410f | 2021-06-09 17:52:35 -0700 | [diff] [blame] | 90 | worker.schedule(Callable::from([&mut, &cv, &finished] { |
| 91 | auto lock = std::lock_guard(mut); |
| 92 | finished = true; |
| 93 | cv.notify_one(); |
| 94 | })); |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 95 | |
Ilya Matyukhin | 916410f | 2021-06-09 17:52:35 -0700 | [diff] [blame] | 96 | auto lock = std::unique_lock(mut); |
| 97 | cv.wait(lock, [&finished] { return finished; }); |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 98 | ASSERT_EQ(results.size(), NUM_TASKS); |
| 99 | EXPECT_TRUE(std::is_sorted(results.begin(), results.end())); |
| 100 | } |
| 101 | |
| 102 | TEST(WorkerThreadTest, ExecutionStopsAfterWorkerIsDestroyed) { |
| 103 | std::promise<void> promise1; |
| 104 | std::promise<void> promise2; |
| 105 | auto future1 = promise1.get_future(); |
| 106 | auto future2 = promise2.get_future(); |
Ilya Matyukhin | 1d998ec | 2021-03-22 22:05:27 +0000 | [diff] [blame] | 107 | std::atomic<bool> value; |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 108 | |
Ilya Matyukhin | 1d998ec | 2021-03-22 22:05:27 +0000 | [diff] [blame] | 109 | // Local scope for the worker to test its destructor when it goes out of scope. |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 110 | { |
| 111 | WorkerThread worker(2 /*maxQueueSize*/); |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 112 | |
Ilya Matyukhin | 1d998ec | 2021-03-22 22:05:27 +0000 | [diff] [blame] | 113 | ASSERT_TRUE(worker.schedule(Callable::from([promise = std::move(promise1)]() mutable { |
| 114 | promise.set_value(); |
| 115 | std::this_thread::sleep_for(200ms); |
| 116 | }))); |
| 117 | |
| 118 | // The first task should start executing. |
Ilya Matyukhin | 77160fa | 2021-05-10 10:07:34 -0700 | [diff] [blame] | 119 | future1.wait(); |
Ilya Matyukhin | 1d998ec | 2021-03-22 22:05:27 +0000 | [diff] [blame] | 120 | |
| 121 | // The second task should schedule successfully. |
| 122 | ASSERT_TRUE( |
| 123 | worker.schedule(Callable::from([promise = std::move(promise2), &value]() mutable { |
| 124 | // The worker should destruct before it gets a chance to execute this. |
| 125 | value = true; |
| 126 | promise.set_value(); |
| 127 | }))); |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // The second task should never execute. |
Ilya Matyukhin | 77160fa | 2021-05-10 10:07:34 -0700 | [diff] [blame] | 131 | future2.wait(); |
Ilya Matyukhin | 1d998ec | 2021-03-22 22:05:27 +0000 | [diff] [blame] | 132 | // The future is expected to be ready but contain an exception. |
| 133 | // Cannot use ASSERT_THROW because exceptions are disabled in this codebase. |
| 134 | // ASSERT_THROW(future2.get(), std::future_error); |
| 135 | EXPECT_FALSE(value); |
Ilya Matyukhin | 1f3c852 | 2021-02-12 12:56:02 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | } // namespace |