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
TEST(CallFinalizationTest,Works)32 TEST(CallFinalizationTest, Works) {
33 auto memory_allocator = MemoryAllocator(
34 ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test"));
35 auto arena = MakeScopedArena(1024, &memory_allocator);
36 std::string evidence;
37 TestContext<Arena> context(arena.get());
38 CallFinalization finalization;
39 auto p = std::make_shared<int>(42);
40 finalization.Add([&evidence, p](const grpc_call_final_info* final_info) {
41 evidence += absl::StrCat("FIRST", final_info->error_string, *p, "\n");
42 });
43 finalization.Add([&evidence, p](const grpc_call_final_info* final_info) {
44 evidence += absl::StrCat("SECOND", final_info->error_string, *p, "\n");
45 });
46 grpc_call_final_info final_info{};
47 final_info.error_string = "123";
48 finalization.Run(&final_info);
49 EXPECT_EQ(evidence, "SECOND12342\nFIRST12342\n");
50 }
51
52 } // namespace grpc_core
53
main(int argc,char ** argv)54 int main(int argc, char** argv) {
55 ::testing::InitGoogleTest(&argc, argv);
56 return RUN_ALL_TESTS();
57 }
58