| 1 |
// Copyright 2021 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "src/core/lib/promise/activity.h" |
| 16 |
|
| 17 |
#include <stdlib.h> |
| 18 |
|
| 19 |
#include <atomic> |
| 20 |
#include <chrono> |
| 21 |
#include <functional> |
| 22 |
#include <thread> |
| 23 |
#include <tuple> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
#include "gmock/gmock.h" |
| 27 |
#include "gtest/gtest.h" |
| 28 |
|
| 29 |
#include "src/core/lib/promise/join.h" |
| 30 |
#include "src/core/lib/promise/promise.h" |
| 31 |
#include "src/core/lib/promise/seq.h" |
| 32 |
#include "src/core/lib/promise/wait_set.h" |
| 33 |
#include "test/core/promise/test_wakeup_schedulers.h" |
| 34 |
|
| 35 |
using testing::_; |
| 36 |
using testing::Mock; |
| 37 |
using testing::MockFunction; |
| 38 |
using testing::SaveArg; |
| 39 |
using testing::StrictMock; |
| 40 |
|
| 41 |
namespace grpc_core { |
| 42 |
|
| 43 |
// A simple Barrier type: stalls progress until it is 'cleared'. |
| 44 |
class Barrier { |
| 45 |
public: |
| 46 |
struct Result {}; |
| 47 |
|
| 48 |
Promise<Result> Wait() { |
| 49 |
return [this]() -> Poll<Result> { |
| 50 |
MutexLock lock(&mu_); |
| 51 |
if (cleared_) { |
| 52 |
return Result{}; |
| 53 |
} else { |
| 54 |
return wait_set_.AddPending(Activity::current()->MakeOwningWaker()); |
| 55 |
} |
| 56 |
}; |
| 57 |
} |
| 58 |
|
| 59 |
void Clear() { |
| 60 |
mu_.Lock(); |
| 61 |
cleared_ = true; |
| 62 |
auto wakeup = wait_set_.TakeWakeupSet(); |
| 63 |
mu_.Unlock(); |
| 64 |
wakeup.Wakeup(); |
| 65 |
} |
| 66 |
|
| 67 |
private: |
| 68 |
Mutex mu_; |
| 69 |
WaitSet wait_set_ ABSL_GUARDED_BY(mu_); |
| 70 |
bool cleared_ ABSL_GUARDED_BY(mu_) = false; |
| 71 |
}; |
| 72 |
|
| 73 |
// A simple Barrier type: stalls progress until it is 'cleared'. |
| 74 |
// This variant supports only a single waiter. |
| 75 |
class SingleBarrier { |
| 76 |
public: |
| 77 |
struct Result {}; |
| 78 |
|
| 79 |
Promise<Result> Wait() { |
| 80 |
return [this]() -> Poll<Result> { |
| 81 |
MutexLock lock(&mu_); |
| 82 |
if (cleared_) { |
| 83 |
return Result{}; |
| 84 |
} else { |
| 85 |
waker_ = Activity::current()->MakeOwningWaker(); |
| 86 |
return Pending(); |
| 87 |
} |
| 88 |
}; |
| 89 |
} |
| 90 |
|
| 91 |
void Clear() { |
| 92 |
mu_.Lock(); |
| 93 |
cleared_ = true; |
| 94 |
auto waker = std::move(waker_); |
| 95 |
mu_.Unlock(); |
| 96 |
waker.Wakeup(); |
| 97 |
} |
| 98 |
|
| 99 |
private: |
| 100 |
Mutex mu_; |
| 101 |
Waker waker_ ABSL_GUARDED_BY(mu_); |
| 102 |
bool cleared_ ABSL_GUARDED_BY(mu_) = false; |
| 103 |
}; |
| 104 |
|
| 105 |
TEST(ActivityTest, ImmediatelyCompleteWithSuccess) { |
| 106 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 107 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 108 |
MakeActivity( |
| 109 |
[] { return [] { return absl::OkStatus(); }; }, NoWakeupScheduler(), |
| 110 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }); |
| 111 |
} |
| 112 |
|
| 113 |
TEST(ActivityTest, ImmediatelyCompleteWithFailure) { |
| 114 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 115 |
EXPECT_CALL(on_done, Call(absl::CancelledError())); |
| 116 |
MakeActivity( |
| 117 |
[] { return [] { return absl::CancelledError(); }; }, NoWakeupScheduler(), |
| 118 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }); |
| 119 |
} |
| 120 |
|
| 121 |
TEST(ActivityTest, DropImmediately) { |
| 122 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 123 |
EXPECT_CALL(on_done, Call(absl::CancelledError())); |
| 124 |
MakeActivity( |
| 125 |
[] { return []() -> Poll<absl::Status> { return Pending(); }; }, |
| 126 |
NoWakeupScheduler(), |
| 127 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }); |
| 128 |
} |
| 129 |
|
| 130 |
template <typename B> |
| 131 |
class BarrierTest : public testing::Test { |
| 132 |
public: |
| 133 |
using Type = B; |
| 134 |
}; |
| 135 |
|
| 136 |
using BarrierTestTypes = testing::Types<Barrier, SingleBarrier>; |
| 137 |
TYPED_TEST_SUITE(BarrierTest, BarrierTestTypes); |
| 138 |
|
| 139 |
TYPED_TEST(BarrierTest, Barrier) { |
| 140 |
typename TestFixture::Type b; |
| 141 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 142 |
auto activity = MakeActivity( |
| 143 |
[&b] { |
| 144 |
return Seq(b.Wait(), [](typename TestFixture::Type::Result) { |
| 145 |
return absl::OkStatus(); |
| 146 |
}); |
| 147 |
}, |
| 148 |
InlineWakeupScheduler(), |
| 149 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }); |
| 150 |
// Clearing the barrier should let the activity proceed to return a result. |
| 151 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 152 |
b.Clear(); |
| 153 |
} |
| 154 |
|
| 155 |
TYPED_TEST(BarrierTest, BarrierPing) { |
| 156 |
typename TestFixture::Type b1; |
| 157 |
typename TestFixture::Type b2; |
| 158 |
StrictMock<MockFunction<void(absl::Status)>> on_done1; |
| 159 |
StrictMock<MockFunction<void(absl::Status)>> on_done2; |
| 160 |
MockCallbackScheduler scheduler1; |
| 161 |
MockCallbackScheduler scheduler2; |
| 162 |
auto activity1 = MakeActivity( |
| 163 |
[&b1, &b2] { |
| 164 |
return Seq(b1.Wait(), [&b2](typename TestFixture::Type::Result) { |
| 165 |
// Clear the barrier whilst executing an activity |
| 166 |
b2.Clear(); |
| 167 |
return absl::OkStatus(); |
| 168 |
}); |
| 169 |
}, |
| 170 |
UseMockCallbackScheduler{&scheduler1}, |
| 171 |
[&on_done1](absl::Status status) { on_done1.Call(std::move(status)); }); |
| 172 |
auto activity2 = MakeActivity( |
| 173 |
[&b2] { |
| 174 |
return Seq(b2.Wait(), [](typename TestFixture::Type::Result) { |
| 175 |
return absl::OkStatus(); |
| 176 |
}); |
| 177 |
}, |
| 178 |
UseMockCallbackScheduler{&scheduler2}, |
| 179 |
[&on_done2](absl::Status status) { on_done2.Call(std::move(status)); }); |
| 180 |
// Since barrier triggers inside activity1 promise, activity2 wakeup will be |
| 181 |
// scheduled from a callback. |
| 182 |
std::function<void()> cb1; |
| 183 |
std::function<void()> cb2; |
| 184 |
EXPECT_CALL(scheduler1, Schedule(_)).WillOnce(SaveArg<0>(&cb1)); |
| 185 |
b1.Clear(); |
| 186 |
Mock::VerifyAndClearExpectations(&scheduler1); |
| 187 |
EXPECT_CALL(on_done1, Call(absl::OkStatus())); |
| 188 |
EXPECT_CALL(scheduler2, Schedule(_)).WillOnce(SaveArg<0>(&cb2)); |
| 189 |
cb1(); |
| 190 |
Mock::VerifyAndClearExpectations(&on_done1); |
| 191 |
EXPECT_CALL(on_done2, Call(absl::OkStatus())); |
| 192 |
cb2(); |
| 193 |
} |
| 194 |
|
| 195 |
TYPED_TEST(BarrierTest, WakeSelf) { |
| 196 |
typename TestFixture::Type b; |
| 197 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 198 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 199 |
MakeActivity( |
| 200 |
[&b] { |
| 201 |
return Seq(Join(b.Wait(), |
| 202 |
[&b] { |
| 203 |
b.Clear(); |
| 204 |
return 1; |
| 205 |
}), |
| 206 |
[](std::tuple<typename TestFixture::Type::Result, int>) { |
| 207 |
return absl::OkStatus(); |
| 208 |
}); |
| 209 |
}, |
| 210 |
NoWakeupScheduler(), |
| 211 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }); |
| 212 |
} |
| 213 |
|
| 214 |
TYPED_TEST(BarrierTest, WakeAfterDestruction) { |
| 215 |
typename TestFixture::Type b; |
| 216 |
{ |
| 217 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 218 |
EXPECT_CALL(on_done, Call(absl::CancelledError())); |
| 219 |
MakeActivity( |
| 220 |
[&b] { |
| 221 |
return Seq(b.Wait(), [](typename TestFixture::Type::Result) { |
| 222 |
return absl::OkStatus(); |
| 223 |
}); |
| 224 |
}, |
| 225 |
InlineWakeupScheduler(), |
| 226 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }); |
| 227 |
} |
| 228 |
b.Clear(); |
| 229 |
} |
| 230 |
|
| 231 |
TEST(ActivityTest, ForceWakeup) { |
| 232 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 233 |
int run_count = 0; |
| 234 |
auto activity = MakeActivity( |
| 235 |
[&run_count]() -> Poll<absl::Status> { |
| 236 |
++run_count; |
| 237 |
switch (run_count) { |
| 238 |
case 1: |
| 239 |
return Pending{}; |
| 240 |
case 2: |
| 241 |
return absl::OkStatus(); |
| 242 |
default: |
| 243 |
abort(); |
| 244 |
} |
| 245 |
}, |
| 246 |
InlineWakeupScheduler(), |
| 247 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }); |
| 248 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 249 |
activity->ForceWakeup(); |
| 250 |
} |
| 251 |
|
| 252 |
struct TestContext { |
| 253 |
bool* done; |
| 254 |
}; |
| 255 |
template <> |
| 256 |
struct ContextType<TestContext> {}; |
| 257 |
|
| 258 |
TEST(ActivityTest, WithContext) { |
| 259 |
bool done = false; |
| 260 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 261 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 262 |
MakeActivity( |
| 263 |
[] { |
| 264 |
*GetContext<TestContext>()->done = true; |
| 265 |
return Immediate(absl::OkStatus()); |
| 266 |
}, |
| 267 |
NoWakeupScheduler(), |
| 268 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }, |
| 269 |
TestContext{&done}); |
| 270 |
EXPECT_TRUE(done); |
| 271 |
} |
| 272 |
|
| 273 |
TEST(ActivityTest, CanCancelDuringExecution) { |
| 274 |
ActivityPtr activity; |
| 275 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 276 |
int run_count = 0; |
| 277 |
|
| 278 |
activity = MakeActivity( |
| 279 |
[&activity, &run_count]() -> Poll<absl::Status> { |
| 280 |
++run_count; |
| 281 |
switch (run_count) { |
| 282 |
case 1: |
| 283 |
return Pending{}; |
| 284 |
case 2: |
| 285 |
activity.reset(); |
| 286 |
return Pending{}; |
| 287 |
default: |
| 288 |
abort(); |
| 289 |
} |
| 290 |
}, |
| 291 |
InlineWakeupScheduler(), |
| 292 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }); |
| 293 |
|
| 294 |
EXPECT_CALL(on_done, Call(absl::CancelledError())); |
| 295 |
activity->ForceWakeup(); |
| 296 |
} |
| 297 |
|
| 298 |
TEST(ActivityTest, CanCancelDuringSuccessfulExecution) { |
| 299 |
ActivityPtr activity; |
| 300 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 301 |
int run_count = 0; |
| 302 |
|
| 303 |
activity = MakeActivity( |
| 304 |
[&activity, &run_count]() -> Poll<absl::Status> { |
| 305 |
++run_count; |
| 306 |
switch (run_count) { |
| 307 |
case 1: |
| 308 |
return Pending{}; |
| 309 |
case 2: |
| 310 |
activity.reset(); |
| 311 |
return absl::OkStatus(); |
| 312 |
default: |
| 313 |
abort(); |
| 314 |
} |
| 315 |
}, |
| 316 |
InlineWakeupScheduler(), |
| 317 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }); |
| 318 |
|
| 319 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 320 |
activity->ForceWakeup(); |
| 321 |
} |
| 322 |
|
| 323 |
TEST(WakerTest, CanWakeupEmptyWaker) { |
| 324 |
// Empty wakers should not do anything upon wakeup. |
| 325 |
Waker().Wakeup(); |
| 326 |
} |
| 327 |
|
| 328 |
TEST(AtomicWakerTest, CanWakeupEmptyWaker) { |
| 329 |
// Empty wakers should not do anything upon wakeup. |
| 330 |
AtomicWaker waker; |
| 331 |
EXPECT_FALSE(waker.Armed()); |
| 332 |
waker.Wakeup(); |
| 333 |
} |
| 334 |
|
| 335 |
class TestWakeable final : public Wakeable { |
| 336 |
public: |
| 337 |
TestWakeable(std::atomic<int>* wakeups, std::atomic<int>* drops) |
| 338 |
: wakeups_(wakeups), drops_(drops) {} |
| 339 |
void Wakeup() override { |
| 340 |
wakeups_->fetch_add(1, std::memory_order_relaxed); |
| 341 |
delete this; |
| 342 |
} |
| 343 |
void Drop() override { |
| 344 |
drops_->fetch_add(1, std::memory_order_relaxed); |
| 345 |
delete this; |
| 346 |
} |
| 347 |
std::string ActivityDebugTag() const override { return "TestWakeable"; } |
| 348 |
|
| 349 |
private: |
| 350 |
std::atomic<int>* const wakeups_; |
| 351 |
std::atomic<int>* const drops_; |
| 352 |
}; |
| 353 |
|
| 354 |
TEST(AtomicWakerTest, ThreadStress) { |
| 355 |
std::vector<std::thread> threads; |
| 356 |
std::atomic<bool> done{false}; |
| 357 |
std::atomic<int> wakeups{0}; |
| 358 |
std::atomic<int> drops{0}; |
| 359 |
std::atomic<int> armed{0}; |
| 360 |
std::atomic<int> not_armed{0}; |
| 361 |
AtomicWaker waker; |
| 362 |
|
| 363 |
threads.reserve(15); |
| 364 |
for (int i = 0; i < 5; i++) { |
| 365 |
threads.emplace_back([&] { |
| 366 |
while (!done.load(std::memory_order_relaxed)) { |
| 367 |
waker.Wakeup(); |
| 368 |
std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 369 |
} |
| 370 |
}); |
| 371 |
} |
| 372 |
for (int i = 0; i < 5; i++) { |
| 373 |
threads.emplace_back([&] { |
| 374 |
while (!done.load(std::memory_order_relaxed)) { |
| 375 |
waker.Set(Waker(new TestWakeable(&wakeups, &drops))); |
| 376 |
std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 377 |
} |
| 378 |
}); |
| 379 |
} |
| 380 |
for (int i = 0; i < 5; i++) { |
| 381 |
threads.emplace_back([&] { |
| 382 |
while (!done.load(std::memory_order_relaxed)) { |
| 383 |
(waker.Armed() ? &armed : ¬_armed) |
| 384 |
->fetch_add(1, std::memory_order_relaxed); |
| 385 |
std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 386 |
} |
| 387 |
}); |
| 388 |
} |
| 389 |
|
| 390 |
do { |
| 391 |
std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 392 |
} while (wakeups.load(std::memory_order_relaxed) == 0 || |
| 393 |
armed.load(std::memory_order_relaxed) == 0 || |
| 394 |
not_armed.load(std::memory_order_relaxed) == 0); |
| 395 |
done.store(true, std::memory_order_relaxed); |
| 396 |
for (auto& t : threads) t.join(); |
| 397 |
} |
| 398 |
|
| 399 |
} // namespace grpc_core |
| 400 |
|
| 401 |
int main(int argc, char** argv) { |
| 402 |
::testing::InitGoogleTest(&argc, argv); |
| 403 |
return RUN_ALL_TESTS(); |
| 404 |
} |