| 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 <algorithm> |
| 16 |
#include <atomic> |
| 17 |
#include <chrono> |
| 18 |
#include <cstdint> |
| 19 |
#include <memory> |
| 20 |
#include <random> |
| 21 |
#include <ratio> |
| 22 |
#include <thread> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include "absl/base/thread_annotations.h" |
| 26 |
#include "absl/functional/bind_front.h" |
| 27 |
#include "absl/time/clock.h" |
| 28 |
#include "absl/time/time.h" |
| 29 |
#include "gmock/gmock.h" |
| 30 |
#include "gtest/gtest.h" |
| 31 |
|
| 32 |
#include <grpc/event_engine/event_engine.h> |
| 33 |
#include <grpc/support/log.h> |
| 34 |
|
| 35 |
#include "src/core/lib/gprpp/sync.h" |
| 36 |
#include "test/core/event_engine/test_suite/event_engine_test.h" |
| 37 |
|
| 38 |
using ::testing::ElementsAre; |
| 39 |
using namespace std::chrono_literals; |
| 40 |
|
| 41 |
class EventEngineTimerTest : public EventEngineTest { |
| 42 |
public: |
| 43 |
void ScheduleCheckCB(std::chrono::steady_clock::time_point when, |
| 44 |
std::atomic<int>* call_count, |
| 45 |
std::atomic<int>* fail_count, int total_expected); |
| 46 |
|
| 47 |
protected: |
| 48 |
void WaitForSignalled(absl::Duration timeout) |
| 49 |
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 50 |
absl::Time deadline = absl::Now() + timeout; |
| 51 |
while (!signaled_) { |
| 52 |
timeout = deadline - absl::Now(); |
| 53 |
ASSERT_GT(timeout, absl::ZeroDuration()); |
| 54 |
cv_.WaitWithTimeout(&mu_, timeout); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
grpc_core::Mutex mu_; |
| 59 |
grpc_core::CondVar cv_; |
| 60 |
bool signaled_ ABSL_GUARDED_BY(mu_) = false; |
| 61 |
}; |
| 62 |
|
| 63 |
TEST_F(EventEngineTimerTest, ImmediateCallbackIsExecutedQuickly) { |
| 64 |
auto engine = this->NewEventEngine(); |
| 65 |
grpc_core::MutexLock lock(&mu_); |
| 66 |
engine->RunAfter(0ms, [this]() { |
| 67 |
grpc_core::MutexLock lock(&mu_); |
| 68 |
signaled_ = true; |
| 69 |
cv_.Signal(); |
| 70 |
}); |
| 71 |
WaitForSignalled(absl::Seconds(5)); |
| 72 |
} |
| 73 |
|
| 74 |
TEST_F(EventEngineTimerTest, SupportsCancellation) { |
| 75 |
auto engine = this->NewEventEngine(); |
| 76 |
auto handle = engine->RunAfter(24h, []() {}); |
| 77 |
ASSERT_TRUE(engine->Cancel(handle)); |
| 78 |
} |
| 79 |
|
| 80 |
TEST_F(EventEngineTimerTest, CancelledCallbackIsNotExecuted) { |
| 81 |
{ |
| 82 |
auto engine = this->NewEventEngine(); |
| 83 |
auto handle = engine->RunAfter(24h, [this]() { |
| 84 |
grpc_core::MutexLock lock(&mu_); |
| 85 |
signaled_ = true; |
| 86 |
}); |
| 87 |
ASSERT_TRUE(engine->Cancel(handle)); |
| 88 |
} |
| 89 |
// The engine is deleted, and all closures should have been flushed |
| 90 |
grpc_core::MutexLock lock(&mu_); |
| 91 |
ASSERT_FALSE(signaled_); |
| 92 |
} |
| 93 |
|
| 94 |
TEST_F(EventEngineTimerTest, TimersRespectScheduleOrdering) { |
| 95 |
// Note: this is a brittle test if the first call to `RunAfter` takes longer |
| 96 |
// than the second callback's wait time. |
| 97 |
std::vector<uint8_t> ordered; |
| 98 |
uint8_t count = 0; |
| 99 |
grpc_core::MutexLock lock(&mu_); |
| 100 |
{ |
| 101 |
auto engine = this->NewEventEngine(); |
| 102 |
engine->RunAfter(3000ms, [&]() { |
| 103 |
grpc_core::MutexLock lock(&mu_); |
| 104 |
ordered.push_back(2); |
| 105 |
++count; |
| 106 |
cv_.Signal(); |
| 107 |
}); |
| 108 |
engine->RunAfter(0ms, [&]() { |
| 109 |
grpc_core::MutexLock lock(&mu_); |
| 110 |
ordered.push_back(1); |
| 111 |
++count; |
| 112 |
cv_.Signal(); |
| 113 |
}); |
| 114 |
// Ensure both callbacks have run. |
| 115 |
while (count != 2) { |
| 116 |
cv_.WaitWithTimeout(&mu_, absl::Milliseconds(8)); |
| 117 |
} |
| 118 |
} |
| 119 |
// The engine is deleted, and all closures should have been flushed beforehand |
| 120 |
ASSERT_THAT(ordered, ElementsAre(1, 2)); |
| 121 |
} |
| 122 |
|
| 123 |
TEST_F(EventEngineTimerTest, CancellingExecutedCallbackIsNoopAndReturnsFalse) { |
| 124 |
auto engine = this->NewEventEngine(); |
| 125 |
grpc_core::MutexLock lock(&mu_); |
| 126 |
auto handle = engine->RunAfter(0ms, [this]() { |
| 127 |
grpc_core::MutexLock lock(&mu_); |
| 128 |
signaled_ = true; |
| 129 |
cv_.Signal(); |
| 130 |
}); |
| 131 |
WaitForSignalled(absl::Seconds(10)); |
| 132 |
// The callback has run, and now we'll try to cancel it. |
| 133 |
ASSERT_FALSE(engine->Cancel(handle)); |
| 134 |
} |
| 135 |
|
| 136 |
void EventEngineTimerTest::ScheduleCheckCB( |
| 137 |
std::chrono::steady_clock::time_point when, std::atomic<int>* call_count, |
| 138 |
std::atomic<int>* fail_count, int total_expected) { |
| 139 |
auto now = std::chrono::steady_clock::now(); |
| 140 |
EXPECT_LE(when, now); |
| 141 |
if (when > now) ++(*fail_count); |
| 142 |
if (++(*call_count) == total_expected) { |
| 143 |
grpc_core::MutexLock lock(&mu_); |
| 144 |
signaled_ = true; |
| 145 |
cv_.Signal(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
TEST_F(EventEngineTimerTest, StressTestTimersNotCalledBeforeScheduled) { |
| 150 |
auto engine = this->NewEventEngine(); |
| 151 |
constexpr int thread_count = 10; |
| 152 |
constexpr int call_count_per_thread = 100; |
| 153 |
constexpr float timeout_min_seconds = 1; |
| 154 |
constexpr float timeout_max_seconds = 10; |
| 155 |
std::atomic<int> call_count{0}; |
| 156 |
std::atomic<int> failed_call_count{0}; |
| 157 |
std::vector<std::thread> threads; |
| 158 |
threads.reserve(thread_count); |
| 159 |
for (int thread_n = 0; thread_n < thread_count; ++thread_n) { |
| 160 |
threads.emplace_back([&]() { |
| 161 |
std::random_device rd; |
| 162 |
std::mt19937 gen(rd()); |
| 163 |
std::uniform_real_distribution<> dis(timeout_min_seconds, |
| 164 |
timeout_max_seconds); |
| 165 |
for (int call_n = 0; call_n < call_count_per_thread; ++call_n) { |
| 166 |
const auto dur = static_cast<int64_t>(1e9 * dis(gen)); |
| 167 |
auto deadline = |
| 168 |
std::chrono::steady_clock::now() + std::chrono::nanoseconds(dur); |
| 169 |
engine->RunAfter( |
| 170 |
std::chrono::nanoseconds(dur), |
| 171 |
absl::bind_front(&EventEngineTimerTest::ScheduleCheckCB, this, |
| 172 |
deadline, &call_count, &failed_call_count, |
| 173 |
thread_count * call_count_per_thread)); |
| 174 |
} |
| 175 |
}); |
| 176 |
} |
| 177 |
for (auto& t : threads) { |
| 178 |
t.join(); |
| 179 |
} |
| 180 |
grpc_core::MutexLock lock(&mu_); |
| 181 |
// to protect against spurious wakeups. |
| 182 |
while (!signaled_) { |
| 183 |
cv_.Wait(&mu_); |
| 184 |
} |
| 185 |
if (failed_call_count.load() != 0) { |
| 186 |
gpr_log(GPR_DEBUG, "failed timer count: %d of %d", failed_call_count.load(), |
| 187 |
thread_count * call_count); |
| 188 |
} |
| 189 |
ASSERT_EQ(0, failed_call_count.load()); |
| 190 |
} |