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/if.h"
16
17 #include "gtest/gtest.h"
18
19 namespace grpc_core {
20
TEST(IfTest,ChooseTrue)21 TEST(IfTest, ChooseTrue) {
22 int execution_order = 0;
23 EXPECT_EQ(If(
24 [&execution_order]() {
25 execution_order = (10 * execution_order) + 1;
26 return true;
27 },
28 [&execution_order]() {
29 execution_order = (10 * execution_order) + 2;
30 return 2;
31 },
32 [&execution_order]() {
33 execution_order = (10 * execution_order) + 3;
34 return 3;
35 })(),
36 Poll<int>(2));
37 EXPECT_EQ(execution_order, 12);
38 }
39
TEST(IfTest,ChooseFalse)40 TEST(IfTest, ChooseFalse) {
41 int execution_order = 0;
42 EXPECT_EQ(If(
43 [&execution_order]() {
44 execution_order = (10 * execution_order) + 1;
45 return false;
46 },
47 [&execution_order]() {
48 execution_order = (10 * execution_order) + 2;
49 return 2;
50 },
51 [&execution_order]() {
52 execution_order = (10 * execution_order) + 3;
53 return 3;
54 })(),
55 Poll<int>(3));
56 EXPECT_EQ(execution_order, 13);
57 }
58
TEST(IfTest,ChooseSuccessfulTrue)59 TEST(IfTest, ChooseSuccessfulTrue) {
60 int execution_order = 0;
61 EXPECT_EQ(If(
62 [&execution_order]() {
63 execution_order = (10 * execution_order) + 1;
64 return absl::StatusOr<bool>(true);
65 },
66 [&execution_order]() {
67 execution_order = (10 * execution_order) + 2;
68 return absl::StatusOr<int>(2);
69 },
70 [&execution_order]() {
71 execution_order = (10 * execution_order) + 3;
72 return absl::StatusOr<int>(3);
73 })(),
74 Poll<absl::StatusOr<int>>(absl::StatusOr<int>(2)));
75 EXPECT_EQ(execution_order, 12);
76 }
77
TEST(IfTest,ChooseSuccessfulFalse)78 TEST(IfTest, ChooseSuccessfulFalse) {
79 int execution_order = 0;
80 EXPECT_EQ(If(
81 [&execution_order]() {
82 execution_order = (10 * execution_order) + 1;
83 return absl::StatusOr<bool>(false);
84 },
85 [&execution_order]() {
86 execution_order = (10 * execution_order) + 2;
87 return absl::StatusOr<int>(2);
88 },
89 [&execution_order]() {
90 execution_order = (10 * execution_order) + 3;
91 return absl::StatusOr<int>(3);
92 })(),
93 Poll<absl::StatusOr<int>>(absl::StatusOr<int>(3)));
94 EXPECT_EQ(execution_order, 13);
95 }
96
TEST(IfTest,ChooseFailure)97 TEST(IfTest, ChooseFailure) {
98 int execution_order = 0;
99 EXPECT_EQ(If(
100 [&execution_order]() {
101 execution_order = (10 * execution_order) + 1;
102 return absl::StatusOr<bool>();
103 },
104 [&execution_order]() {
105 execution_order = (10 * execution_order) + 2;
106 return absl::StatusOr<int>(2);
107 },
108 [&execution_order]() {
109 execution_order = (10 * execution_order) + 3;
110 return absl::StatusOr<int>(3);
111 })(),
112 Poll<absl::StatusOr<int>>(absl::StatusOr<int>()));
113 EXPECT_EQ(execution_order, 1);
114 }
115
TEST(IfTest,ChoosePending)116 TEST(IfTest, ChoosePending) {
117 int execution_order = 0;
118 int once = false;
119 auto if_combiner = If(
120 [&execution_order, &once]() -> Poll<bool> {
121 execution_order = (10 * execution_order) + 1;
122 if (once) return true;
123 once = true;
124 return Pending{};
125 },
126 [&execution_order]() {
127 execution_order = (10 * execution_order) + 2;
128 return 2;
129 },
130 [&execution_order]() {
131 execution_order = (10 * execution_order) + 3;
132 return 3;
133 });
134
135 Poll<int> first_execution = if_combiner();
136 EXPECT_FALSE(first_execution.ready());
137 EXPECT_EQ(execution_order, 1);
138
139 execution_order = 0;
140 Poll<int> second_execution = if_combiner();
141 EXPECT_TRUE(second_execution.ready());
142 EXPECT_EQ(second_execution.value(), 2);
143 EXPECT_EQ(execution_order, 12);
144 }
145
TEST(IfTest,ImmediateChooseTrue)146 TEST(IfTest, ImmediateChooseTrue) {
147 int execution_order = 0;
148 EXPECT_EQ(If(
149 true,
150 [&execution_order]() {
151 execution_order = (10 * execution_order) + 2;
152 return 2;
153 },
154 [&execution_order]() {
155 execution_order = (10 * execution_order) + 3;
156 return 3;
157 })(),
158 Poll<int>(2));
159 EXPECT_EQ(execution_order, 2);
160 }
161
TEST(IfTest,ImmediateChooseFalse)162 TEST(IfTest, ImmediateChooseFalse) {
163 int execution_order = 0;
164 EXPECT_EQ(If(
165 false,
166 [&execution_order]() {
167 execution_order = (10 * execution_order) + 2;
168 return 2;
169 },
170 [&execution_order]() {
171 execution_order = (10 * execution_order) + 3;
172 return 3;
173 })(),
174 Poll<int>(3));
175 EXPECT_EQ(execution_order, 3);
176 }
177
178 } // namespace grpc_core
179
main(int argc,char ** argv)180 int main(int argc, char** argv) {
181 ::testing::InitGoogleTest(&argc, argv);
182 return RUN_ALL_TESTS();
183 }
184