try_join_test.cc
Go to the documentation of this file.
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 
16 
17 #include <functional>
18 #include <tuple>
19 #include <utility>
20 
21 #include "absl/utility/utility.h"
22 #include "gtest/gtest.h"
23 
24 namespace grpc_core {
25 
26 template <typename T>
27 using P = std::function<Poll<absl::StatusOr<T>>()>;
28 
29 template <typename T>
31  return [x] { return absl::StatusOr<T>(x); };
32 }
33 
34 template <typename T>
36  return [] { return absl::StatusOr<T>(); };
37 }
38 
39 template <typename... T>
40 Poll<absl::StatusOr<std::tuple<T...>>> ok(T... x) {
41  return absl::StatusOr<std::tuple<T...>>(absl::in_place, x...);
42 }
43 
44 template <typename... T>
45 Poll<absl::StatusOr<std::tuple<T...>>> fail() {
46  return absl::StatusOr<std::tuple<T...>>();
47 }
48 
49 template <typename T>
51  return []() -> Poll<absl::StatusOr<T>> { return Pending(); };
52 }
53 
54 TEST(TryJoinTest, Join1) { EXPECT_EQ(TryJoin(instant_ok(1))(), ok(1)); }
55 
56 TEST(TryJoinTest, Join1Fail) {
57  EXPECT_EQ(TryJoin(instant_fail<int>())(), fail<int>());
58 }
59 
60 TEST(TryJoinTest, Join2Success) {
61  EXPECT_EQ(TryJoin(instant_ok(1), instant_ok(2))(), ok(1, 2));
62 }
63 
64 TEST(TryJoinTest, Join2Fail1) {
65  EXPECT_EQ(TryJoin(instant_ok(1), instant_fail<int>())(), (fail<int, int>()));
66 }
67 
68 TEST(TryJoinTest, Join2Fail2) {
69  EXPECT_EQ(TryJoin(instant_fail<int>(), instant_ok(2))(), (fail<int, int>()));
70 }
71 
72 TEST(TryJoinTest, Join2Fail1P) {
73  EXPECT_EQ(TryJoin(pending<int>(), instant_fail<int>())(), (fail<int, int>()));
74 }
75 
76 TEST(TryJoinTest, Join2Fail2P) {
77  EXPECT_EQ(TryJoin(instant_fail<int>(), pending<int>())(), (fail<int, int>()));
78 }
79 
80 } // namespace grpc_core
81 
82 int main(int argc, char** argv) {
83  ::testing::InitGoogleTest(&argc, argv);
84  return RUN_ALL_TESTS();
85 }
main
int main(int argc, char **argv)
Definition: try_join_test.cc:82
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::TEST
TEST(AvlTest, NoOp)
Definition: avl_test.cc:21
grpc_core::instant_fail
P< T > instant_fail()
Definition: try_join_test.cc:35
grpc_core::pending
P< T > pending()
Definition: try_join_test.cc:50
T
#define T(upbtypeconst, upbtype, ctype, default_value)
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
grpc_core::Pending
Definition: poll.h:29
grpc_core::fail
Poll< absl::StatusOr< std::tuple< T... > > > fail()
Definition: try_join_test.cc:45
grpc_core::instant_ok
P< T > instant_ok(T x)
Definition: try_join_test.cc:30
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::ok
Poll< absl::StatusOr< std::tuple< T... > > > ok(T... x)
Definition: try_join_test.cc:40
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
P
Definition: miscompile_with_no_unique_address_test.cc:29
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
grpc_core::TryJoin
promise_detail::TryJoin< Promises... > TryJoin(Promises... promises)
Definition: try_join.h:77
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
try_join.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:40