• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 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/promise/inter_activity_latch.h"
16 
17 #include <grpc/grpc.h>
18 
19 #include "absl/status/status.h"
20 #include "gtest/gtest.h"
21 #include "src/core/lib/event_engine/default_event_engine.h"
22 #include "src/core/lib/promise/event_engine_wakeup_scheduler.h"
23 #include "src/core/lib/promise/seq.h"
24 #include "src/core/util/notification.h"
25 
26 using grpc_event_engine::experimental::GetDefaultEventEngine;
27 
28 namespace grpc_core {
29 namespace {
30 
TEST(InterActivityLatchTest,Works)31 TEST(InterActivityLatchTest, Works) {
32   InterActivityLatch<void> latch;
33 
34   // Start some waiting activities
35   Notification n1;
36   auto a1 = MakeActivity(
37       [&] {
38         return Seq(latch.Wait(), [&](Empty) {
39           n1.Notify();
40           return absl::OkStatus();
41         });
42       },
43       EventEngineWakeupScheduler{GetDefaultEventEngine()}, [](absl::Status) {});
44   Notification n2;
45   auto a2 = MakeActivity(
46       [&] {
47         return Seq(latch.Wait(), [&](Empty) {
48           n2.Notify();
49           return absl::OkStatus();
50         });
51       },
52       EventEngineWakeupScheduler{GetDefaultEventEngine()}, [](absl::Status) {});
53   Notification n3;
54   auto a3 = MakeActivity(
55       [&] {
56         return Seq(latch.Wait(), [&](Empty) {
57           n3.Notify();
58           return absl::OkStatus();
59         });
60       },
61       EventEngineWakeupScheduler{GetDefaultEventEngine()}, [](absl::Status) {});
62 
63   ASSERT_FALSE(n1.HasBeenNotified());
64   ASSERT_FALSE(n2.HasBeenNotified());
65   ASSERT_FALSE(n3.HasBeenNotified());
66 
67   // Start a setting activity
68   auto kicker = MakeActivity(
69       [&] {
70         latch.Set();
71         return absl::OkStatus();
72       },
73       EventEngineWakeupScheduler{GetDefaultEventEngine()}, [](absl::Status) {});
74 
75   // Start another waiting activity
76   Notification n4;
77   auto a4 = MakeActivity(
78       [&] {
79         return Seq(latch.Wait(), [&](Empty) {
80           n4.Notify();
81           return absl::OkStatus();
82         });
83       },
84       EventEngineWakeupScheduler{GetDefaultEventEngine()}, [](absl::Status) {});
85 
86   // Everything should finish
87   n1.WaitForNotification();
88   n2.WaitForNotification();
89   n3.WaitForNotification();
90   n4.WaitForNotification();
91 }
92 
93 }  // namespace
94 }  // namespace grpc_core
95 
main(int argc,char ** argv)96 int main(int argc, char** argv) {
97   ::testing::InitGoogleTest(&argc, argv);
98   grpc_init();  // for GetDefaultEventEngine
99   int r = RUN_ALL_TESTS();
100   grpc_shutdown();
101   return r;
102 }
103