| 1 |
// Copyright 2022 The 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 <memory> |
| 17 |
#include <thread> |
| 18 |
#include <utility> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
#include "absl/functional/any_invocable.h" |
| 22 |
#include "absl/status/status.h" |
| 23 |
#include "absl/time/time.h" |
| 24 |
#include "gtest/gtest.h" |
| 25 |
|
| 26 |
#include <grpc/event_engine/event_engine.h> |
| 27 |
#include <grpc/grpc.h> |
| 28 |
|
| 29 |
#include "src/core/lib/event_engine/default_event_engine.h" |
| 30 |
#include "src/core/lib/event_engine/posix_engine/event_poller.h" |
| 31 |
#include "src/core/lib/event_engine/posix_engine/lockfree_event.h" |
| 32 |
#include "src/core/lib/event_engine/posix_engine/posix_engine_closure.h" |
| 33 |
#include "src/core/lib/gprpp/sync.h" |
| 34 |
|
| 35 |
using ::grpc_event_engine::experimental::EventEngine; |
| 36 |
using ::grpc_event_engine::posix_engine::Scheduler; |
| 37 |
|
| 38 |
namespace { |
| 39 |
class TestScheduler : public Scheduler { |
| 40 |
public: |
| 41 |
explicit TestScheduler(std::shared_ptr<EventEngine> engine) |
| 42 |
: engine_(std::move(engine)) {} |
| 43 |
void Run( |
| 44 |
grpc_event_engine::experimental::EventEngine::Closure* closure) override { |
| 45 |
engine_->Run(closure); |
| 46 |
} |
| 47 |
|
| 48 |
void Run(absl::AnyInvocable<void()> cb) override { |
| 49 |
engine_->Run(std::move(cb)); |
| 50 |
} |
| 51 |
|
| 52 |
private: |
| 53 |
std::shared_ptr<EventEngine> engine_; |
| 54 |
}; |
| 55 |
|
| 56 |
TestScheduler* g_scheduler; |
| 57 |
|
| 58 |
} // namespace |
| 59 |
|
| 60 |
namespace grpc_event_engine { |
| 61 |
namespace posix_engine { |
| 62 |
|
| 63 |
TEST(LockFreeEventTest, BasicTest) { |
| 64 |
LockfreeEvent event(g_scheduler); |
| 65 |
grpc_core::Mutex mu; |
| 66 |
grpc_core::CondVar cv; |
| 67 |
event.InitEvent(); |
| 68 |
grpc_core::MutexLock lock(&mu); |
| 69 |
// Set NotifyOn first and then SetReady |
| 70 |
event.NotifyOn( |
| 71 |
PosixEngineClosure::TestOnlyToClosure([&mu, &cv](absl::Status status) { |
| 72 |
grpc_core::MutexLock lock(&mu); |
| 73 |
EXPECT_TRUE(status.ok()); |
| 74 |
cv.Signal(); |
| 75 |
})); |
| 76 |
event.SetReady(); |
| 77 |
EXPECT_FALSE(cv.WaitWithTimeout(&mu, absl::Seconds(10))); |
| 78 |
|
| 79 |
// SetReady first first and then call NotifyOn |
| 80 |
event.SetReady(); |
| 81 |
event.NotifyOn( |
| 82 |
PosixEngineClosure::TestOnlyToClosure([&mu, &cv](absl::Status status) { |
| 83 |
grpc_core::MutexLock lock(&mu); |
| 84 |
EXPECT_TRUE(status.ok()); |
| 85 |
cv.Signal(); |
| 86 |
})); |
| 87 |
EXPECT_FALSE(cv.WaitWithTimeout(&mu, absl::Seconds(10))); |
| 88 |
|
| 89 |
// Set NotifyOn and then call SetShutdown |
| 90 |
event.NotifyOn( |
| 91 |
PosixEngineClosure::TestOnlyToClosure([&mu, &cv](absl::Status status) { |
| 92 |
grpc_core::MutexLock lock(&mu); |
| 93 |
EXPECT_FALSE(status.ok()); |
| 94 |
EXPECT_EQ(status, absl::CancelledError("Shutdown")); |
| 95 |
cv.Signal(); |
| 96 |
})); |
| 97 |
event.SetShutdown(absl::CancelledError("Shutdown")); |
| 98 |
EXPECT_FALSE(cv.WaitWithTimeout(&mu, absl::Seconds(10))); |
| 99 |
event.DestroyEvent(); |
| 100 |
} |
| 101 |
|
| 102 |
TEST(LockFreeEventTest, MultiThreadedTest) { |
| 103 |
std::vector<std::thread> threads; |
| 104 |
LockfreeEvent event(g_scheduler); |
| 105 |
grpc_core::Mutex mu; |
| 106 |
grpc_core::CondVar cv; |
| 107 |
bool signalled = false; |
| 108 |
int active = 0; |
| 109 |
static constexpr int kNumOperations = 100; |
| 110 |
threads.reserve(2); |
| 111 |
event.InitEvent(); |
| 112 |
// Spin up two threads alternating between NotifyOn and SetReady |
| 113 |
for (int i = 0; i < 2; i++) { |
| 114 |
threads.emplace_back([&, thread_id = i]() { |
| 115 |
for (int j = 0; j < kNumOperations; j++) { |
| 116 |
grpc_core::MutexLock lock(&mu); |
| 117 |
// Wait for both threads to process the previous operation before |
| 118 |
// starting the next one. |
| 119 |
while (signalled) { |
| 120 |
cv.Wait(&mu); |
| 121 |
} |
| 122 |
active++; |
| 123 |
if (thread_id == 0) { |
| 124 |
event.NotifyOn(PosixEngineClosure::TestOnlyToClosure( |
| 125 |
[&mu, &cv, &signalled](absl::Status status) { |
| 126 |
grpc_core::MutexLock lock(&mu); |
| 127 |
EXPECT_TRUE(status.ok()); |
| 128 |
signalled = true; |
| 129 |
cv.SignalAll(); |
| 130 |
})); |
| 131 |
} else { |
| 132 |
event.SetReady(); |
| 133 |
} |
| 134 |
while (!signalled) { |
| 135 |
cv.Wait(&mu); |
| 136 |
} |
| 137 |
// The last thread to finish the current operation sets signalled to |
| 138 |
// false and wakes up the other thread if its blocked waiting to |
| 139 |
// start the next operation. |
| 140 |
if (--active == 0) { |
| 141 |
signalled = false; |
| 142 |
cv.Signal(); |
| 143 |
} |
| 144 |
} |
| 145 |
}); |
| 146 |
} |
| 147 |
for (auto& t : threads) { |
| 148 |
t.join(); |
| 149 |
} |
| 150 |
event.SetShutdown(absl::OkStatus()); |
| 151 |
event.DestroyEvent(); |
| 152 |
} |
| 153 |
|
| 154 |
} // namespace posix_engine |
| 155 |
} // namespace grpc_event_engine |
| 156 |
|
| 157 |
int main(int argc, char** argv) { |
| 158 |
::testing::InitGoogleTest(&argc, argv); |
| 159 |
// TODO(ctiller): EventEngine temporarily needs grpc to be initialized first |
| 160 |
// until we clear out the iomgr shutdown code. |
| 161 |
grpc_init(); |
| 162 |
g_scheduler = new TestScheduler( |
| 163 |
grpc_event_engine::experimental::GetDefaultEventEngine()); |
| 164 |
int r = RUN_ALL_TESTS(); |
| 165 |
grpc_shutdown(); |
| 166 |
return r; |
| 167 |
} |