Debian Codegraph @

test/core/promise/arena_promise_test.cc

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