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