try_join_test.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include "src/core/lib/promise/try_join.h"
  15. #include <gtest/gtest.h>
  16. namespace grpc_core {
  17. template <typename T>
  18. using P = std::function<Poll<absl::StatusOr<T>>()>;
  19. template <typename T>
  20. P<T> instant_ok(T x) {
  21. return [x] { return absl::StatusOr<T>(x); };
  22. }
  23. template <typename T>
  24. P<T> instant_fail() {
  25. return [] { return absl::StatusOr<T>(); };
  26. }
  27. template <typename... T>
  28. Poll<absl::StatusOr<std::tuple<T...>>> ok(T... x) {
  29. return absl::StatusOr<std::tuple<T...>>(absl::in_place, x...);
  30. }
  31. template <typename... T>
  32. Poll<absl::StatusOr<std::tuple<T...>>> fail() {
  33. return absl::StatusOr<std::tuple<T...>>();
  34. }
  35. template <typename T>
  36. P<T> pending() {
  37. return []() -> Poll<absl::StatusOr<T>> { return Pending(); };
  38. }
  39. TEST(TryJoinTest, Join1) { EXPECT_EQ(TryJoin(instant_ok(1))(), ok(1)); }
  40. TEST(TryJoinTest, Join1Fail) {
  41. EXPECT_EQ(TryJoin(instant_fail<int>())(), fail<int>());
  42. }
  43. TEST(TryJoinTest, Join2Success) {
  44. EXPECT_EQ(TryJoin(instant_ok(1), instant_ok(2))(), ok(1, 2));
  45. }
  46. TEST(TryJoinTest, Join2Fail1) {
  47. EXPECT_EQ(TryJoin(instant_ok(1), instant_fail<int>())(), (fail<int, int>()));
  48. }
  49. TEST(TryJoinTest, Join2Fail2) {
  50. EXPECT_EQ(TryJoin(instant_fail<int>(), instant_ok(2))(), (fail<int, int>()));
  51. }
  52. TEST(TryJoinTest, Join2Fail1P) {
  53. EXPECT_EQ(TryJoin(pending<int>(), instant_fail<int>())(), (fail<int, int>()));
  54. }
  55. TEST(TryJoinTest, Join2Fail2P) {
  56. EXPECT_EQ(TryJoin(instant_fail<int>(), pending<int>())(), (fail<int, int>()));
  57. }
  58. } // namespace grpc_core
  59. int main(int argc, char** argv) {
  60. ::testing::InitGoogleTest(&argc, argv);
  61. return RUN_ALL_TESTS();
  62. }