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