| 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 <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> |
| 30 |
P<T> instant_ok(T x) { |
| 31 |
return [x] { return absl::StatusOr<T>(x); }; |
| 32 |
} |
| 33 |
|
| 34 |
template <typename T> |
| 35 |
P<T> instant_fail() { |
| 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> |
| 50 |
P<T> pending() { |
| 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 |
} |