call_finalization_test.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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/channel/call_finalization.h"
  15. #include <gtest/gtest.h>
  16. #include "src/core/lib/resource_quota/resource_quota.h"
  17. #include "test/core/promise/test_context.h"
  18. namespace grpc_core {
  19. static auto* g_memory_allocator = new MemoryAllocator(
  20. ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test"));
  21. TEST(CallFinalizationTest, Works) {
  22. auto arena = MakeScopedArena(1024, g_memory_allocator);
  23. std::string evidence;
  24. TestContext<Arena> context(arena.get());
  25. CallFinalization finalization;
  26. auto p = std::make_shared<int>(42);
  27. finalization.Add([&evidence, p](const grpc_call_final_info* final_info) {
  28. evidence += absl::StrCat("FIRST", final_info->error_string, *p, "\n");
  29. });
  30. finalization.Add([&evidence, p](const grpc_call_final_info* final_info) {
  31. evidence += absl::StrCat("SECOND", final_info->error_string, *p, "\n");
  32. });
  33. grpc_call_final_info final_info{};
  34. final_info.error_string = "123";
  35. finalization.Run(&final_info);
  36. EXPECT_EQ(evidence, "SECOND12342\nFIRST12342\n");
  37. }
  38. } // namespace grpc_core
  39. int main(int argc, char** argv) {
  40. ::testing::InitGoogleTest(&argc, argv);
  41. return RUN_ALL_TESTS();
  42. }