| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2019 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "src/core/lib/gprpp/work_serializer.h" |
| 20 |
|
| 21 |
#include <stddef.h> |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <memory> |
| 25 |
#include <thread> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
#include "absl/synchronization/barrier.h" |
| 29 |
#include "gtest/gtest.h" |
| 30 |
|
| 31 |
#include <grpc/grpc.h> |
| 32 |
#include <grpc/support/sync.h> |
| 33 |
#include <grpc/support/time.h> |
| 34 |
|
| 35 |
#include "src/core/lib/gprpp/notification.h" |
| 36 |
#include "src/core/lib/gprpp/thd.h" |
| 37 |
#include "test/core/util/test_config.h" |
| 38 |
|
| 39 |
namespace { |
| 40 |
TEST(WorkSerializerTest, NoOp) { grpc_core::WorkSerializer lock; } |
| 41 |
|
| 42 |
TEST(WorkSerializerTest, ExecuteOneRun) { |
| 43 |
grpc_core::WorkSerializer lock; |
| 44 |
gpr_event done; |
| 45 |
gpr_event_init(&done); |
| 46 |
lock.Run([&done]() { gpr_event_set(&done, reinterpret_cast<void*>(1)); }, |
| 47 |
DEBUG_LOCATION); |
| 48 |
EXPECT_TRUE(gpr_event_wait(&done, grpc_timeout_seconds_to_deadline(5)) != |
| 49 |
nullptr); |
| 50 |
} |
| 51 |
|
| 52 |
TEST(WorkSerializerTest, ExecuteOneScheduleAndDrain) { |
| 53 |
grpc_core::WorkSerializer lock; |
| 54 |
gpr_event done; |
| 55 |
gpr_event_init(&done); |
| 56 |
lock.Schedule([&done]() { gpr_event_set(&done, reinterpret_cast<void*>(1)); }, |
| 57 |
DEBUG_LOCATION); |
| 58 |
EXPECT_EQ(gpr_event_get(&done), nullptr); |
| 59 |
lock.DrainQueue(); |
| 60 |
EXPECT_TRUE(gpr_event_wait(&done, grpc_timeout_seconds_to_deadline(5)) != |
| 61 |
nullptr); |
| 62 |
} |
| 63 |
|
| 64 |
class TestThread { |
| 65 |
public: |
| 66 |
explicit TestThread(grpc_core::WorkSerializer* lock) |
| 67 |
: lock_(lock), thread_("grpc_execute_many", ExecuteManyLoop, this) { |
| 68 |
gpr_event_init(&done_); |
| 69 |
thread_.Start(); |
| 70 |
} |
| 71 |
|
| 72 |
~TestThread() { |
| 73 |
EXPECT_NE(gpr_event_wait(&done_, gpr_inf_future(GPR_CLOCK_REALTIME)), |
| 74 |
nullptr); |
| 75 |
thread_.Join(); |
| 76 |
} |
| 77 |
|
| 78 |
private: |
| 79 |
static void ExecuteManyLoop(void* arg) { |
| 80 |
TestThread* self = static_cast<TestThread*>(arg); |
| 81 |
size_t n = 1; |
| 82 |
for (size_t i = 0; i < 10; i++) { |
| 83 |
for (size_t j = 0; j < 10000; j++) { |
| 84 |
struct ExecutionArgs { |
| 85 |
size_t* counter; |
| 86 |
size_t value; |
| 87 |
}; |
| 88 |
ExecutionArgs* c = new ExecutionArgs; |
| 89 |
c->counter = &self->counter_; |
| 90 |
c->value = n++; |
| 91 |
self->lock_->Run( |
| 92 |
[c]() { |
| 93 |
EXPECT_TRUE(*c->counter == c->value - 1); |
| 94 |
*c->counter = c->value; |
| 95 |
delete c; |
| 96 |
}, |
| 97 |
DEBUG_LOCATION); |
| 98 |
} |
| 99 |
// sleep for a little bit, to test other threads picking up the load |
| 100 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100)); |
| 101 |
} |
| 102 |
self->lock_->Run( |
| 103 |
[self]() { gpr_event_set(&self->done_, reinterpret_cast<void*>(1)); }, |
| 104 |
DEBUG_LOCATION); |
| 105 |
} |
| 106 |
|
| 107 |
grpc_core::WorkSerializer* lock_ = nullptr; |
| 108 |
grpc_core::Thread thread_; |
| 109 |
size_t counter_ = 0; |
| 110 |
gpr_event done_; |
| 111 |
}; |
| 112 |
|
| 113 |
TEST(WorkSerializerTest, ExecuteMany) { |
| 114 |
grpc_core::WorkSerializer lock; |
| 115 |
{ |
| 116 |
std::vector<std::unique_ptr<TestThread>> threads; |
| 117 |
for (size_t i = 0; i < 10; ++i) { |
| 118 |
threads.push_back(std::make_unique<TestThread>(&lock)); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
class TestThreadScheduleAndDrain { |
| 124 |
public: |
| 125 |
explicit TestThreadScheduleAndDrain(grpc_core::WorkSerializer* lock) |
| 126 |
: lock_(lock), thread_("grpc_execute_many", ExecuteManyLoop, this) { |
| 127 |
gpr_event_init(&done_); |
| 128 |
thread_.Start(); |
| 129 |
} |
| 130 |
|
| 131 |
~TestThreadScheduleAndDrain() { |
| 132 |
EXPECT_NE(gpr_event_wait(&done_, gpr_inf_future(GPR_CLOCK_REALTIME)), |
| 133 |
nullptr); |
| 134 |
thread_.Join(); |
| 135 |
} |
| 136 |
|
| 137 |
private: |
| 138 |
static void ExecuteManyLoop(void* arg) { |
| 139 |
TestThreadScheduleAndDrain* self = |
| 140 |
static_cast<TestThreadScheduleAndDrain*>(arg); |
| 141 |
size_t n = 1; |
| 142 |
for (size_t i = 0; i < 10; i++) { |
| 143 |
for (size_t j = 0; j < 10000; j++) { |
| 144 |
struct ExecutionArgs { |
| 145 |
size_t* counter; |
| 146 |
size_t value; |
| 147 |
}; |
| 148 |
ExecutionArgs* c = new ExecutionArgs; |
| 149 |
c->counter = &self->counter_; |
| 150 |
c->value = n++; |
| 151 |
self->lock_->Schedule( |
| 152 |
[c]() { |
| 153 |
EXPECT_TRUE(*c->counter == c->value - 1); |
| 154 |
*c->counter = c->value; |
| 155 |
delete c; |
| 156 |
}, |
| 157 |
DEBUG_LOCATION); |
| 158 |
} |
| 159 |
self->lock_->DrainQueue(); |
| 160 |
// sleep for a little bit, to test other threads picking up the load |
| 161 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100)); |
| 162 |
} |
| 163 |
self->lock_->Run( |
| 164 |
[self]() { gpr_event_set(&self->done_, reinterpret_cast<void*>(1)); }, |
| 165 |
DEBUG_LOCATION); |
| 166 |
} |
| 167 |
|
| 168 |
grpc_core::WorkSerializer* lock_ = nullptr; |
| 169 |
grpc_core::Thread thread_; |
| 170 |
size_t counter_ = 0; |
| 171 |
gpr_event done_; |
| 172 |
}; |
| 173 |
|
| 174 |
TEST(WorkSerializerTest, ExecuteManyScheduleAndDrain) { |
| 175 |
grpc_core::WorkSerializer lock; |
| 176 |
{ |
| 177 |
std::vector<std::unique_ptr<TestThreadScheduleAndDrain>> threads; |
| 178 |
for (size_t i = 0; i < 10; ++i) { |
| 179 |
threads.push_back(std::make_unique<TestThreadScheduleAndDrain>(&lock)); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
TEST(WorkSerializerTest, ExecuteManyMixedRunScheduleAndDrain) { |
| 185 |
grpc_core::WorkSerializer lock; |
| 186 |
{ |
| 187 |
std::vector<std::unique_ptr<TestThread>> run_threads; |
| 188 |
std::vector<std::unique_ptr<TestThreadScheduleAndDrain>> schedule_threads; |
| 189 |
for (size_t i = 0; i < 10; ++i) { |
| 190 |
run_threads.push_back(std::make_unique<TestThread>(&lock)); |
| 191 |
schedule_threads.push_back( |
| 192 |
std::make_unique<TestThreadScheduleAndDrain>(&lock)); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
// Tests that work serializers allow destruction from the last callback |
| 198 |
TEST(WorkSerializerTest, CallbackDestroysWorkSerializer) { |
| 199 |
auto lock = std::make_shared<grpc_core::WorkSerializer>(); |
| 200 |
lock->Run([&]() { lock.reset(); }, DEBUG_LOCATION); |
| 201 |
} |
| 202 |
|
| 203 |
// Tests additional racy conditions when the last callback triggers work |
| 204 |
// serializer destruction. |
| 205 |
TEST(WorkSerializerTest, WorkSerializerDestructionRace) { |
| 206 |
for (int i = 0; i < 1000; ++i) { |
| 207 |
auto lock = std::make_shared<grpc_core::WorkSerializer>(); |
| 208 |
grpc_core::Notification notification; |
| 209 |
std::thread t1([&]() { |
| 210 |
notification.WaitForNotification(); |
| 211 |
lock.reset(); |
| 212 |
}); |
| 213 |
lock->Run([&]() { notification.Notify(); }, DEBUG_LOCATION); |
| 214 |
t1.join(); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// Tests racy conditions when the last callback triggers work |
| 219 |
// serializer destruction. |
| 220 |
TEST(WorkSerializerTest, WorkSerializerDestructionRaceMultipleThreads) { |
| 221 |
auto lock = std::make_shared<grpc_core::WorkSerializer>(); |
| 222 |
absl::Barrier barrier(11); |
| 223 |
std::vector<std::thread> threads; |
| 224 |
threads.reserve(10); |
| 225 |
for (int i = 0; i < 10; ++i) { |
| 226 |
threads.emplace_back([lock, &barrier]() mutable { |
| 227 |
barrier.Block(); |
| 228 |
lock->Run([lock]() mutable { lock.reset(); }, DEBUG_LOCATION); |
| 229 |
}); |
| 230 |
} |
| 231 |
barrier.Block(); |
| 232 |
lock.reset(); |
| 233 |
for (auto& thread : threads) { |
| 234 |
thread.join(); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
} // namespace |
| 239 |
|
| 240 |
int main(int argc, char** argv) { |
| 241 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 242 |
::testing::InitGoogleTest(&argc, argv); |
| 243 |
grpc_init(); |
| 244 |
int retval = RUN_ALL_TESTS(); |
| 245 |
grpc_shutdown(); |
| 246 |
return retval; |
| 247 |
} |