try_seq_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 <stdlib.h>
18 
19 #include <algorithm>
20 #include <string>
21 #include <vector>
22 
23 #include "gtest/gtest.h"
24 
25 namespace grpc_core {
26 
27 TEST(TrySeqTest, SucceedAndThen) {
28  EXPECT_EQ(TrySeq([] { return absl::StatusOr<int>(1); },
29  [](int i) {
30  return [i]() { return absl::StatusOr<int>(i + 1); };
31  })(),
33 }
34 
35 TEST(TrySeqTest, SucceedDirectlyAndThenDirectly) {
36  EXPECT_EQ(
37  TrySeq([] { return 1; }, [](int i) { return [i]() { return i + 1; }; })(),
39 }
40 
41 TEST(TrySeqTest, SucceedAndThenChangeType) {
42  EXPECT_EQ(
43  TrySeq([] { return absl::StatusOr<int>(42); },
44  [](int i) {
45  return [i]() {
47  };
48  })(),
50 }
51 
52 TEST(TrySeqTest, FailAndThen) {
54  [](int) {
55  return []() -> Poll<absl::StatusOr<double>> { abort(); };
56  })(),
59 }
60 
61 TEST(TrySeqTest, RawSucceedAndThen) {
62  EXPECT_EQ(TrySeq([] { return absl::OkStatus(); },
63  [] { return []() { return absl::OkStatus(); }; })(),
65 }
66 
67 TEST(TrySeqTest, RawFailAndThen) {
68  EXPECT_EQ(TrySeq([] { return absl::CancelledError(); },
69  []() { return []() -> Poll<absl::Status> { abort(); }; })(),
71 }
72 
73 TEST(TrySeqTest, RawSucceedAndThenValue) {
74  EXPECT_EQ(TrySeq([] { return absl::OkStatus(); },
75  [] { return []() { return absl::StatusOr<int>(42); }; })(),
77 }
78 
79 TEST(TrySeqIterTest, Ok) {
80  std::vector<int> v{1, 2, 3, 4, 5};
81  EXPECT_EQ(TrySeqIter(v.begin(), v.end(), 0,
82  [](int elem, int accum) {
83  return [elem, accum]() -> absl::StatusOr<int> {
84  return elem + accum;
85  };
86  })(),
88 }
89 
90 TEST(TrySeqIterTest, ErrorAt3) {
91  std::vector<int> v{1, 2, 3, 4, 5};
92  EXPECT_EQ(TrySeqIter(v.begin(), v.end(), 0,
93  [](int elem, int accum) {
94  return [elem, accum]() -> absl::StatusOr<int> {
95  if (elem < 3) {
96  return elem + accum;
97  }
98  if (elem == 3) {
99  return absl::CancelledError();
100  }
101  abort(); // unreachable
102  };
103  })(),
105 }
106 
107 } // namespace grpc_core
108 
109 int main(int argc, char** argv) {
110  ::testing::InitGoogleTest(&argc, argv);
111  return RUN_ALL_TESTS();
112 }
grpc_core
Definition: call_metric_recorder.h:31
absl::CancelledError
Status CancelledError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:331
elem
Timer elem
Definition: event_engine/iomgr_event_engine/timer_heap_test.cc:109
absl::OkStatus
Status OkStatus()
Definition: third_party/abseil-cpp/absl/status/status.h:882
grpc_core::TEST
TEST(AvlTest, NoOp)
Definition: avl_test.cc:21
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
try_seq.h
xds_interop_client.int
int
Definition: xds_interop_client.py:113
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
grpc_core::TEST
TEST(TrySeqIterTest, ErrorAt3)
Definition: try_seq_test.cc:90
grpc_core::TrySeqIter
promise_detail::BasicSeqIter< promise_detail::TrySeqTraits, Factory, Argument, Iter > TrySeqIter(Iter begin, Iter end, Argument argument, Factory factory)
Definition: try_seq.h:149
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::TrySeq
promise_detail::TrySeq< Functors... > TrySeq(Functors... functors)
Definition: try_seq.h:134
grpc_core::Poll
absl::variant< Pending, T > Poll
Definition: poll.h:38
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
to_string
static bool to_string(zval *from)
Definition: protobuf/php/ext/google/protobuf/convert.c:333
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
main
int main(int argc, char **argv)
Definition: try_seq_test.cc:109


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:41