| 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 <grpc/support/port_platform.h> |
| 16 |
|
| 17 |
#include "src/core/lib/event_engine/common_closures.h" |
| 18 |
|
| 19 |
#include "absl/functional/any_invocable.h" |
| 20 |
#include "gtest/gtest.h" |
| 21 |
|
| 22 |
#include "src/core/lib/gprpp/notification.h" |
| 23 |
|
| 24 |
using ::grpc_event_engine::experimental::AnyInvocableClosure; |
| 25 |
using ::grpc_event_engine::experimental::SelfDeletingClosure; |
| 26 |
|
| 27 |
class AnyInvocableClosureTest : public testing::Test {}; |
| 28 |
|
| 29 |
TEST_F(AnyInvocableClosureTest, CallsItsFunction) { |
| 30 |
grpc_core::Notification signal; |
| 31 |
AnyInvocableClosure closure([&signal] { signal.Notify(); }); |
| 32 |
closure.Run(); |
| 33 |
signal.WaitForNotification(); |
| 34 |
} |
| 35 |
|
| 36 |
class SelfDeletingClosureTest : public testing::Test {}; |
| 37 |
|
| 38 |
TEST_F(SelfDeletingClosureTest, CallsItsFunction) { |
| 39 |
grpc_core::Notification signal; |
| 40 |
auto* closure = SelfDeletingClosure::Create([&signal] { signal.Notify(); }); |
| 41 |
closure->Run(); |
| 42 |
signal.WaitForNotification(); |
| 43 |
// ASAN should catch if this closure is not deleted |
| 44 |
} |
| 45 |
|
| 46 |
TEST_F(SelfDeletingClosureTest, CallsItsFunctionAndIsDestroyed) { |
| 47 |
grpc_core::Notification fn_called; |
| 48 |
grpc_core::Notification destroyed; |
| 49 |
auto* closure = |
| 50 |
SelfDeletingClosure::Create([&fn_called] { fn_called.Notify(); }, |
| 51 |
[&destroyed] { destroyed.Notify(); }); |
| 52 |
closure->Run(); |
| 53 |
fn_called.WaitForNotification(); |
| 54 |
destroyed.WaitForNotification(); |
| 55 |
} |
| 56 |
|
| 57 |
int main(int argc, char** argv) { |
| 58 |
testing::InitGoogleTest(&argc, argv); |
| 59 |
auto result = RUN_ALL_TESTS(); |
| 60 |
return result; |
| 61 |
} |