| 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/channel/call_finalization.h" |
| 16 |
|
| 17 |
#include <memory> |
| 18 |
#include <string> |
| 19 |
|
| 20 |
#include "absl/strings/str_cat.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/resource_quota/memory_quota.h" |
| 27 |
#include "src/core/lib/resource_quota/resource_quota.h" |
| 28 |
#include "test/core/promise/test_context.h" |
| 29 |
|
| 30 |
namespace grpc_core { |
| 31 |
|
| 32 |
static auto* g_memory_allocator = new MemoryAllocator( |
| 33 |
ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test")); |
| 34 |
|
| 35 |
TEST(CallFinalizationTest, Works) { |
| 36 |
auto arena = MakeScopedArena(1024, g_memory_allocator); |
| 37 |
std::string evidence; |
| 38 |
TestContext<Arena> context(arena.get()); |
| 39 |
CallFinalization finalization; |
| 40 |
auto p = std::make_shared<int>(42); |
| 41 |
finalization.Add([&evidence, p](const grpc_call_final_info* final_info) { |
| 42 |
evidence += absl::StrCat("FIRST", final_info->error_string, *p, "\n"); |
| 43 |
}); |
| 44 |
finalization.Add([&evidence, p](const grpc_call_final_info* final_info) { |
| 45 |
evidence += absl::StrCat("SECOND", final_info->error_string, *p, "\n"); |
| 46 |
}); |
| 47 |
grpc_call_final_info final_info{}; |
| 48 |
final_info.error_string = "123"; |
| 49 |
finalization.Run(&final_info); |
| 50 |
EXPECT_EQ(evidence, "SECOND12342\nFIRST12342\n"); |
| 51 |
} |
| 52 |
|
| 53 |
} // namespace grpc_core |
| 54 |
|
| 55 |
int main(int argc, char** argv) { |
| 56 |
::testing::InitGoogleTest(&argc, argv); |
| 57 |
return RUN_ALL_TESTS(); |
| 58 |
} |