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/try_join.h"
16
17 #include <functional>
18 #include <memory>
19 #include <utility>
20
21 #include "absl/utility/utility.h"
22 #include "gtest/gtest.h"
23
24 namespace grpc_core {
25
26 struct AbslStatusTraits {
27 template <typename... Promises>
TryJoinImplgrpc_core::AbslStatusTraits28 static auto TryJoinImpl(Promises... promises) {
29 return TryJoin<absl::StatusOr>(std::move(promises)...);
30 }
31
32 template <typename T>
33 using Promise = std::function<Poll<absl::StatusOr<T>>()>;
34
35 template <typename T>
instant_okgrpc_core::AbslStatusTraits36 static Promise<T> instant_ok(T x) {
37 return [x] { return absl::StatusOr<T>(x); };
38 }
39
instant_ok_statusgrpc_core::AbslStatusTraits40 static auto instant_ok_status() {
41 return [] { return absl::OkStatus(); };
42 }
43
44 template <typename T>
instant_failgrpc_core::AbslStatusTraits45 static Promise<T> instant_fail() {
46 return [] { return absl::StatusOr<T>(); };
47 }
48
49 template <typename... T>
okgrpc_core::AbslStatusTraits50 static Poll<absl::StatusOr<std::tuple<T...>>> ok(T... x) {
51 return absl::StatusOr<std::tuple<T...>>(absl::in_place, x...);
52 }
53
54 template <typename... T>
failgrpc_core::AbslStatusTraits55 static Poll<absl::StatusOr<std::tuple<T...>>> fail() {
56 return absl::StatusOr<std::tuple<T...>>();
57 }
58
59 template <typename T>
pendinggrpc_core::AbslStatusTraits60 static Promise<T> pending() {
61 return []() -> Poll<absl::StatusOr<T>> { return Pending(); };
62 }
63 };
64
65 struct ValueOrFailureTraits {
66 template <typename... Promises>
TryJoinImplgrpc_core::ValueOrFailureTraits67 static auto TryJoinImpl(Promises... promises) {
68 return TryJoin<ValueOrFailure>(std::move(promises)...);
69 }
70
71 template <typename T>
72 using Promise = std::function<Poll<ValueOrFailure<T>>()>;
73
74 template <typename T>
instant_okgrpc_core::ValueOrFailureTraits75 static Promise<T> instant_ok(T x) {
76 return [x] { return ValueOrFailure<T>(x); };
77 }
78
instant_ok_statusgrpc_core::ValueOrFailureTraits79 static auto instant_ok_status() {
80 return [] { return StatusFlag(true); };
81 }
82
83 template <typename T>
instant_failgrpc_core::ValueOrFailureTraits84 static Promise<T> instant_fail() {
85 return [] { return Failure{}; };
86 }
87
88 template <typename... T>
okgrpc_core::ValueOrFailureTraits89 static Poll<ValueOrFailure<std::tuple<T...>>> ok(T... x) {
90 return ValueOrFailure<std::tuple<T...>>(std::tuple<T...>(x...));
91 }
92
93 template <typename... T>
failgrpc_core::ValueOrFailureTraits94 static Poll<ValueOrFailure<std::tuple<T...>>> fail() {
95 return Failure{};
96 }
97
98 template <typename T>
pendinggrpc_core::ValueOrFailureTraits99 static Promise<T> pending() {
100 return []() -> Poll<ValueOrFailure<T>> { return Pending(); };
101 }
102 };
103
104 template <typename T>
105 class TryJoinTest : public ::testing::Test {};
106
107 using Traits = ::testing::Types<AbslStatusTraits, ValueOrFailureTraits>;
108 TYPED_TEST_SUITE(TryJoinTest, Traits);
109
TYPED_TEST(TryJoinTest,Join1)110 TYPED_TEST(TryJoinTest, Join1) {
111 EXPECT_EQ(TypeParam::TryJoinImpl(TypeParam::instant_ok(1))(),
112 TypeParam::ok(1));
113 }
114
TYPED_TEST(TryJoinTest,Join1Fail)115 TYPED_TEST(TryJoinTest, Join1Fail) {
116 EXPECT_EQ(TypeParam::TryJoinImpl(TypeParam::template instant_fail<int>())(),
117 TypeParam::template fail<int>());
118 }
119
TYPED_TEST(TryJoinTest,Join2Success)120 TYPED_TEST(TryJoinTest, Join2Success) {
121 EXPECT_EQ(TypeParam::TryJoinImpl(TypeParam::instant_ok(1),
122 TypeParam::instant_ok(2))(),
123 TypeParam::ok(1, 2));
124 }
125
TYPED_TEST(TryJoinTest,Join2Fail1)126 TYPED_TEST(TryJoinTest, Join2Fail1) {
127 EXPECT_EQ(TypeParam::TryJoinImpl(TypeParam::instant_ok(1),
128 TypeParam::template instant_fail<int>())(),
129 (TypeParam::template fail<int, int>()));
130 }
131
TYPED_TEST(TryJoinTest,Join2Fail2)132 TYPED_TEST(TryJoinTest, Join2Fail2) {
133 EXPECT_EQ(TypeParam::TryJoinImpl(TypeParam::template instant_fail<int>(),
134 TypeParam::instant_ok(2))(),
135 (TypeParam::template fail<int, int>()));
136 }
137
TYPED_TEST(TryJoinTest,Join2Fail1P)138 TYPED_TEST(TryJoinTest, Join2Fail1P) {
139 EXPECT_EQ(TypeParam::TryJoinImpl(TypeParam::template pending<int>(),
140 TypeParam::template instant_fail<int>())(),
141 (TypeParam::template fail<int, int>()));
142 }
143
TYPED_TEST(TryJoinTest,Join2Fail2P)144 TYPED_TEST(TryJoinTest, Join2Fail2P) {
145 EXPECT_EQ(TypeParam::TryJoinImpl(TypeParam::template instant_fail<int>(),
146 TypeParam::template pending<int>())(),
147 (TypeParam::template fail<int, int>()));
148 }
149
TYPED_TEST(TryJoinTest,JoinStatus)150 TYPED_TEST(TryJoinTest, JoinStatus) {
151 EXPECT_EQ(TypeParam::TryJoinImpl(TypeParam::instant_ok_status(),
152 TypeParam::instant_ok_status())(),
153 TypeParam::ok(Empty{}, Empty{}));
154 }
155
156 } // namespace grpc_core
157
main(int argc,char ** argv)158 int main(int argc, char** argv) {
159 ::testing::InitGoogleTest(&argc, argv);
160 return RUN_ALL_TESTS();
161 }
162