| 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 |
#include <grpc/support/port_platform.h> |
| 15 |
|
| 16 |
#include <algorithm> |
| 17 |
#include <memory> |
| 18 |
#include <thread> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
#include "absl/functional/any_invocable.h" |
| 22 |
#include "absl/status/status.h" |
| 23 |
#include "absl/status/statusor.h" |
| 24 |
#include "absl/time/clock.h" |
| 25 |
#include "absl/time/time.h" |
| 26 |
#include "gtest/gtest.h" |
| 27 |
|
| 28 |
#include <grpc/event_engine/endpoint_config.h> |
| 29 |
#include <grpc/event_engine/event_engine.h> |
| 30 |
#include <grpc/event_engine/memory_allocator.h> |
| 31 |
#include <grpc/grpc.h> |
| 32 |
|
| 33 |
#include "src/core/lib/event_engine/default_event_engine.h" |
| 34 |
#include "test/core/util/test_config.h" |
| 35 |
|
| 36 |
namespace { |
| 37 |
|
| 38 |
using ::grpc_event_engine::experimental::EventEngine; |
| 39 |
using ::grpc_event_engine::experimental::GetDefaultEventEngine; |
| 40 |
|
| 41 |
class DefaultEngineTest : public testing::Test { |
| 42 |
protected: |
| 43 |
// Does nothing, fills space that a nullptr could not |
| 44 |
class FakeEventEngine : public EventEngine { |
| 45 |
public: |
| 46 |
FakeEventEngine() = default; |
| 47 |
~FakeEventEngine() override = default; |
| 48 |
absl::StatusOr<std::unique_ptr<Listener>> CreateListener( |
| 49 |
Listener::AcceptCallback /* on_accept */, |
| 50 |
absl::AnyInvocable<void(absl::Status)> /* on_shutdown */, |
| 51 |
const grpc_event_engine::experimental::EndpointConfig& /* config */, |
| 52 |
std::unique_ptr< |
| 53 |
grpc_event_engine::experimental:: |
| 54 |
MemoryAllocatorFactory> /* memory_allocator_factory */) |
| 55 |
override { |
| 56 |
return absl::UnimplementedError("test"); |
| 57 |
}; |
| 58 |
ConnectionHandle Connect( |
| 59 |
OnConnectCallback /* on_connect */, const ResolvedAddress& /* addr */, |
| 60 |
const grpc_event_engine::experimental::EndpointConfig& /* args */, |
| 61 |
grpc_event_engine::experimental::MemoryAllocator /* memory_allocator */, |
| 62 |
Duration /* timeout */) override { |
| 63 |
return {-1, -1}; |
| 64 |
}; |
| 65 |
bool CancelConnect(ConnectionHandle /* handle */) override { |
| 66 |
return false; |
| 67 |
}; |
| 68 |
bool IsWorkerThread() override { return false; }; |
| 69 |
std::unique_ptr<DNSResolver> GetDNSResolver( |
| 70 |
const DNSResolver::ResolverOptions& /* options */) override { |
| 71 |
return nullptr; |
| 72 |
}; |
| 73 |
void Run(Closure* /* closure */) override{}; |
| 74 |
void Run(absl::AnyInvocable<void()> /* closure */) override{}; |
| 75 |
TaskHandle RunAfter(Duration /* when */, Closure* /* closure */) override { |
| 76 |
return {-1, -1}; |
| 77 |
} |
| 78 |
TaskHandle RunAfter(Duration /* when */, |
| 79 |
absl::AnyInvocable<void()> /* closure */) override { |
| 80 |
return {-1, -1}; |
| 81 |
} |
| 82 |
bool Cancel(TaskHandle /* handle */) override { return false; }; |
| 83 |
}; |
| 84 |
}; |
| 85 |
|
| 86 |
TEST_F(DefaultEngineTest, SharedPtrGlobalEventEngineLifetimesAreValid) { |
| 87 |
int create_count = 0; |
| 88 |
grpc_event_engine::experimental::SetEventEngineFactory([&create_count] { |
| 89 |
++create_count; |
| 90 |
return std::make_unique<FakeEventEngine>(); |
| 91 |
}); |
| 92 |
std::shared_ptr<EventEngine> ee2; |
| 93 |
{ |
| 94 |
std::shared_ptr<EventEngine> ee1 = GetDefaultEventEngine(); |
| 95 |
ASSERT_EQ(1, create_count); |
| 96 |
ee2 = GetDefaultEventEngine(); |
| 97 |
ASSERT_EQ(1, create_count); |
| 98 |
ASSERT_EQ(ee2.use_count(), 2); |
| 99 |
} |
| 100 |
// Ensure the first shared_ptr did not delete the global |
| 101 |
ASSERT_TRUE(ee2.unique()); |
| 102 |
ASSERT_FALSE(ee2->IsWorkerThread()); // useful for ASAN |
| 103 |
// destroy the global engine via the last shared_ptr, and create a new one. |
| 104 |
ee2.reset(); |
| 105 |
ee2 = GetDefaultEventEngine(); |
| 106 |
ASSERT_EQ(2, create_count); |
| 107 |
ASSERT_TRUE(ee2.unique()); |
| 108 |
grpc_event_engine::experimental::EventEngineFactoryReset(); |
| 109 |
} |
| 110 |
|
| 111 |
TEST_F(DefaultEngineTest, StressTestSharedPtr) { |
| 112 |
constexpr int thread_count = 13; |
| 113 |
constexpr absl::Duration spin_time = absl::Seconds(3); |
| 114 |
std::vector<std::thread> threads; |
| 115 |
threads.reserve(thread_count); |
| 116 |
for (int i = 0; i < thread_count; i++) { |
| 117 |
threads.emplace_back([&spin_time] { |
| 118 |
auto timeout = absl::Now() + spin_time; |
| 119 |
do { |
| 120 |
GetDefaultEventEngine().reset(); |
| 121 |
} while (timeout > absl::Now()); |
| 122 |
}); |
| 123 |
} |
| 124 |
for (auto& thd : threads) { |
| 125 |
thd.join(); |
| 126 |
} |
| 127 |
} |
| 128 |
} // namespace |
| 129 |
|
| 130 |
int main(int argc, char** argv) { |
| 131 |
testing::InitGoogleTest(&argc, argv); |
| 132 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 133 |
grpc_init(); |
| 134 |
auto result = RUN_ALL_TESTS(); |
| 135 |
grpc_shutdown(); |
| 136 |
return result; |
| 137 |
} |