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