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