arena_promise_test.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include "src/core/lib/promise/arena_promise.h"
  15. #include <memory>
  16. #include <gtest/gtest.h>
  17. #include "src/core/lib/resource_quota/resource_quota.h"
  18. #include "test/core/promise/test_context.h"
  19. namespace grpc_core {
  20. static auto* g_memory_allocator = new MemoryAllocator(
  21. ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test"));
  22. TEST(ArenaPromiseTest, AllocatedWorks) {
  23. auto arena = MakeScopedArena(1024, g_memory_allocator);
  24. TestContext<Arena> context(arena.get());
  25. int x = 42;
  26. ArenaPromise<int> p([x] { return Poll<int>(x); });
  27. EXPECT_EQ(p(), Poll<int>(42));
  28. p = ArenaPromise<int>([] { return Poll<int>(43); });
  29. EXPECT_EQ(p(), Poll<int>(43));
  30. }
  31. TEST(ArenaPromiseTest, DestructionWorks) {
  32. auto arena = MakeScopedArena(1024, g_memory_allocator);
  33. TestContext<Arena> context(arena.get());
  34. auto x = std::make_shared<int>(42);
  35. auto p = ArenaPromise<int>([x] { return Poll<int>(*x); });
  36. ArenaPromise<int> q(std::move(p));
  37. EXPECT_EQ(q(), Poll<int>(42));
  38. }
  39. TEST(ArenaPromiseTest, MoveAssignmentWorks) {
  40. auto arena = MakeScopedArena(1024, g_memory_allocator);
  41. TestContext<Arena> context(arena.get());
  42. auto x = std::make_shared<int>(42);
  43. auto p = ArenaPromise<int>([x] { return Poll<int>(*x); });
  44. p = ArenaPromise<int>();
  45. }
  46. } // namespace grpc_core
  47. int main(int argc, char** argv) {
  48. ::testing::InitGoogleTest(&argc, argv);
  49. return RUN_ALL_TESTS();
  50. }