1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include "thread/CommonPool.h"
#include <array>
#include <condition_variable>
#include <set>
#include <thread>
#include "unistd.h"
using namespace android;
using namespace android::uirenderer;
TEST(CommonPool, post) {
std::atomic_bool ran(false);
CommonPool::post([&ran] { ran = true; });
for (int i = 0; !ran && i < 1000; i++) {
usleep(1);
}
EXPECT_TRUE(ran) << "Failed to flip atomic after 1 second";
}
// test currently relies on timings, which
// makes it flaky. Disable for now
TEST(DISABLED_CommonPool, threadCount) {
std::set<pid_t> threads;
std::array<std::future<pid_t>, 64> futures;
for (int i = 0; i < futures.size(); i++) {
futures[i] = CommonPool::async([] {
usleep(10);
return gettid();
});
}
for (auto& f : futures) {
threads.insert(f.get());
}
EXPECT_EQ(threads.size(), CommonPool::THREAD_COUNT);
EXPECT_EQ(0, threads.count(gettid()));
}
// Disabled since this is flaky. This isn't a necessarily useful functional test, so being
// disabled isn't that significant. However it may be good to resurrect this somehow.
TEST(CommonPool, DISABLED_singleThread) {
std::mutex mutex;
std::condition_variable fence;
bool isProcessing = false;
bool queuedSecond = false;
auto f1 = CommonPool::async([&] {
{
std::unique_lock lock{mutex};
isProcessing = true;
fence.notify_all();
while (!queuedSecond) {
fence.wait(lock);
}
}
return gettid();
});
{
std::unique_lock lock{mutex};
while (!isProcessing) {
fence.wait(lock);
}
}
auto f2 = CommonPool::async([] {
return gettid();
});
{
std::unique_lock lock{mutex};
queuedSecond = true;
fence.notify_all();
}
auto tid1 = f1.get();
auto tid2 = f2.get();
EXPECT_EQ(tid1, tid2);
EXPECT_NE(gettid(), tid1);
}
// Test currently relies on timings
// which makes it flaky, disable for now
TEST(DISABLED_CommonPool, fullQueue) {
std::mutex lock;
std::condition_variable fence;
bool signaled = false;
static constexpr auto QUEUE_COUNT = CommonPool::THREAD_COUNT + CommonPool::QUEUE_SIZE + 10;
std::atomic_int queuedCount{0};
std::array<std::future<void>, QUEUE_COUNT> futures;
std::thread queueThread{[&] {
for (int i = 0; i < QUEUE_COUNT; i++) {
futures[i] = CommonPool::async([&] {
std::unique_lock _lock{lock};
while (!signaled) {
fence.wait(_lock);
}
});
queuedCount++;
}
}};
int previous;
do {
previous = queuedCount.load();
usleep(10000);
} while (previous != queuedCount.load());
EXPECT_GT(queuedCount.load(), CommonPool::QUEUE_SIZE);
EXPECT_LT(queuedCount.load(), QUEUE_COUNT);
{
std::unique_lock _lock{lock};
signaled = true;
fence.notify_all();
}
queueThread.join();
EXPECT_EQ(queuedCount.load(), QUEUE_COUNT);
// Ensure all our tasks are finished before return as they have references to the stack
for (auto& f : futures) {
f.get();
}
}
class ObjectTracker {
static std::atomic_int sGlobalCount;
public:
ObjectTracker() {
sGlobalCount++;
}
ObjectTracker(const ObjectTracker&) {
sGlobalCount++;
}
ObjectTracker(ObjectTracker&&) {
sGlobalCount++;
}
~ObjectTracker() {
sGlobalCount--;
}
static int count() { return sGlobalCount.load(); }
};
std::atomic_int ObjectTracker::sGlobalCount{0};
TEST(CommonPool, asyncLifecycleCheck) {
ASSERT_EQ(0, ObjectTracker::count());
{
ObjectTracker obj;
ASSERT_EQ(1, ObjectTracker::count());
EXPECT_LT(1, CommonPool::async([obj] { return ObjectTracker::count(); }).get());
}
CommonPool::waitForIdle();
ASSERT_EQ(0, ObjectTracker::count());
}
TEST(CommonPool, syncLifecycleCheck) {
ASSERT_EQ(0, ObjectTracker::count());
{
ObjectTracker obj;
ASSERT_EQ(1, ObjectTracker::count());
EXPECT_LT(1, CommonPool::runSync([obj] { return ObjectTracker::count(); }));
}
CommonPool::waitForIdle();
ASSERT_EQ(0, ObjectTracker::count());
}
|