promise_factory_test.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/detail/promise_factory.h"
  15. #include <gtest/gtest.h>
  16. #include "absl/functional/bind_front.h"
  17. #include "src/core/lib/gprpp/capture.h"
  18. #include "src/core/lib/promise/promise.h"
  19. namespace grpc_core {
  20. namespace promise_detail {
  21. namespace testing {
  22. template <typename Arg, typename F>
  23. PromiseFactory<Arg, F> MakeFactory(F f) {
  24. return PromiseFactory<Arg, F>(std::move(f));
  25. }
  26. TEST(AdaptorTest, FactoryFromPromise) {
  27. EXPECT_EQ(
  28. MakeFactory<void>([]() { return Poll<int>(Poll<int>(42)); }).Once()(),
  29. Poll<int>(42));
  30. EXPECT_EQ(
  31. MakeFactory<void>([]() { return Poll<int>(Poll<int>(42)); }).Repeated()(),
  32. Poll<int>(42));
  33. EXPECT_EQ(MakeFactory<void>(Promise<int>([]() {
  34. return Poll<int>(Poll<int>(42));
  35. })).Once()(),
  36. Poll<int>(42));
  37. EXPECT_EQ(MakeFactory<void>(Promise<int>([]() {
  38. return Poll<int>(Poll<int>(42));
  39. })).Repeated()(),
  40. Poll<int>(42));
  41. }
  42. TEST(AdaptorTest, FactoryFromBindFrontPromise) {
  43. EXPECT_EQ(MakeFactory<void>(
  44. absl::bind_front([](int i) { return Poll<int>(i); }, 42))
  45. .Once()(),
  46. Poll<int>(42));
  47. }
  48. TEST(AdaptorTest, FactoryFromCapturePromise) {
  49. EXPECT_EQ(MakeFactory<void>(Capture([](int* i) { return Poll<int>(*i); }, 42))
  50. .Once()(),
  51. Poll<int>(42));
  52. }
  53. } // namespace testing
  54. } // namespace promise_detail
  55. } // namespace grpc_core
  56. int main(int argc, char** argv) {
  57. ::testing::InitGoogleTest(&argc, argv);
  58. return RUN_ALL_TESTS();
  59. }