| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "src/core/lib/channel/channel_stack.h" |
| 20 |
|
| 21 |
#include <limits.h> |
| 22 |
|
| 23 |
#include <string> |
| 24 |
|
| 25 |
#include "absl/status/status.h" |
| 26 |
#include "gtest/gtest.h" |
| 27 |
|
| 28 |
#include <grpc/support/alloc.h> |
| 29 |
|
| 30 |
#include "src/core/lib/channel/channel_args.h" |
| 31 |
#include "src/core/lib/channel/channel_args_preconditioning.h" |
| 32 |
#include "src/core/lib/config/core_configuration.h" |
| 33 |
#include "src/core/lib/gprpp/status_helper.h" |
| 34 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 35 |
#include "test/core/util/test_config.h" |
| 36 |
|
| 37 |
static grpc_error_handle channel_init_func(grpc_channel_element* elem, |
| 38 |
grpc_channel_element_args* args) { |
| 39 |
int test_value = grpc_channel_args_find_integer(args->channel_args, |
| 40 |
"test_key", {-1, 0, INT_MAX}); |
| 41 |
EXPECT_EQ(test_value, 42); |
| 42 |
auto* ee = grpc_channel_args_find_pointer< |
| 43 |
grpc_event_engine::experimental::EventEngine>( |
| 44 |
args->channel_args, GRPC_INTERNAL_ARG_EVENT_ENGINE); |
| 45 |
EXPECT_NE(ee, nullptr); |
| 46 |
EXPECT_TRUE(args->is_first); |
| 47 |
EXPECT_TRUE(args->is_last); |
| 48 |
*static_cast<int*>(elem->channel_data) = 0; |
| 49 |
return absl::OkStatus(); |
| 50 |
} |
| 51 |
|
| 52 |
static grpc_error_handle call_init_func( |
| 53 |
grpc_call_element* elem, const grpc_call_element_args* /*args*/) { |
| 54 |
++*static_cast<int*>(elem->channel_data); |
| 55 |
*static_cast<int*>(elem->call_data) = 0; |
| 56 |
return absl::OkStatus(); |
| 57 |
} |
| 58 |
|
| 59 |
static void channel_destroy_func(grpc_channel_element* /*elem*/) {} |
| 60 |
|
| 61 |
static void call_destroy_func(grpc_call_element* elem, |
| 62 |
const grpc_call_final_info* /*final_info*/, |
| 63 |
grpc_closure* /*ignored*/) { |
| 64 |
++*static_cast<int*>(elem->channel_data); |
| 65 |
} |
| 66 |
|
| 67 |
static void call_func(grpc_call_element* elem, |
| 68 |
grpc_transport_stream_op_batch* /*op*/) { |
| 69 |
++*static_cast<int*>(elem->call_data); |
| 70 |
} |
| 71 |
|
| 72 |
static void channel_func(grpc_channel_element* elem, |
| 73 |
grpc_transport_op* /*op*/) { |
| 74 |
++*static_cast<int*>(elem->channel_data); |
| 75 |
} |
| 76 |
|
| 77 |
static void free_channel(void* arg, grpc_error_handle /*error*/) { |
| 78 |
grpc_channel_stack_destroy(static_cast<grpc_channel_stack*>(arg)); |
| 79 |
gpr_free(arg); |
| 80 |
} |
| 81 |
|
| 82 |
static void free_call(void* arg, grpc_error_handle /*error*/) { |
| 83 |
grpc_call_stack_destroy(static_cast<grpc_call_stack*>(arg), nullptr, nullptr); |
| 84 |
gpr_free(arg); |
| 85 |
} |
| 86 |
|
| 87 |
TEST(ChannelStackTest, CreateChannelStack) { |
| 88 |
const grpc_channel_filter filter = { |
| 89 |
call_func, |
| 90 |
nullptr, |
| 91 |
channel_func, |
| 92 |
sizeof(int), |
| 93 |
call_init_func, |
| 94 |
grpc_call_stack_ignore_set_pollset_or_pollset_set, |
| 95 |
call_destroy_func, |
| 96 |
sizeof(int), |
| 97 |
channel_init_func, |
| 98 |
grpc_channel_stack_no_post_init, |
| 99 |
channel_destroy_func, |
| 100 |
grpc_channel_next_get_info, |
| 101 |
"some_test_filter"}; |
| 102 |
const grpc_channel_filter* filters = &filter; |
| 103 |
grpc_channel_stack* channel_stack; |
| 104 |
grpc_call_stack* call_stack; |
| 105 |
grpc_channel_element* channel_elem; |
| 106 |
grpc_call_element* call_elem; |
| 107 |
int* channel_data; |
| 108 |
int* call_data; |
| 109 |
grpc_core::ExecCtx exec_ctx; |
| 110 |
grpc_slice path = grpc_slice_from_static_string("/service/method"); |
| 111 |
|
| 112 |
channel_stack = static_cast<grpc_channel_stack*>( |
| 113 |
gpr_malloc(grpc_channel_stack_size(&filters, 1))); |
| 114 |
auto channel_args = grpc_core::CoreConfiguration::Get() |
| 115 |
.channel_args_preconditioning() |
| 116 |
.PreconditionChannelArgs(nullptr) |
| 117 |
.Set("test_key", 42); |
| 118 |
ASSERT_TRUE(GRPC_LOG_IF_ERROR( |
| 119 |
"grpc_channel_stack_init", |
| 120 |
grpc_channel_stack_init(1, free_channel, channel_stack, &filters, 1, |
| 121 |
channel_args, "test", channel_stack))); |
| 122 |
EXPECT_EQ(channel_stack->count, 1); |
| 123 |
channel_elem = grpc_channel_stack_element(channel_stack, 0); |
| 124 |
channel_data = static_cast<int*>(channel_elem->channel_data); |
| 125 |
EXPECT_EQ(*channel_data, 0); |
| 126 |
|
| 127 |
call_stack = |
| 128 |
static_cast<grpc_call_stack*>(gpr_malloc(channel_stack->call_stack_size)); |
| 129 |
const grpc_call_element_args args = { |
| 130 |
call_stack, /* call_stack */ |
| 131 |
nullptr, /* server_transport_data */ |
| 132 |
nullptr, /* context */ |
| 133 |
path, /* path */ |
| 134 |
gpr_get_cycle_counter(), /* start_time */ |
| 135 |
grpc_core::Timestamp::InfFuture(), /* deadline */ |
| 136 |
nullptr, /* arena */ |
| 137 |
nullptr, /* call_combiner */ |
| 138 |
}; |
| 139 |
grpc_error_handle error = |
| 140 |
grpc_call_stack_init(channel_stack, 1, free_call, call_stack, &args); |
| 141 |
ASSERT_TRUE(error.ok()) << grpc_core::StatusToString(error); |
| 142 |
EXPECT_EQ(call_stack->count, 1); |
| 143 |
call_elem = grpc_call_stack_element(call_stack, 0); |
| 144 |
EXPECT_EQ(call_elem->filter, channel_elem->filter); |
| 145 |
EXPECT_EQ(call_elem->channel_data, channel_elem->channel_data); |
| 146 |
call_data = static_cast<int*>(call_elem->call_data); |
| 147 |
EXPECT_EQ(*call_data, 0); |
| 148 |
EXPECT_EQ(*channel_data, 1); |
| 149 |
|
| 150 |
GRPC_CALL_STACK_UNREF(call_stack, "done"); |
| 151 |
grpc_core::ExecCtx::Get()->Flush(); |
| 152 |
EXPECT_EQ(*channel_data, 2); |
| 153 |
|
| 154 |
GRPC_CHANNEL_STACK_UNREF(channel_stack, "done"); |
| 155 |
|
| 156 |
grpc_slice_unref(path); |
| 157 |
} |
| 158 |
|
| 159 |
int main(int argc, char** argv) { |
| 160 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 161 |
::testing::InitGoogleTest(&argc, argv); |
| 162 |
grpc::testing::TestGrpcScope grpc_scope; |
| 163 |
return RUN_ALL_TESTS(); |
| 164 |
} |