// Copyright 2021 gRPC authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "src/core/lib/promise/arena_promise.h" #include #include #include "gtest/gtest.h" #include #include "src/core/lib/gprpp/ref_counted_ptr.h" #include "src/core/lib/iomgr/exec_ctx.h" #include "src/core/lib/resource_quota/memory_quota.h" #include "src/core/lib/resource_quota/resource_quota.h" #include "test/core/promise/test_context.h" #include "test/core/util/test_config.h" namespace grpc_core { class ArenaPromiseTest : public ::testing::Test { protected: MemoryAllocator memory_allocator_ = MemoryAllocator( ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test")); }; TEST_F(ArenaPromiseTest, DefaultInitializationYieldsNoValue) { auto arena = MakeScopedArena(1024, &memory_allocator_); TestContext context(arena.get()); ArenaPromise p; EXPECT_FALSE(p.has_value()); } TEST_F(ArenaPromiseTest, AllocatedWorks) { ExecCtx exec_ctx; auto arena = MakeScopedArena(1024, &memory_allocator_); TestContext context(arena.get()); int x = 42; ArenaPromise p([x] { return Poll(x); }); EXPECT_TRUE(p.has_value()); EXPECT_EQ(p(), Poll(42)); p = ArenaPromise([] { return Poll(43); }); EXPECT_EQ(p(), Poll(43)); } TEST_F(ArenaPromiseTest, DestructionWorks) { ExecCtx exec_ctx; auto arena = MakeScopedArena(1024, &memory_allocator_); TestContext context(arena.get()); auto x = std::make_shared(42); auto p = ArenaPromise([x] { return Poll(*x); }); ArenaPromise q(std::move(p)); EXPECT_EQ(q(), Poll(42)); } TEST_F(ArenaPromiseTest, MoveAssignmentWorks) { ExecCtx exec_ctx; auto arena = MakeScopedArena(1024, &memory_allocator_); TestContext context(arena.get()); auto x = std::make_shared(42); auto p = ArenaPromise([x] { return Poll(*x); }); p = ArenaPromise(); } TEST_F(ArenaPromiseTest, AllocatedUniquePtrWorks) { ExecCtx exec_ctx; auto arena = MakeScopedArena(1024, &memory_allocator_); TestContext context(arena.get()); std::array garbage = {0, 1, 2, 3, 4}; auto freer = [garbage](int* p) { free(p + garbage[0]); }; using Ptr = std::unique_ptr; Ptr x(([] { int* p = static_cast(malloc(sizeof(*p))); *p = 42; return p; })(), freer); static_assert(sizeof(x) > sizeof(arena_promise_detail::ArgType), "This test assumes the unique ptr will go down the allocated " "path for ArenaPromise"); ArenaPromise initial_promise( [x = std::move(x)]() mutable { return Poll(std::move(x)); }); ArenaPromise p(std::move(initial_promise)); EXPECT_EQ(*p().value(), 42); } } // namespace grpc_core int main(int argc, char** argv) { grpc::testing::TestEnvironment give_me_a_name(&argc, argv); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }