test/core/end2end/tests/channelz.cc
Go to the documentation of this file.
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 <stdio.h>
20 #include <string.h>
21 
22 #include <grpc/byte_buffer.h>
23 #include <grpc/grpc.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/time.h>
27 
34 
35 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
36 
38  const char* test_name,
39  grpc_channel_args* client_args,
40  grpc_channel_args* server_args) {
42  gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
43  f = config.create_fixture(client_args, server_args);
44  config.init_server(&f, server_args);
45  config.init_client(&f, client_args);
46  return f;
47 }
48 
51 }
52 
54  return n_seconds_from_now(5);
55 }
56 
58  grpc_event ev;
59  do {
61  } while (ev.type != GRPC_QUEUE_SHUTDOWN);
62 }
63 
65  if (!f->server) return;
66  grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
67  grpc_event ev;
68  do {
70  nullptr);
71  } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
72  grpc_server_destroy(f->server);
73  f->server = nullptr;
74 }
75 
77  if (!f->client) return;
78  grpc_channel_destroy(f->client);
79  f->client = nullptr;
80 }
81 
85 
87  drain_cq(f->cq);
89 }
90 
93  bool request_is_success) {
94  grpc_call* c;
95  grpc_call* s;
96  cq_verifier* cqv = cq_verifier_create(f.cq);
97  grpc_op ops[6];
98  grpc_op* op;
106  int was_cancelled = 2;
107 
108  gpr_timespec deadline = five_seconds_from_now();
109  c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
110  grpc_slice_from_static_string("/foo"), nullptr,
111  deadline, nullptr);
112  GPR_ASSERT(c);
113 
118 
119  memset(ops, 0, sizeof(ops));
120  op = ops;
123  op->flags = 0;
124  op->reserved = nullptr;
125  op++;
127  op->flags = 0;
128  op->reserved = nullptr;
129  op++;
132  op->flags = 0;
133  op->reserved = nullptr;
134  op++;
140  op->flags = 0;
141  op->reserved = nullptr;
142  op++;
143  error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
144  nullptr);
146 
147  error =
149  &request_metadata_recv, f.cq, f.cq, tag(101));
151  CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
152  cq_verify(cqv);
153 
154  memset(ops, 0, sizeof(ops));
155  op = ops;
158  op->flags = 0;
159  op->reserved = nullptr;
160  op++;
164  request_is_success ? GRPC_STATUS_OK : GRPC_STATUS_UNIMPLEMENTED;
165  grpc_slice status_details = grpc_slice_from_static_string("xyz");
166  op->data.send_status_from_server.status_details = &status_details;
167  op->flags = 0;
168  op->reserved = nullptr;
169  op++;
172  op->flags = 0;
173  op->reserved = nullptr;
174  op++;
175  error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
176  nullptr);
178 
179  CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
180  CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
181  cq_verify(cqv);
182 
183  GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
186 
192 
194  grpc_call_unref(s);
195 
196  cq_verifier_destroy(cqv);
197 }
198 
201 
202  grpc_arg arg[] = {
205  0),
207  const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), true)};
209 
210  f = begin_test(config, "test_channelz", &args, &args);
211  grpc_core::channelz::ChannelNode* channelz_channel =
213  GPR_ASSERT(channelz_channel != nullptr);
214 
215  grpc_core::channelz::ServerNode* channelz_server =
217  GPR_ASSERT(channelz_server != nullptr);
218 
219  std::string json = channelz_channel->RenderJsonString();
220  // nothing is present yet
221  GPR_ASSERT(json.find("\"callsStarted\"") == json.npos);
222  GPR_ASSERT(json.find("\"callsFailed\"") == json.npos);
223  GPR_ASSERT(json.find("\"callsSucceeded\"") == json.npos);
224 
225  // one successful request
226  run_one_request(config, f, true);
227 
228  json = channelz_channel->RenderJsonString();
229  GPR_ASSERT(json.find("\"callsStarted\":\"1\"") != json.npos);
230  GPR_ASSERT(json.find("\"callsSucceeded\":\"1\"") != json.npos);
231 
232  // one failed request
233  run_one_request(config, f, false);
234 
235  json = channelz_channel->RenderJsonString();
236  GPR_ASSERT(json.find("\"callsStarted\":\"2\"") != json.npos);
237  GPR_ASSERT(json.find("\"callsFailed\":\"1\"") != json.npos);
238  GPR_ASSERT(json.find("\"callsSucceeded\":\"1\"") != json.npos);
239  // channel tracing is not enabled, so these should not be preset.
240  GPR_ASSERT(json.find("\"trace\"") == json.npos);
241  GPR_ASSERT(json.find("\"description\":\"Channel created\"") == json.npos);
242  GPR_ASSERT(json.find("\"severity\":\"CT_INFO\"") == json.npos);
243 
244  json = channelz_server->RenderJsonString();
245  GPR_ASSERT(json.find("\"callsStarted\":\"2\"") != json.npos);
246  GPR_ASSERT(json.find("\"callsFailed\":\"1\"") != json.npos);
247  GPR_ASSERT(json.find("\"callsSucceeded\":\"1\"") != json.npos);
248  // channel tracing is not enabled, so these should not be preset.
249  GPR_ASSERT(json.find("\"trace\"") == json.npos);
250  GPR_ASSERT(json.find("\"description\":\"Channel created\"") == json.npos);
251  GPR_ASSERT(json.find("\"severity\":\"CT_INFO\"") == json.npos);
252 
253  json = channelz_server->RenderServerSockets(0, 100);
254  GPR_ASSERT(json.find("\"end\":true") != json.npos);
255 
256  end_test(&f);
257  config.tear_down_data(&f);
258 }
259 
262 
263  grpc_arg arg[] = {
266  1024 * 1024),
268  const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), true)};
270 
271  f = begin_test(config, "test_channelz_with_channel_trace", &args, &args);
272  grpc_core::channelz::ChannelNode* channelz_channel =
274  GPR_ASSERT(channelz_channel != nullptr);
275 
276  grpc_core::channelz::ServerNode* channelz_server =
278  GPR_ASSERT(channelz_server != nullptr);
279 
280  run_one_request(config, f, true);
281 
282  std::string json = channelz_channel->RenderJsonString();
283  GPR_ASSERT(json.find("\"trace\"") != json.npos);
284  GPR_ASSERT(json.find("\"description\":\"Channel created\"") != json.npos);
285  GPR_ASSERT(json.find("\"severity\":\"CT_INFO\"") != json.npos);
286 
287  json = channelz_server->RenderJsonString();
288  GPR_ASSERT(json.find("\"trace\"") != json.npos);
289  GPR_ASSERT(json.find("\"description\":\"Server created\"") != json.npos);
290  GPR_ASSERT(json.find("\"severity\":\"CT_INFO\"") != json.npos);
291 
292  end_test(&f);
293  config.tear_down_data(&f);
294 }
295 
298 
299  grpc_arg arg[] = {
302  0),
304  const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), false)};
306 
307  f = begin_test(config, "test_channelz_disabled", &args, &args);
308  grpc_core::channelz::ChannelNode* channelz_channel =
310  GPR_ASSERT(channelz_channel == nullptr);
311  // one successful request
312  run_one_request(config, f, true);
313  GPR_ASSERT(channelz_channel == nullptr);
314  end_test(&f);
315  config.tear_down_data(&f);
316 }
317 
322 }
323 
324 void channelz_pre_init(void) {}
grpc_arg
Definition: grpc_types.h:103
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
grpc_op::flags
uint32_t flags
Definition: grpc_types.h:644
grpc_call_error
grpc_call_error
Definition: grpc_types.h:464
grpc_call_details_destroy
GRPCAPI void grpc_call_details_destroy(grpc_call_details *details)
Definition: call_details.cc:36
grpc_call_details_init
GRPCAPI void grpc_call_details_init(grpc_call_details *details)
Definition: call_details.cc:30
grpc_op::grpc_op_data::grpc_op_recv_status_on_client::trailing_metadata
grpc_metadata_array * trailing_metadata
Definition: grpc_types.h:701
grpc_timeout_seconds_to_deadline
gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s)
Definition: test/core/util/test_config.cc:81
log.h
grpc_op::grpc_op_data::grpc_op_recv_status_on_client::status
grpc_status_code * status
Definition: grpc_types.h:702
memset
return memset(p, 0, total)
n_seconds_from_now
static gpr_timespec n_seconds_from_now(int n)
Definition: test/core/end2end/tests/channelz.cc:49
grpc_call_details::flags
uint32_t flags
Definition: grpc_types.h:589
grpc_op::grpc_op_data::send_initial_metadata
struct grpc_op::grpc_op_data::grpc_op_send_initial_metadata send_initial_metadata
grpc_metadata_array
Definition: grpc_types.h:579
grpc_call_details
Definition: grpc_types.h:585
shutdown_client
static void shutdown_client(grpc_end2end_test_fixture *f)
Definition: test/core/end2end/tests/channelz.cc:76
grpc_op::reserved
void * reserved
Definition: grpc_types.h:646
run_one_request
static void run_one_request(grpc_end2end_test_config, grpc_end2end_test_fixture f, bool request_is_success)
Definition: test/core/end2end/tests/channelz.cc:91
string.h
channelz_pre_init
void channelz_pre_init(void)
Definition: test/core/end2end/tests/channelz.cc:324
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
GRPC_QUEUE_SHUTDOWN
@ GRPC_QUEUE_SHUTDOWN
Definition: grpc_types.h:554
GRPC_OP_COMPLETE
@ GRPC_OP_COMPLETE
Definition: grpc_types.h:558
test_channelz_disabled
static void test_channelz_disabled(grpc_end2end_test_config config)
Definition: test/core/end2end/tests/channelz.cc:296
GRPC_CALL_OK
@ GRPC_CALL_OK
Definition: grpc_types.h:466
status
absl::Status status
Definition: rls.cc:251
GRPC_ARG_ENABLE_CHANNELZ
#define GRPC_ARG_ENABLE_CHANNELZ
Definition: grpc_types.h:323
time.h
grpc_call_details::method
grpc_slice method
Definition: grpc_types.h:586
grpc_end2end_test_config
Definition: end2end_tests.h:53
grpc_channel_args
Definition: grpc_types.h:132
grpc_core::channelz::ServerNode
Definition: channelz.h:239
shutdown_server
static void shutdown_server(grpc_end2end_test_fixture *f)
Definition: test/core/end2end/tests/channelz.cc:64
grpc_op::data
union grpc_op::grpc_op_data data
grpc_end2end_test_fixture
Definition: end2end_tests.h:46
begin_test
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, const char *test_name, grpc_channel_args *client_args, grpc_channel_args *server_args)
Definition: test/core/end2end/tests/channelz.cc:37
grpc_metadata_array_destroy
GRPCAPI void grpc_metadata_array_destroy(grpc_metadata_array *array)
Definition: metadata_array.cc:35
grpc_server_request_call
GRPCAPI grpc_call_error grpc_server_request_call(grpc_server *server, grpc_call **call, grpc_call_details *details, grpc_metadata_array *request_metadata, grpc_completion_queue *cq_bound_to_call, grpc_completion_queue *cq_for_notification, void *tag_new)
Definition: src/core/lib/surface/server.cc:1526
trailing_metadata_recv
static grpc_metadata_array trailing_metadata_recv
Definition: test/core/fling/client.cc:43
channelz_registry.h
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GRPC_STATUS_OK
@ GRPC_STATUS_OK
Definition: include/grpc/impl/codegen/status.h:30
GRPC_OP_RECV_INITIAL_METADATA
@ GRPC_OP_RECV_INITIAL_METADATA
Definition: grpc_types.h:617
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
GRPC_OP_SEND_STATUS_FROM_SERVER
@ GRPC_OP_SEND_STATUS_FROM_SERVER
Definition: grpc_types.h:612
grpc_call_unref
GRPCAPI void grpc_call_unref(grpc_call *call)
Definition: call.cc:1770
grpc_op::grpc_op_data::grpc_op_send_status_from_server::status
grpc_status_code status
Definition: grpc_types.h:673
grpc_op::grpc_op_data::grpc_op_recv_status_on_client::error_string
const char ** error_string
Definition: grpc_types.h:707
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_event
Definition: grpc_types.h:564
grpc_completion_queue
Definition: completion_queue.cc:347
cq_verifier_destroy
void cq_verifier_destroy(cq_verifier *v)
Definition: cq_verifier.cc:92
grpc_channel_get_channelz_node
grpc_core::channelz::ChannelNode * grpc_channel_get_channelz_node(grpc_channel *channel)
Definition: src/core/lib/surface/channel.h:183
test_channelz
static void test_channelz(grpc_end2end_test_config config)
Definition: test/core/end2end/tests/channelz.cc:199
grpc.h
grpc_call
struct grpc_call grpc_call
Definition: grpc_types.h:70
grpc_op
Definition: grpc_types.h:640
arg
Definition: cmdline.cc:40
grpc_slice_from_static_string
GPRAPI grpc_slice grpc_slice_from_static_string(const char *source)
Definition: slice/slice.cc:89
cq_verifier_create
cq_verifier * cq_verifier_create(grpc_completion_queue *cq)
Definition: cq_verifier.cc:86
was_cancelled
static int was_cancelled
Definition: test/core/fling/server.cc:58
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
cq_verifier
Definition: cq_verifier.cc:76
request_metadata_recv
static grpc_metadata_array request_metadata_recv
Definition: test/core/fling/server.cc:48
grpc_server_destroy
GRPCAPI void grpc_server_destroy(grpc_server *server)
Definition: src/core/lib/surface/server.cc:1519
CQ_EXPECT_COMPLETION
#define CQ_EXPECT_COMPLETION(v, tag, success)
Definition: cq_verifier.h:58
end2end_tests.h
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
grpc_op::op
grpc_op_type op
Definition: grpc_types.h:642
grpc_op::grpc_op_data::grpc_op_send_initial_metadata::count
size_t count
Definition: grpc_types.h:653
grpc_op::grpc_op_data::grpc_op_recv_status_on_client::status_details
grpc_slice * status_details
Definition: grpc_types.h:703
grpc_core::channelz::ChannelNode
Definition: channelz.h:178
details
static grpc_slice details
Definition: test/core/fling/client.cc:46
grpc_channel_create_call
GRPCAPI grpc_call * grpc_channel_create_call(grpc_channel *channel, grpc_call *parent_call, uint32_t propagation_mask, grpc_completion_queue *completion_queue, grpc_slice method, const grpc_slice *host, gpr_timespec deadline, void *reserved)
Definition: channel.cc:311
grpc_op::grpc_op_data::recv_close_on_server
struct grpc_op::grpc_op_data::grpc_op_recv_close_on_server recv_close_on_server
grpc_core::channelz::BaseNode::RenderJsonString
std::string RenderJsonString()
Definition: src/core/lib/channel/channelz.cc:67
GPR_ARRAY_SIZE
#define GPR_ARRAY_SIZE(array)
Definition: useful.h:129
GRPC_PROPAGATE_DEFAULTS
#define GRPC_PROPAGATE_DEFAULTS
Definition: propagation_bits.h:45
grpc_op::grpc_op_data::send_status_from_server
struct grpc_op::grpc_op_data::grpc_op_send_status_from_server send_status_from_server
channelz
void channelz(grpc_end2end_test_config config)
Definition: test/core/end2end/tests/channelz.cc:318
grpc_channel_arg_integer_create
grpc_arg grpc_channel_arg_integer_create(char *name, int value)
Definition: channel_args.cc:484
cq_verifier.h
grpc_completion_queue_destroy
GRPCAPI void grpc_completion_queue_destroy(grpc_completion_queue *cq)
Definition: completion_queue.cc:1424
GRPC_OP_SEND_INITIAL_METADATA
@ GRPC_OP_SEND_INITIAL_METADATA
Definition: grpc_types.h:598
GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE
#define GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE
Definition: grpc_types.h:318
alloc.h
tag
static void * tag(intptr_t t)
Definition: test/core/end2end/tests/channelz.cc:35
grpc_op::grpc_op_data::recv_status_on_client
struct grpc_op::grpc_op_data::grpc_op_recv_status_on_client recv_status_on_client
grpc_op::grpc_op_data::grpc_op_send_status_from_server::trailing_metadata_count
size_t trailing_metadata_count
Definition: grpc_types.h:671
grpc_core::channelz::ServerNode::RenderServerSockets
std::string RenderServerSockets(intptr_t start_socket_id, intptr_t max_results)
Definition: src/core/lib/channel/channelz.cc:280
grpc_server_shutdown_and_notify
GRPCAPI void grpc_server_shutdown_and_notify(grpc_server *server, grpc_completion_queue *cq, void *tag)
Definition: src/core/lib/surface/server.cc:1503
grpc_completion_queue_next
GRPCAPI grpc_event grpc_completion_queue_next(grpc_completion_queue *cq, gpr_timespec deadline, void *reserved)
Definition: completion_queue.cc:1133
cq_verify
void cq_verify(cq_verifier *v, int timeout_sec)
Definition: cq_verifier.cc:268
grpc_completion_queue_shutdown
GRPCAPI void grpc_completion_queue_shutdown(grpc_completion_queue *cq)
Definition: completion_queue.cc:1416
grpc_channel_destroy
GRPCAPI void grpc_channel_destroy(grpc_channel *channel)
Definition: channel.cc:437
server.h
config_s
Definition: bloaty/third_party/zlib/deflate.c:120
GRPC_OP_RECV_CLOSE_ON_SERVER
@ GRPC_OP_RECV_CLOSE_ON_SERVER
Definition: grpc_types.h:633
GRPC_STATUS_UNIMPLEMENTED
@ GRPC_STATUS_UNIMPLEMENTED
Definition: include/grpc/impl/codegen/status.h:124
grpc_op::grpc_op_data::recv_initial_metadata
struct grpc_op::grpc_op_data::grpc_op_recv_initial_metadata recv_initial_metadata
grpc_op::grpc_op_data::grpc_op_send_status_from_server::status_details
grpc_slice * status_details
Definition: grpc_types.h:677
gpr_timespec
Definition: gpr_types.h:50
grpc_event::type
grpc_completion_type type
Definition: grpc_types.h:566
end_test
static void end_test(grpc_end2end_test_fixture *f)
Definition: test/core/end2end/tests/channelz.cc:82
GRPC_OP_RECV_STATUS_ON_CLIENT
@ GRPC_OP_RECV_STATUS_ON_CLIENT
Definition: grpc_types.h:627
grpc_op::grpc_op_data::grpc_op_recv_initial_metadata::recv_initial_metadata
grpc_metadata_array * recv_initial_metadata
Definition: grpc_types.h:685
grpc_core::CppImplOf< Server, grpc_server >::FromC
static Server * FromC(grpc_server *c_type)
Definition: cpp_impl_of.h:30
grpc_slice_str_cmp
GPRAPI int grpc_slice_str_cmp(grpc_slice a, const char *b)
Definition: slice/slice.cc:426
test_channelz_with_channel_trace
static void test_channelz_with_channel_trace(grpc_end2end_test_config config)
Definition: test/core/end2end/tests/channelz.cc:260
op
static grpc_op * op
Definition: test/core/fling/client.cc:47
ops
static grpc_op ops[6]
Definition: test/core/fling/client.cc:39
initial_metadata_recv
static grpc_metadata_array initial_metadata_recv
Definition: test/core/fling/client.cc:42
grpc_call_start_batch
GRPCAPI grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, size_t nops, void *tag, void *reserved)
Definition: call.cc:1831
grpc_core::Server::channelz_node
channelz::ServerNode * channelz_node() const
Definition: src/core/lib/surface/server.h:133
grpc_event::tag
void * tag
Definition: grpc_types.h:576
grpc_op::grpc_op_data::grpc_op_recv_close_on_server::cancelled
int * cancelled
Definition: grpc_types.h:714
cq
static grpc_completion_queue * cq
Definition: test/core/fling/client.cc:37
GRPC_OP_SEND_CLOSE_FROM_CLIENT
@ GRPC_OP_SEND_CLOSE_FROM_CLIENT
Definition: grpc_types.h:607
call_details
static grpc_call_details call_details
Definition: test/core/fling/server.cc:47
grpc_metadata_array_init
GRPCAPI void grpc_metadata_array_init(grpc_metadata_array *array)
Definition: metadata_array.cc:30
five_seconds_from_now
static gpr_timespec five_seconds_from_now(void)
Definition: test/core/end2end/tests/channelz.cc:53
drain_cq
static void drain_cq(grpc_completion_queue *cq)
Definition: test/core/end2end/tests/channelz.cc:57
channel.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:53