10#include "llvm/ADT/DenseMap.h"
11#include "gmock/gmock.h"
12#include "gtest/gtest.h"
21 const int TasksCnt = 100;
26 int IncrementsPerTask = 1000;
32 auto ScheduleIncrements = [&]() {
33 for (
int TaskI = 0; TaskI < TasksCnt; ++TaskI) {
35 for (
int Increment = 0; Increment < IncrementsPerTask; ++Increment) {
36 std::lock_guard<std::mutex> Lock(Mutex);
46 std::lock_guard<std::mutex> Lock(Mutex);
52 std::lock_guard<std::mutex> Lock(Mutex);
53 ASSERT_EQ(
Counter, TasksCnt * IncrementsPerTask);
57 std::lock_guard<std::mutex> Lock(Mutex);
63 std::lock_guard<std::mutex> Lock(Mutex);
64 ASSERT_EQ(
Counter, TasksCnt * IncrementsPerTask);
68 const unsigned NumThreads = 5;
69 const unsigned NumKeys = 100;
70 const unsigned NumIterations = 100;
73 std::atomic<unsigned> ComputeCount(0);
74 std::atomic<int> ComputeResult[NumKeys];
75 std::fill(std::begin(ComputeResult), std::end(ComputeResult), -1);
78 for (
unsigned I = 0; I < NumThreads; ++I)
79 Tasks.
runAsync(
"worker" + std::to_string(I), [&] {
80 for (unsigned J = 0; J < NumIterations; J++)
81 for (unsigned K = 0; K < NumKeys; K++) {
82 int Result = Cache.get(K, [&] { return ++ComputeCount; });
83 EXPECT_THAT(ComputeResult[K].exchange(Result),
84 testing::AnyOf(-1, Result))
85 <<
"Got inconsistent results from memoize";
89 EXPECT_GE(ComputeCount, NumKeys) <<
"Computed each key once";
90 EXPECT_LE(ComputeCount, NumThreads * NumKeys)
91 <<
"Worst case, computed each key in every thread";
92 for (
int Result : ComputeResult)
93 EXPECT_GT(Result, 0) <<
"All results in expected domain";
103 std::atomic<char> ValueA(0), ValueB(0);
105 Tasks.runAsync(
"A", [&] {
106 ValueA = Cache.get(0, [&] {
112 Tasks.runAsync(
"A", [&] {
113 ValueB = Cache.get(0, [&] {
121 ASSERT_EQ(ValueA, ValueB);
122 ASSERT_THAT(ValueA.load(), testing::AnyOf(
'A',
'B'));
127TEST(PeriodicThrottlerTest, Minimal) {
130 EXPECT_FALSE(Once());
131 EXPECT_FALSE(Once());
134 std::chrono::hours(24));
135 EXPECT_FALSE(Later());
136 EXPECT_FALSE(Later());
137 EXPECT_FALSE(Later());
140 EXPECT_TRUE(Always());
141 EXPECT_TRUE(Always());
142 EXPECT_TRUE(Always());
Runs tasks on separate (detached) threads and wait for all tasks to finish.
void runAsync(const llvm::Twine &Name, llvm::unique_function< void()> Action)
Memoize is a cache to store and reuse computation results based on a key.
A threadsafe flag that is initially clear.
Used to guard an operation that should run at most every N seconds.
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
TEST(BackgroundQueueTest, Priority)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//