xref: /aosp_15_r20/external/grpc-grpc/test/core/promise/promise_factory_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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/detail/promise_factory.h"
16 
17 #include "absl/functional/bind_front.h"
18 #include "gtest/gtest.h"
19 
20 #include "src/core/lib/promise/poll.h"
21 
22 namespace grpc_core {
23 namespace promise_detail {
24 namespace testing {
25 
26 template <typename Arg, typename F>
MakeOnceFactory(F f)27 auto MakeOnceFactory(F f) {
28   return OncePromiseFactory<Arg, F>(std::move(f));
29 }
30 template <typename Arg, typename F>
MakeRepeatedFactory(F f)31 auto MakeRepeatedFactory(F f) {
32   return RepeatedPromiseFactory<Arg, F>(std::move(f));
33 }
34 
TEST(AdaptorTest,FactoryFromPromise)35 TEST(AdaptorTest, FactoryFromPromise) {
36   EXPECT_EQ(
37       MakeOnceFactory<void>([]() { return Poll<int>(Poll<int>(42)); }).Make()(),
38       Poll<int>(42));
39   EXPECT_EQ(MakeRepeatedFactory<void>([]() {
40               return Poll<int>(Poll<int>(42));
41             }).Make()(),
42             Poll<int>(42));
43   EXPECT_EQ(
44       MakeOnceFactory<void>([]() { return Poll<int>(Poll<int>(42)); }).Make()(),
45       Poll<int>(42));
46   EXPECT_EQ(MakeRepeatedFactory<void>([]() {
47               return Poll<int>(Poll<int>(42));
48             }).Make()(),
49             Poll<int>(42));
50 }
51 
TEST(AdaptorTest,FactoryFromBindFrontPromise)52 TEST(AdaptorTest, FactoryFromBindFrontPromise) {
53   EXPECT_EQ(MakeOnceFactory<void>(
54                 absl::bind_front([](int i) { return Poll<int>(i); }, 42))
55                 .Make()(),
56             Poll<int>(42));
57 }
58 
59 }  // namespace testing
60 }  // namespace promise_detail
61 
62 }  // namespace grpc_core
63 
main(int argc,char ** argv)64 int main(int argc, char** argv) {
65   ::testing::InitGoogleTest(&argc, argv);
66   return RUN_ALL_TESTS();
67 }
68