From d9ac89a4411a156236da786006c352ec140af021 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Fri, 16 Sep 2022 09:06:03 -0700 Subject: [PATCH] [gprpp] absl::Notification polyfill (#31008) * [gprpp] absl::Notification polyfill * grpc_core:: * Automated change: Fix sanity tests * fix * Automated change: Fix sanity tests Co-authored-by: ctiller --- BUILD | 10 +++ CMakeLists.txt | 36 ++++++++ build_autogenerated.yaml | 13 +++ gRPC-C++.podspec | 2 + gRPC-Core.podspec | 2 + grpc.gemspec | 1 + package.xml | 1 + .../binder/wire_format/wire_reader_impl.h | 4 +- src/core/lib/gprpp/notification.h | 67 ++++++++++++++ src/core/lib/surface/server.cc | 2 +- src/core/lib/surface/server.h | 9 +- test/core/gprpp/BUILD | 12 +++ test/core/gprpp/notification_test.cc | 88 +++++++++++++++++++ test/core/gprpp/work_serializer_test.cc | 4 +- test/core/memory_usage/callback_client.cc | 4 +- test/core/promise/sleep_test.cc | 14 +-- test/core/surface/init_test.cc | 5 +- .../transport/binder/binder_transport_test.cc | 12 +-- .../chttp2/graceful_shutdown_test.cc | 11 ++- .../transport/chttp2/streams_not_seen_test.cc | 11 ++- test/cpp/end2end/client_lb_end2end_test.cc | 4 +- test/cpp/end2end/test_service_impl.cc | 5 +- tools/doxygen/Doxyfile.c++.internal | 1 + tools/doxygen/Doxyfile.core.internal | 1 + tools/run_tests/generated/tests.json | 24 +++++ 25 files changed, 298 insertions(+), 45 deletions(-) create mode 100644 src/core/lib/gprpp/notification.h create mode 100644 test/core/gprpp/notification_test.cc diff --git a/BUILD b/BUILD index b5c788b70e6..f6c9f0bb02d 100644 --- a/BUILD +++ b/BUILD @@ -3355,6 +3355,7 @@ grpc_cc_library( "json", "latch", "memory_quota", + "notification", "orphanable", "packed_table", "poll", @@ -3616,6 +3617,15 @@ grpc_cc_library( ], ) +grpc_cc_library( + name = "notification", + hdrs = [ + "src/core/lib/gprpp/notification.h", + ], + external_deps = ["absl/time"], + deps = ["gpr"], +) + grpc_cc_library( name = "channel_args", srcs = [ diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f9b81c4a95..25fe5a564a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1059,6 +1059,7 @@ if(gRPC_BUILD_TESTS) add_dependencies(buildtests_cxx murmur_hash_test) add_dependencies(buildtests_cxx no_destruct_test) add_dependencies(buildtests_cxx nonblocking_test) + add_dependencies(buildtests_cxx notification_test) add_dependencies(buildtests_cxx num_external_connectivity_watchers_test) add_dependencies(buildtests_cxx observable_test) if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX) @@ -13748,6 +13749,41 @@ target_link_libraries(nonblocking_test ) +endif() +if(gRPC_BUILD_TESTS) + +add_executable(notification_test + test/core/gprpp/notification_test.cc + third_party/googletest/googletest/src/gtest-all.cc + third_party/googletest/googlemock/src/gmock-all.cc +) + +target_include_directories(notification_test + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/include + ${_gRPC_ADDRESS_SORTING_INCLUDE_DIR} + ${_gRPC_RE2_INCLUDE_DIR} + ${_gRPC_SSL_INCLUDE_DIR} + ${_gRPC_UPB_GENERATED_DIR} + ${_gRPC_UPB_GRPC_GENERATED_DIR} + ${_gRPC_UPB_INCLUDE_DIR} + ${_gRPC_XXHASH_INCLUDE_DIR} + ${_gRPC_ZLIB_INCLUDE_DIR} + third_party/googletest/googletest/include + third_party/googletest/googletest + third_party/googletest/googlemock/include + third_party/googletest/googlemock + ${_gRPC_PROTO_GENS_DIR} +) + +target_link_libraries(notification_test + ${_gRPC_PROTOBUF_LIBRARIES} + ${_gRPC_ALLTARGETS_LIBRARIES} + gpr +) + + endif() if(gRPC_BUILD_TESTS) diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index 267f93992e0..774f8e5c0b1 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -762,6 +762,7 @@ libs: - src/core/lib/gprpp/dual_ref_counted.h - src/core/lib/gprpp/manual_constructor.h - src/core/lib/gprpp/match.h + - src/core/lib/gprpp/notification.h - src/core/lib/gprpp/orphanable.h - src/core/lib/gprpp/overload.h - src/core/lib/gprpp/packed_table.h @@ -1959,6 +1960,7 @@ libs: - src/core/lib/gprpp/dual_ref_counted.h - src/core/lib/gprpp/manual_constructor.h - src/core/lib/gprpp/match.h + - src/core/lib/gprpp/notification.h - src/core/lib/gprpp/orphanable.h - src/core/lib/gprpp/overload.h - src/core/lib/gprpp/packed_table.h @@ -7853,6 +7855,17 @@ targets: - test/cpp/end2end/nonblocking_test.cc deps: - grpc++_test_util +- name: notification_test + gtest: true + build: test + language: c++ + headers: + - src/core/lib/gprpp/notification.h + src: + - test/core/gprpp/notification_test.cc + deps: + - gpr + uses_polling: false - name: num_external_connectivity_watchers_test gtest: true build: test diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index 66630f72c1a..28fe29c96af 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec @@ -729,6 +729,7 @@ Pod::Spec.new do |s| 'src/core/lib/gprpp/memory.h', 'src/core/lib/gprpp/mpscq.h', 'src/core/lib/gprpp/no_destruct.h', + 'src/core/lib/gprpp/notification.h', 'src/core/lib/gprpp/orphanable.h', 'src/core/lib/gprpp/overload.h', 'src/core/lib/gprpp/packed_table.h', @@ -1591,6 +1592,7 @@ Pod::Spec.new do |s| 'src/core/lib/gprpp/memory.h', 'src/core/lib/gprpp/mpscq.h', 'src/core/lib/gprpp/no_destruct.h', + 'src/core/lib/gprpp/notification.h', 'src/core/lib/gprpp/orphanable.h', 'src/core/lib/gprpp/overload.h', 'src/core/lib/gprpp/packed_table.h', diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec index 9cda6c58268..cdcc70ce913 100644 --- a/gRPC-Core.podspec +++ b/gRPC-Core.podspec @@ -1152,6 +1152,7 @@ Pod::Spec.new do |s| 'src/core/lib/gprpp/mpscq.cc', 'src/core/lib/gprpp/mpscq.h', 'src/core/lib/gprpp/no_destruct.h', + 'src/core/lib/gprpp/notification.h', 'src/core/lib/gprpp/orphanable.h', 'src/core/lib/gprpp/overload.h', 'src/core/lib/gprpp/packed_table.h', @@ -2218,6 +2219,7 @@ Pod::Spec.new do |s| 'src/core/lib/gprpp/memory.h', 'src/core/lib/gprpp/mpscq.h', 'src/core/lib/gprpp/no_destruct.h', + 'src/core/lib/gprpp/notification.h', 'src/core/lib/gprpp/orphanable.h', 'src/core/lib/gprpp/overload.h', 'src/core/lib/gprpp/packed_table.h', diff --git a/grpc.gemspec b/grpc.gemspec index 590a6a8448f..d21c0f99ae0 100644 --- a/grpc.gemspec +++ b/grpc.gemspec @@ -1065,6 +1065,7 @@ Gem::Specification.new do |s| s.files += %w( src/core/lib/gprpp/mpscq.cc ) s.files += %w( src/core/lib/gprpp/mpscq.h ) s.files += %w( src/core/lib/gprpp/no_destruct.h ) + s.files += %w( src/core/lib/gprpp/notification.h ) s.files += %w( src/core/lib/gprpp/orphanable.h ) s.files += %w( src/core/lib/gprpp/overload.h ) s.files += %w( src/core/lib/gprpp/packed_table.h ) diff --git a/package.xml b/package.xml index 1a41961f1d1..ad9ee75ee91 100644 --- a/package.xml +++ b/package.xml @@ -1047,6 +1047,7 @@ + diff --git a/src/core/ext/transport/binder/wire_format/wire_reader_impl.h b/src/core/ext/transport/binder/wire_format/wire_reader_impl.h index 24043ac1881..e1a47b452eb 100644 --- a/src/core/ext/transport/binder/wire_format/wire_reader_impl.h +++ b/src/core/ext/transport/binder/wire_format/wire_reader_impl.h @@ -21,7 +21,6 @@ #include #include "absl/container/flat_hash_map.h" -#include "absl/synchronization/notification.h" #include @@ -29,6 +28,7 @@ #include "src/core/ext/transport/binder/wire_format/binder.h" #include "src/core/ext/transport/binder/wire_format/wire_reader.h" #include "src/core/ext/transport/binder/wire_format/wire_writer.h" +#include "src/core/lib/gprpp/notification.h" namespace grpc_binder { @@ -105,7 +105,7 @@ class WireReaderImpl : public WireReader { ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_); std::shared_ptr transport_stream_receiver_; - absl::Notification connection_noti_; + grpc_core::Notification connection_noti_; grpc_core::Mutex mu_; bool connected_ ABSL_GUARDED_BY(mu_) = false; bool recvd_setup_transport_ ABSL_GUARDED_BY(mu_) = false; diff --git a/src/core/lib/gprpp/notification.h b/src/core/lib/gprpp/notification.h new file mode 100644 index 00000000000..1d19874cf49 --- /dev/null +++ b/src/core/lib/gprpp/notification.h @@ -0,0 +1,67 @@ +// Copyright 2022 The gRPC Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef GRPC_CORE_LIB_GPRPP_NOTIFICATION_H +#define GRPC_CORE_LIB_GPRPP_NOTIFICATION_H + +#include + +#include "absl/time/clock.h" +#include "absl/time/time.h" + +#include "src/core/lib/gprpp/sync.h" + +namespace grpc_core { + +// Polyfill for absl::Notification until we can use that type. +class Notification { + public: + void Notify() { + MutexLock lock(&mu_); + notified_ = true; + cv_.SignalAll(); + } + + void WaitForNotification() { + MutexLock lock(&mu_); + while (!notified_) { + cv_.Wait(&mu_); + } + } + + bool WaitForNotificationWithTimeout(absl::Duration timeout) { + auto now = absl::Now(); + auto deadline = now + timeout; + MutexLock lock(&mu_); + while (!notified_ && now < deadline) { + cv_.WaitWithTimeout(&mu_, deadline - now); + now = absl::Now(); + } + return notified_; + } + + bool HasBeenNotified() { + MutexLock lock(&mu_); + return notified_; + } + + private: + Mutex mu_; + CondVar cv_; + bool notified_ = false; +}; + +} // namespace grpc_core + +#endif // GRPC_CORE_LIB_GPRPP_NOTIFICATION_H diff --git a/src/core/lib/surface/server.cc b/src/core/lib/surface/server.cc index cff4e5f5db7..591650b8ab3 100644 --- a/src/core/lib/surface/server.cc +++ b/src/core/lib/surface/server.cc @@ -790,7 +790,7 @@ void DonePublishedShutdown(void* /*done_arg*/, grpc_cq_completion* storage) { // connection is NOT closed until the server is done with all those calls. // -- Once there are no more calls in progress, the channel is closed. void Server::ShutdownAndNotify(grpc_completion_queue* cq, void* tag) { - absl::Notification* await_requests = nullptr; + Notification* await_requests = nullptr; ChannelBroadcaster broadcaster; { // Wait for startup to be finished. Locks mu_global. diff --git a/src/core/lib/surface/server.h b/src/core/lib/surface/server.h index 8664d9793e8..b68f026f36f 100644 --- a/src/core/lib/surface/server.h +++ b/src/core/lib/surface/server.h @@ -33,7 +33,6 @@ #include "absl/base/thread_annotations.h" #include "absl/memory/memory.h" #include "absl/status/statusor.h" -#include "absl/synchronization/notification.h" #include "absl/types/optional.h" #include @@ -49,6 +48,7 @@ #include "src/core/lib/debug/trace.h" #include "src/core/lib/gprpp/cpp_impl_of.h" #include "src/core/lib/gprpp/dual_ref_counted.h" +#include "src/core/lib/gprpp/notification.h" #include "src/core/lib/gprpp/orphanable.h" #include "src/core/lib/gprpp/ref_counted_ptr.h" #include "src/core/lib/gprpp/sync.h" @@ -419,14 +419,14 @@ class Server : public InternallyRefCounted, } // Returns a notification pointer to wait on if there are requests in-flight, // or null. - absl::Notification* ShutdownUnrefOnShutdownCall() + Notification* ShutdownUnrefOnShutdownCall() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_global_) GRPC_MUST_USE_RESULT { if (shutdown_refs_.fetch_sub(1, std::memory_order_acq_rel) == 1) { // There is no request in-flight. MaybeFinishShutdown(); return nullptr; } - requests_complete_ = absl::make_unique(); + requests_complete_ = absl::make_unique(); return requests_complete_.get(); } @@ -478,8 +478,7 @@ class Server : public InternallyRefCounted, std::atomic shutdown_refs_{1}; bool shutdown_published_ ABSL_GUARDED_BY(mu_global_) = false; std::vector shutdown_tags_ ABSL_GUARDED_BY(mu_global_); - std::unique_ptr requests_complete_ - ABSL_GUARDED_BY(mu_global_); + std::unique_ptr requests_complete_ ABSL_GUARDED_BY(mu_global_); std::list channels_; diff --git a/test/core/gprpp/BUILD b/test/core/gprpp/BUILD index 5897573760b..7d3a7bc99a9 100644 --- a/test/core/gprpp/BUILD +++ b/test/core/gprpp/BUILD @@ -401,3 +401,15 @@ grpc_cc_test( "//test/core/util:grpc_test_util", ], ) + +grpc_cc_test( + name = "notification_test", + srcs = ["notification_test.cc"], + external_deps = [ + "gtest", + ], + language = "C++", + uses_event_engine = False, + uses_polling = False, + deps = ["//:notification"], +) diff --git a/test/core/gprpp/notification_test.cc b/test/core/gprpp/notification_test.cc new file mode 100644 index 00000000000..bae987c52c9 --- /dev/null +++ b/test/core/gprpp/notification_test.cc @@ -0,0 +1,88 @@ +// Copyright 2022 The gRPC Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/core/lib/gprpp/notification.h" + +#include + +#include +#include + +#include + +namespace grpc_core { +namespace testing { +namespace { + +TEST(Notification, Works) { + Notification n; + EXPECT_FALSE(n.HasBeenNotified()); + n.Notify(); + EXPECT_TRUE(n.HasBeenNotified()); + n.WaitForNotification(); + EXPECT_TRUE(n.HasBeenNotified()); +} + +TEST(Notification, Waits) { + Notification n; + auto start = absl::Now(); + std::thread t([&n] { + absl::SleepFor(absl::Seconds(5)); + n.Notify(); + }); + n.WaitForNotification(); + auto end = absl::Now(); + EXPECT_GE(end - start, absl::Seconds(5)); + t.join(); +} + +TEST(Notification, WaitsWithTimeout) { + Notification n; + auto start = absl::Now(); + std::thread t([&n] { + absl::SleepFor(absl::Seconds(5)); + n.Notify(); + }); + EXPECT_TRUE(n.WaitForNotificationWithTimeout(absl::Seconds(10))); + auto end = absl::Now(); + EXPECT_GE(end - start, absl::Seconds(5)); + EXPECT_LE(end - start, absl::Seconds(10)); + t.join(); +} + +TEST(Notification, WaitWithTimeoutCanFinishEarly) { + Notification n; + auto start = absl::Now(); + std::thread t([&n] { + absl::SleepFor(absl::Seconds(5)); + n.Notify(); + }); + EXPECT_FALSE(n.WaitForNotificationWithTimeout(absl::Seconds(1))); + auto end = absl::Now(); + EXPECT_GE(end - start, absl::Seconds(1)); + EXPECT_LE(end - start, absl::Seconds(5)); + n.WaitForNotification(); + end = absl::Now(); + EXPECT_GE(end - start, absl::Seconds(5)); + t.join(); +} + +} // namespace +} // namespace testing +} // namespace grpc_core + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/core/gprpp/work_serializer_test.cc b/test/core/gprpp/work_serializer_test.cc index 1f9a506a1f4..519bd30d4da 100644 --- a/test/core/gprpp/work_serializer_test.cc +++ b/test/core/gprpp/work_serializer_test.cc @@ -25,13 +25,13 @@ #include "absl/memory/memory.h" #include "absl/synchronization/barrier.h" -#include "absl/synchronization/notification.h" #include #include #include #include "src/core/lib/gpr/useful.h" +#include "src/core/lib/gprpp/notification.h" #include "src/core/lib/gprpp/thd.h" #include "src/core/lib/iomgr/executor.h" #include "test/core/util/test_config.h" @@ -205,7 +205,7 @@ TEST(WorkSerializerTest, CallbackDestroysWorkSerializer) { TEST(WorkSerializerTest, WorkSerializerDestructionRace) { for (int i = 0; i < 1000; ++i) { auto lock = std::make_shared(); - absl::Notification notification; + grpc_core::Notification notification; std::thread t1([&]() { notification.WaitForNotification(); lock.reset(); diff --git a/test/core/memory_usage/callback_client.cc b/test/core/memory_usage/callback_client.cc index f43b8d4f151..314cd190a30 100644 --- a/test/core/memory_usage/callback_client.cc +++ b/test/core/memory_usage/callback_client.cc @@ -28,7 +28,6 @@ #include "absl/flags/flag.h" #include "absl/flags/parse.h" #include "absl/strings/string_view.h" -#include "absl/synchronization/notification.h" #include #include @@ -36,6 +35,7 @@ #include #include +#include "src/core/lib/gprpp/notification.h" #include "src/proto/grpc/testing/benchmark_service.grpc.pb.h" #include "src/proto/grpc/testing/messages.pb.h" #include "test/core/memory_usage/memstats.h" @@ -74,7 +74,7 @@ struct CallParams { grpc::testing::SimpleRequest request; grpc::testing::SimpleResponse response; grpc::testing::MemorySize snapshot_response; - absl::Notification done; + grpc_core::Notification done; }; // Simple Unary RPC to send to confirm connection is open diff --git a/test/core/promise/sleep_test.cc b/test/core/promise/sleep_test.cc index 2bfd7effdde..1babe8f93dc 100644 --- a/test/core/promise/sleep_test.cc +++ b/test/core/promise/sleep_test.cc @@ -20,11 +20,11 @@ #include #include -#include "absl/synchronization/notification.h" #include "gtest/gtest.h" #include +#include "src/core/lib/gprpp/notification.h" #include "src/core/lib/iomgr/exec_ctx.h" #include "src/core/lib/promise/exec_ctx_wakeup_scheduler.h" #include "src/core/lib/promise/race.h" @@ -35,7 +35,7 @@ namespace { TEST(Sleep, Zzzz) { ExecCtx exec_ctx; - absl::Notification done; + Notification done; Timestamp done_time = Timestamp::Now() + Duration::Seconds(1); // Sleep for one second then set done to true. auto activity = MakeActivity(Sleep(done_time), InlineWakeupScheduler(), @@ -50,7 +50,7 @@ TEST(Sleep, Zzzz) { TEST(Sleep, AlreadyDone) { ExecCtx exec_ctx; - absl::Notification done; + Notification done; Timestamp done_time = Timestamp::Now() - Duration::Seconds(1); // Sleep for no time at all then set done to true. auto activity = MakeActivity(Sleep(done_time), InlineWakeupScheduler(), @@ -63,7 +63,7 @@ TEST(Sleep, AlreadyDone) { TEST(Sleep, Cancel) { ExecCtx exec_ctx; - absl::Notification done; + Notification done; Timestamp done_time = Timestamp::Now() + Duration::Seconds(1); // Sleep for one second but race it to complete immediately auto activity = MakeActivity( @@ -80,7 +80,7 @@ TEST(Sleep, Cancel) { TEST(Sleep, MoveSemantics) { // ASAN should help determine if there are any memory leaks here ExecCtx exec_ctx; - absl::Notification done; + Notification done; Timestamp done_time = Timestamp::Now() + Duration::Milliseconds(111); Sleep donor(done_time); Sleep sleeper = std::move(donor); @@ -98,11 +98,11 @@ TEST(Sleep, StressTest) { // Kick off a bunch sleeps for one second. static const int kNumActivities = 100000; ExecCtx exec_ctx; - std::vector> notifications; + std::vector> notifications; std::vector activities; gpr_log(GPR_INFO, "Starting %d sleeps for 1sec", kNumActivities); for (int i = 0; i < kNumActivities; i++) { - auto notification = std::make_shared(); + auto notification = std::make_shared(); auto activity = MakeActivity( Sleep(Timestamp::Now() + Duration::Seconds(1)), ExecCtxWakeupScheduler(), diff --git a/test/core/surface/init_test.cc b/test/core/surface/init_test.cc index 84db32e8ed3..e63c5b19389 100644 --- a/test/core/surface/init_test.cc +++ b/test/core/surface/init_test.cc @@ -20,13 +20,12 @@ #include -#include "absl/synchronization/notification.h" - #include #include #include #include "src/core/lib/event_engine/default_event_engine.h" +#include "src/core/lib/gprpp/notification.h" #include "src/core/lib/iomgr/exec_ctx.h" #include "test/core/util/test_config.h" @@ -142,7 +141,7 @@ TEST(Init, repeatedly_blocking) { TEST(Init, TimerManagerHoldsLastInit) { grpc_init(); - absl::Notification n; + grpc_core::Notification n; grpc_event_engine::experimental::GetDefaultEventEngine()->RunAfter( std::chrono::seconds(1), [&n] { grpc_shutdown(); diff --git a/test/core/transport/binder/binder_transport_test.cc b/test/core/transport/binder/binder_transport_test.cc index c73ef8b7b07..df3cf7eeeb8 100644 --- a/test/core/transport/binder/binder_transport_test.cc +++ b/test/core/transport/binder/binder_transport_test.cc @@ -27,12 +27,12 @@ #include "absl/memory/memory.h" #include "absl/strings/match.h" #include "absl/strings/str_join.h" -#include "absl/synchronization/notification.h" #include #include #include "src/core/ext/transport/binder/transport/binder_stream.h" +#include "src/core/lib/gprpp/notification.h" #include "src/core/lib/resource_quota/resource_quota.h" #include "test/core/transport/binder/mock_objects.h" #include "test/core/util/test_config.h" @@ -111,7 +111,7 @@ void MockCallback(void* arg, grpc_error_handle error); class MockGrpcClosure { public: - explicit MockGrpcClosure(absl::Notification* notification = nullptr) + explicit MockGrpcClosure(grpc_core::Notification* notification = nullptr) : notification_(notification) { GRPC_CLOSURE_INIT(&closure_, MockCallback, this, nullptr); } @@ -119,7 +119,7 @@ class MockGrpcClosure { grpc_closure* GetGrpcClosure() { return &closure_; } MOCK_METHOD(void, Callback, (grpc_error_handle), ()); - absl::Notification* notification_; + grpc_core::Notification* notification_; private: grpc_closure closure_; @@ -288,7 +288,7 @@ struct MakeRecvInitialMetadata { grpc_core::ScopedArenaPtr arena = grpc_core::MakeScopedArena(1024, g_memory_allocator); grpc_metadata_batch grpc_initial_metadata{arena.get()}; - absl::Notification notification; + grpc_core::Notification notification; }; struct MakeRecvMessage { @@ -306,7 +306,7 @@ struct MakeRecvMessage { } MockGrpcClosure ready; - absl::Notification notification; + grpc_core::Notification notification; absl::optional grpc_message; }; @@ -332,7 +332,7 @@ struct MakeRecvTrailingMetadata { grpc_core::ScopedArenaPtr arena = grpc_core::MakeScopedArena(1024, g_memory_allocator); grpc_metadata_batch grpc_trailing_metadata{arena.get()}; - absl::Notification notification; + grpc_core::Notification notification; }; const Metadata kDefaultMetadata = { diff --git a/test/core/transport/chttp2/graceful_shutdown_test.cc b/test/core/transport/chttp2/graceful_shutdown_test.cc index 4dfae2aa7f8..2e7702a2b29 100644 --- a/test/core/transport/chttp2/graceful_shutdown_test.cc +++ b/test/core/transport/chttp2/graceful_shutdown_test.cc @@ -34,7 +34,6 @@ #include "absl/strings/match.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" -#include "absl/synchronization/notification.h" #include "absl/time/clock.h" #include "absl/time/time.h" #include "gtest/gtest.h" @@ -51,6 +50,7 @@ #include "src/core/lib/channel/channel_args.h" #include "src/core/lib/channel/channelz.h" #include "src/core/lib/gpr/useful.h" +#include "src/core/lib/gprpp/notification.h" #include "src/core/lib/gprpp/sync.h" #include "src/core/lib/iomgr/closure.h" #include "src/core/lib/iomgr/endpoint.h" @@ -101,7 +101,7 @@ class GracefulShutdownTest : public ::testing::Test { nullptr) == GRPC_ERROR_NONE); grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr); // Start polling on the client - absl::Notification client_poller_thread_started_notification; + Notification client_poller_thread_started_notification; client_poll_thread_ = absl::make_unique( [this, &client_poller_thread_started_notification]() { grpc_completion_queue* client_cq = @@ -225,7 +225,7 @@ class GracefulShutdownTest : public ::testing::Test { } void WriteBuffer(grpc_slice_buffer* buffer) { - absl::Notification on_write_done_notification_; + Notification on_write_done_notification_; GRPC_CLOSURE_INIT(&on_write_done_, OnWriteDone, &on_write_done_notification_, nullptr); grpc_endpoint_write(fds_.client, buffer, &on_write_done_, nullptr, @@ -237,8 +237,7 @@ class GracefulShutdownTest : public ::testing::Test { static void OnWriteDone(void* arg, grpc_error_handle error) { GPR_ASSERT(GRPC_ERROR_IS_NONE(error)); - absl::Notification* on_write_done_notification_ = - static_cast(arg); + Notification* on_write_done_notification_ = static_cast(arg); on_write_done_notification_->Notify(); } @@ -251,7 +250,7 @@ class GracefulShutdownTest : public ::testing::Test { grpc_closure on_read_done_; Mutex mu_; CondVar read_cv_; - absl::Notification read_end_notification_; + Notification read_end_notification_; grpc_slice_buffer read_buffer_; std::string read_bytes_ ABSL_GUARDED_BY(mu_); grpc_closure on_write_done_; diff --git a/test/core/transport/chttp2/streams_not_seen_test.cc b/test/core/transport/chttp2/streams_not_seen_test.cc index 86cd15c65d0..cf5fd572503 100644 --- a/test/core/transport/chttp2/streams_not_seen_test.cc +++ b/test/core/transport/chttp2/streams_not_seen_test.cc @@ -36,7 +36,6 @@ #include "absl/strings/match.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" -#include "absl/synchronization/notification.h" #include "absl/time/time.h" #include "absl/types/optional.h" #include "gtest/gtest.h" @@ -60,6 +59,7 @@ #include "src/core/lib/gpr/useful.h" #include "src/core/lib/gprpp/debug_location.h" #include "src/core/lib/gprpp/host_port.h" +#include "src/core/lib/gprpp/notification.h" #include "src/core/lib/gprpp/sync.h" #include "src/core/lib/gprpp/time.h" #include "src/core/lib/iomgr/closure.h" @@ -350,7 +350,7 @@ class StreamsNotSeenTest : public ::testing::Test { } void WriteBuffer(grpc_slice_buffer* buffer) { - absl::Notification on_write_done_notification_; + Notification on_write_done_notification_; GRPC_CLOSURE_INIT(&on_write_done_, OnWriteDone, &on_write_done_notification_, nullptr); grpc_endpoint_write(tcp_, buffer, &on_write_done_, nullptr, @@ -362,8 +362,7 @@ class StreamsNotSeenTest : public ::testing::Test { static void OnWriteDone(void* arg, grpc_error_handle error) { GPR_ASSERT(GRPC_ERROR_IS_NONE(error)); - absl::Notification* on_write_done_notification_ = - static_cast(arg); + Notification* on_write_done_notification_ = static_cast(arg); on_write_done_notification_->Notify(); } @@ -414,11 +413,11 @@ class StreamsNotSeenTest : public ::testing::Test { test_tcp_server server_; std::unique_ptr server_poll_thread_; grpc_endpoint* tcp_ = nullptr; - absl::Notification connect_notification_; + Notification connect_notification_; grpc_slice_buffer read_buffer_; grpc_closure on_write_done_; grpc_closure on_read_done_; - absl::Notification read_end_notification_; + Notification read_end_notification_; std::string read_bytes_ ABSL_GUARDED_BY(mu_); grpc_channel* channel_ = nullptr; grpc_completion_queue* cq_ = nullptr; diff --git a/test/cpp/end2end/client_lb_end2end_test.cc b/test/cpp/end2end/client_lb_end2end_test.cc index ee093b54749..45226eb7816 100644 --- a/test/cpp/end2end/client_lb_end2end_test.cc +++ b/test/cpp/end2end/client_lb_end2end_test.cc @@ -1266,7 +1266,7 @@ TEST_F(PickFirstTest, FailsEmptyResolverUpdate) { gpr_log(GPR_INFO, "****** SENDING INITIAL RESOLVER RESULT *******"); // Send a resolver result with an empty address list and a callback // that triggers a notification. - absl::Notification notification; + grpc_core::Notification notification; grpc_core::Resolver::Result result; result.addresses.emplace(); result.result_health_callback = [&](absl::Status status) { @@ -1677,7 +1677,7 @@ TEST_F(RoundRobinTest, FailsEmptyResolverUpdate) { gpr_log(GPR_INFO, "****** SENDING INITIAL RESOLVER RESULT *******"); // Send a resolver result with an empty address list and a callback // that triggers a notification. - absl::Notification notification; + grpc_core::Notification notification; grpc_core::Resolver::Result result; result.addresses.emplace(); result.resolution_note = "injected error"; diff --git a/test/cpp/end2end/test_service_impl.cc b/test/cpp/end2end/test_service_impl.cc index ea84225d5b4..2e014a289ff 100644 --- a/test/cpp/end2end/test_service_impl.cc +++ b/test/cpp/end2end/test_service_impl.cc @@ -23,13 +23,12 @@ #include -#include "absl/synchronization/notification.h" - #include #include #include #include +#include "src/core/lib/gprpp/notification.h" #include "src/proto/grpc/testing/echo.grpc.pb.h" #include "test/cpp/util/string_ref_helper.h" @@ -637,7 +636,7 @@ CallbackTestServiceImpl::BidiStream(CallbackServerContext* context) { bool setup_done_{false}; std::thread finish_thread_; bool client_try_cancel_ = false; - absl::Notification cancel_notification_; + grpc_core::Notification cancel_notification_; }; return new Reactor(context); diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index 8ead9c6bf3b..87e745536bb 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -2049,6 +2049,7 @@ src/core/lib/gprpp/memory.h \ src/core/lib/gprpp/mpscq.cc \ src/core/lib/gprpp/mpscq.h \ src/core/lib/gprpp/no_destruct.h \ +src/core/lib/gprpp/notification.h \ src/core/lib/gprpp/orphanable.h \ src/core/lib/gprpp/overload.h \ src/core/lib/gprpp/packed_table.h \ diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index 1ddfb76fc7c..b337c29f4d3 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -1840,6 +1840,7 @@ src/core/lib/gprpp/memory.h \ src/core/lib/gprpp/mpscq.cc \ src/core/lib/gprpp/mpscq.h \ src/core/lib/gprpp/no_destruct.h \ +src/core/lib/gprpp/notification.h \ src/core/lib/gprpp/orphanable.h \ src/core/lib/gprpp/overload.h \ src/core/lib/gprpp/packed_table.h \ diff --git a/tools/run_tests/generated/tests.json b/tools/run_tests/generated/tests.json index a6fe0a3f0b0..e9cf3c9174f 100644 --- a/tools/run_tests/generated/tests.json +++ b/tools/run_tests/generated/tests.json @@ -4787,6 +4787,30 @@ ], "uses_polling": true }, + { + "args": [], + "benchmark": false, + "ci_platforms": [ + "linux", + "mac", + "posix", + "windows" + ], + "cpu_cost": 1.0, + "exclude_configs": [], + "exclude_iomgrs": [], + "flaky": false, + "gtest": true, + "language": "c++", + "name": "notification_test", + "platforms": [ + "linux", + "mac", + "posix", + "windows" + ], + "uses_polling": false + }, { "args": [], "benchmark": false,