| 1 |
// Copyright 2022 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/sleep.h" |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
#include <cstddef> |
| 19 |
#include <memory> |
| 20 |
#include <utility> |
| 21 |
#include <vector> |
| 22 |
|
| 23 |
#include "gmock/gmock.h" |
| 24 |
#include "gtest/gtest.h" |
| 25 |
|
| 26 |
#include <grpc/grpc.h> |
| 27 |
|
| 28 |
#include "src/core/lib/event_engine/default_event_engine.h" |
| 29 |
#include "src/core/lib/gprpp/notification.h" |
| 30 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 31 |
#include "src/core/lib/promise/exec_ctx_wakeup_scheduler.h" |
| 32 |
#include "src/core/lib/promise/race.h" |
| 33 |
#include "test/core/event_engine/mock_event_engine.h" |
| 34 |
#include "test/core/promise/test_wakeup_schedulers.h" |
| 35 |
|
| 36 |
using grpc_event_engine::experimental::EventEngine; |
| 37 |
using grpc_event_engine::experimental::GetDefaultEventEngine; |
| 38 |
using grpc_event_engine::experimental::MockEventEngine; |
| 39 |
using testing::_; |
| 40 |
using testing::DoAll; |
| 41 |
using testing::Matcher; |
| 42 |
using testing::Mock; |
| 43 |
using testing::Return; |
| 44 |
using testing::SaveArg; |
| 45 |
using testing::StrictMock; |
| 46 |
|
| 47 |
namespace grpc_core { |
| 48 |
namespace { |
| 49 |
|
| 50 |
TEST(Sleep, Zzzz) { |
| 51 |
ExecCtx exec_ctx; |
| 52 |
Notification done; |
| 53 |
Timestamp done_time = Timestamp::Now() + Duration::Seconds(1); |
| 54 |
auto engine = GetDefaultEventEngine(); |
| 55 |
// Sleep for one second then set done to true. |
| 56 |
auto activity = MakeActivity( |
| 57 |
Sleep(done_time), InlineWakeupScheduler(), |
| 58 |
[&done](absl::Status r) { |
| 59 |
EXPECT_EQ(r, absl::OkStatus()); |
| 60 |
done.Notify(); |
| 61 |
}, |
| 62 |
engine.get()); |
| 63 |
done.WaitForNotification(); |
| 64 |
exec_ctx.InvalidateNow(); |
| 65 |
EXPECT_GE(Timestamp::Now(), done_time); |
| 66 |
} |
| 67 |
|
| 68 |
TEST(Sleep, OverlyEagerEventEngine) { |
| 69 |
StrictMock<MockEventEngine> mock_event_engine; |
| 70 |
|
| 71 |
ExecCtx exec_ctx; |
| 72 |
bool done = false; |
| 73 |
// Schedule a sleep for a very long time. |
| 74 |
Timestamp done_time = Timestamp::Now() + Duration::Seconds(1e6); |
| 75 |
EventEngine::Closure* wakeup = nullptr; |
| 76 |
EXPECT_CALL(mock_event_engine, RunAfter(_, Matcher<EventEngine::Closure*>(_))) |
| 77 |
.WillOnce( |
| 78 |
DoAll(SaveArg<1>(&wakeup), Return(EventEngine::TaskHandle{42, 123}))); |
| 79 |
auto activity = MakeActivity( |
| 80 |
Sleep(done_time), InlineWakeupScheduler(), |
| 81 |
[&done](absl::Status r) { |
| 82 |
EXPECT_EQ(r, absl::OkStatus()); |
| 83 |
done = true; |
| 84 |
}, |
| 85 |
static_cast<EventEngine*>(&mock_event_engine)); |
| 86 |
Mock::VerifyAndClearExpectations(&mock_event_engine); |
| 87 |
EXPECT_NE(wakeup, nullptr); |
| 88 |
EXPECT_FALSE(done); |
| 89 |
// Schedule the wakeup instantaneously - It won't have passed the scheduled |
| 90 |
// time yet, but sleep should believe the event engine. |
| 91 |
wakeup->Run(); |
| 92 |
EXPECT_TRUE(done); |
| 93 |
} |
| 94 |
|
| 95 |
TEST(Sleep, AlreadyDone) { |
| 96 |
ExecCtx exec_ctx; |
| 97 |
Notification done; |
| 98 |
Timestamp done_time = Timestamp::Now() - Duration::Seconds(1); |
| 99 |
auto engine = GetDefaultEventEngine(); |
| 100 |
// Sleep for no time at all then set done to true. |
| 101 |
auto activity = MakeActivity( |
| 102 |
Sleep(done_time), InlineWakeupScheduler(), |
| 103 |
[&done](absl::Status r) { |
| 104 |
EXPECT_EQ(r, absl::OkStatus()); |
| 105 |
done.Notify(); |
| 106 |
}, |
| 107 |
engine.get()); |
| 108 |
done.WaitForNotification(); |
| 109 |
} |
| 110 |
|
| 111 |
TEST(Sleep, Cancel) { |
| 112 |
ExecCtx exec_ctx; |
| 113 |
Notification done; |
| 114 |
Timestamp done_time = Timestamp::Now() + Duration::Seconds(1); |
| 115 |
auto engine = GetDefaultEventEngine(); |
| 116 |
// Sleep for one second but race it to complete immediately |
| 117 |
auto activity = MakeActivity( |
| 118 |
Race(Sleep(done_time), [] { return absl::CancelledError(); }), |
| 119 |
InlineWakeupScheduler(), |
| 120 |
[&done](absl::Status r) { |
| 121 |
EXPECT_EQ(r, absl::CancelledError()); |
| 122 |
done.Notify(); |
| 123 |
}, |
| 124 |
engine.get()); |
| 125 |
done.WaitForNotification(); |
| 126 |
exec_ctx.InvalidateNow(); |
| 127 |
EXPECT_LT(Timestamp::Now(), done_time); |
| 128 |
} |
| 129 |
|
| 130 |
TEST(Sleep, MoveSemantics) { |
| 131 |
// ASAN should help determine if there are any memory leaks here |
| 132 |
ExecCtx exec_ctx; |
| 133 |
Notification done; |
| 134 |
Timestamp done_time = Timestamp::Now() + Duration::Milliseconds(111); |
| 135 |
Sleep donor(done_time); |
| 136 |
Sleep sleeper = std::move(donor); |
| 137 |
auto engine = GetDefaultEventEngine(); |
| 138 |
auto activity = MakeActivity( |
| 139 |
std::move(sleeper), InlineWakeupScheduler(), |
| 140 |
[&done](absl::Status r) { |
| 141 |
EXPECT_EQ(r, absl::OkStatus()); |
| 142 |
done.Notify(); |
| 143 |
}, |
| 144 |
engine.get()); |
| 145 |
done.WaitForNotification(); |
| 146 |
exec_ctx.InvalidateNow(); |
| 147 |
EXPECT_GE(Timestamp::Now(), done_time); |
| 148 |
} |
| 149 |
|
| 150 |
TEST(Sleep, StressTest) { |
| 151 |
// Kick off a bunch sleeps for one second. |
| 152 |
static const int kNumActivities = 100000; |
| 153 |
ExecCtx exec_ctx; |
| 154 |
std::vector<std::shared_ptr<Notification>> notifications; |
| 155 |
std::vector<ActivityPtr> activities; |
| 156 |
auto engine = GetDefaultEventEngine(); |
| 157 |
gpr_log(GPR_INFO, "Starting %d sleeps for 1sec", kNumActivities); |
| 158 |
for (int i = 0; i < kNumActivities; i++) { |
| 159 |
auto notification = std::make_shared<Notification>(); |
| 160 |
auto activity = MakeActivity( |
| 161 |
Sleep(Timestamp::Now() + Duration::Seconds(1)), |
| 162 |
ExecCtxWakeupScheduler(), |
| 163 |
[notification](absl::Status /*r*/) { notification->Notify(); }, |
| 164 |
engine.get()); |
| 165 |
notifications.push_back(std::move(notification)); |
| 166 |
activities.push_back(std::move(activity)); |
| 167 |
} |
| 168 |
gpr_log(GPR_INFO, |
| 169 |
"Waiting for the first %d sleeps, whilst cancelling the other half", |
| 170 |
kNumActivities / 2); |
| 171 |
for (size_t i = 0; i < kNumActivities / 2; i++) { |
| 172 |
notifications[i]->WaitForNotification(); |
| 173 |
activities[i].reset(); |
| 174 |
activities[i + kNumActivities / 2].reset(); |
| 175 |
exec_ctx.Flush(); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
} // namespace |
| 180 |
} // namespace grpc_core |
| 181 |
|
| 182 |
int main(int argc, char** argv) { |
| 183 |
::testing::InitGoogleTest(&argc, argv); |
| 184 |
grpc_init(); |
| 185 |
int r = RUN_ALL_TESTS(); |
| 186 |
grpc_shutdown(); |
| 187 |
return r; |
| 188 |
} |