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/arena_promise.h"
16
17 #include <grpc/event_engine/memory_allocator.h>
18
19 #include <array>
20 #include <memory>
21
22 #include "gtest/gtest.h"
23 #include "src/core/lib/iomgr/exec_ctx.h"
24 #include "src/core/lib/resource_quota/memory_quota.h"
25 #include "src/core/lib/resource_quota/resource_quota.h"
26 #include "src/core/util/ref_counted_ptr.h"
27 #include "test/core/promise/test_context.h"
28 #include "test/core/test_util/test_config.h"
29
30 namespace grpc_core {
31
TEST(ArenaPromiseTest,DefaultInitializationYieldsNoValue)32 TEST(ArenaPromiseTest, DefaultInitializationYieldsNoValue) {
33 auto arena = SimpleArenaAllocator()->MakeArena();
34 TestContext<Arena> context(arena.get());
35 ArenaPromise<int> p;
36 EXPECT_FALSE(p.has_value());
37 }
38
TEST(ArenaPromiseTest,AllocatedWorks)39 TEST(ArenaPromiseTest, AllocatedWorks) {
40 ExecCtx exec_ctx;
41 auto arena = SimpleArenaAllocator()->MakeArena();
42 TestContext<Arena> context(arena.get());
43 int x = 42;
44 ArenaPromise<int> p([x] { return Poll<int>(x); });
45 EXPECT_TRUE(p.has_value());
46 EXPECT_EQ(p(), Poll<int>(42));
47 p = ArenaPromise<int>([] { return Poll<int>(43); });
48 EXPECT_EQ(p(), Poll<int>(43));
49 }
50
TEST(ArenaPromiseTest,DestructionWorks)51 TEST(ArenaPromiseTest, DestructionWorks) {
52 ExecCtx exec_ctx;
53 auto arena = SimpleArenaAllocator()->MakeArena();
54 TestContext<Arena> context(arena.get());
55 auto x = std::make_shared<int>(42);
56 auto p = ArenaPromise<int>([x] { return Poll<int>(*x); });
57 ArenaPromise<int> q(std::move(p));
58 EXPECT_EQ(q(), Poll<int>(42));
59 }
60
TEST(ArenaPromiseTest,MoveAssignmentWorks)61 TEST(ArenaPromiseTest, MoveAssignmentWorks) {
62 ExecCtx exec_ctx;
63 auto arena = SimpleArenaAllocator()->MakeArena();
64 TestContext<Arena> context(arena.get());
65 auto x = std::make_shared<int>(42);
66 auto p = ArenaPromise<int>([x] { return Poll<int>(*x); });
67 p = ArenaPromise<int>();
68 }
69
TEST(ArenaPromiseTest,AllocatedUniquePtrWorks)70 TEST(ArenaPromiseTest, AllocatedUniquePtrWorks) {
71 ExecCtx exec_ctx;
72 auto arena = SimpleArenaAllocator()->MakeArena();
73 TestContext<Arena> context(arena.get());
74 std::array<int, 5> garbage = {0, 1, 2, 3, 4};
75 auto freer = [garbage](int* p) { free(p + garbage[0]); };
76 using Ptr = std::unique_ptr<int, decltype(freer)>;
77 Ptr x(([] {
78 int* p = static_cast<decltype(p)>(malloc(sizeof(*p)));
79 *p = 42;
80 return p;
81 })(),
82 freer);
83 static_assert(sizeof(x) > sizeof(arena_promise_detail::ArgType),
84 "This test assumes the unique ptr will go down the allocated "
85 "path for ArenaPromise");
86 ArenaPromise<Ptr> initial_promise(
87 [x = std::move(x)]() mutable { return Poll<Ptr>(std::move(x)); });
88 ArenaPromise<Ptr> p(std::move(initial_promise));
89 EXPECT_EQ(*p().value(), 42);
90 }
91
92 } // namespace grpc_core
93
main(int argc,char ** argv)94 int main(int argc, char** argv) {
95 grpc::testing::TestEnvironment give_me_a_name(&argc, argv);
96 ::testing::InitGoogleTest(&argc, argv);
97 return RUN_ALL_TESTS();
98 }
99