xref: /aosp_15_r20/external/grpc-grpc/test/core/promise/latch_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2021 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/latch.h"
16 
17 #include <tuple>
18 #include <utility>
19 
20 #include "absl/status/status.h"
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 
24 #include "src/core/lib/promise/activity.h"
25 #include "src/core/lib/promise/join.h"
26 #include "src/core/lib/promise/seq.h"
27 #include "test/core/promise/test_wakeup_schedulers.h"
28 
29 using testing::MockFunction;
30 using testing::StrictMock;
31 
32 namespace grpc_core {
33 
TEST(LatchTest,Works)34 TEST(LatchTest, Works) {
35   Latch<int> latch;
36   StrictMock<MockFunction<void(absl::Status)>> on_done;
37   EXPECT_CALL(on_done, Call(absl::OkStatus()));
38   MakeActivity(
39       [&latch] {
40         return Seq(Join(latch.Wait(),
41                         [&latch]() {
42                           latch.Set(42);
43                           return true;
44                         }),
45                    [](std::tuple<int, bool> result) {
46                      EXPECT_EQ(std::get<0>(result), 42);
47                      return absl::OkStatus();
48                    });
49       },
50       NoWakeupScheduler(),
51       [&on_done](absl::Status status) { on_done.Call(std::move(status)); });
52 }
53 
TEST(LatchTest,WaitAndCopyWorks)54 TEST(LatchTest, WaitAndCopyWorks) {
55   Latch<std::string> latch;
56   StrictMock<MockFunction<void(absl::Status)>> on_done;
57   EXPECT_CALL(on_done, Call(absl::OkStatus()));
58   MakeActivity(
59       [&latch] {
60         return Seq(Join(latch.WaitAndCopy(), latch.WaitAndCopy(),
61                         [&latch]() {
62                           latch.Set(
63                               "Once a jolly swagman camped by a billabong, "
64                               "under the shade of a coolibah tree.");
65                           return true;
66                         }),
67                    [](std::tuple<std::string, std::string, bool> result) {
68                      EXPECT_EQ(std::get<0>(result),
69                                "Once a jolly swagman camped by a billabong, "
70                                "under the shade of a coolibah tree.");
71                      EXPECT_EQ(std::get<1>(result),
72                                "Once a jolly swagman camped by a billabong, "
73                                "under the shade of a coolibah tree.");
74                      return absl::OkStatus();
75                    });
76       },
77       NoWakeupScheduler(),
78       [&on_done](absl::Status status) { on_done.Call(std::move(status)); });
79 }
80 
TEST(LatchTest,Void)81 TEST(LatchTest, Void) {
82   Latch<void> latch;
83   StrictMock<MockFunction<void(absl::Status)>> on_done;
84   EXPECT_CALL(on_done, Call(absl::OkStatus()));
85   MakeActivity(
86       [&latch] {
87         return Seq(Join(latch.Wait(),
88                         [&latch]() {
89                           latch.Set();
90                           return true;
91                         }),
92                    [](std::tuple<Empty, bool>) { return absl::OkStatus(); });
93       },
94       NoWakeupScheduler(),
95       [&on_done](absl::Status status) { on_done.Call(std::move(status)); });
96 }
97 
TEST(LatchTest,ExternallyObservableVoid)98 TEST(LatchTest, ExternallyObservableVoid) {
99   ExternallyObservableLatch<void> latch;
100   StrictMock<MockFunction<void(absl::Status)>> on_done;
101   EXPECT_CALL(on_done, Call(absl::OkStatus()));
102   MakeActivity(
103       [&latch] {
104         return Seq(Join(latch.Wait(),
105                         [&latch]() {
106                           latch.Set();
107                           return true;
108                         }),
109                    [](std::tuple<Empty, bool>) { return absl::OkStatus(); });
110       },
111       NoWakeupScheduler(),
112       [&on_done](absl::Status status) { on_done.Call(std::move(status)); });
113 }
114 
115 }  // namespace grpc_core
116 
main(int argc,char ** argv)117 int main(int argc, char** argv) {
118   ::testing::InitGoogleTest(&argc, argv);
119   return RUN_ALL_TESTS();
120 }
121