aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorGravatar ncteisen <ncteisen@gmail.com>2018-07-13 14:05:27 -0700
committerGravatar ncteisen <ncteisen@gmail.com>2018-07-16 23:29:29 -0700
commit97066fd0e46a82ffa6b012117cfd428888d255f3 (patch)
tree73c07c3854289064479f89cf9b88046280131eac /test
parent9a6c722e301be70715fa4f16cea22d172d605615 (diff)
Support for GetTopChannels
Diffstat (limited to 'test')
-rw-r--r--test/core/channel/channel_trace_test.cc2
-rw-r--r--test/core/channel/channelz_test.cc81
-rw-r--r--test/core/end2end/tests/channelz.cc16
-rw-r--r--test/cpp/util/channel_trace_proto_helper.cc14
-rw-r--r--test/cpp/util/channel_trace_proto_helper.h5
5 files changed, 97 insertions, 21 deletions
diff --git a/test/core/channel/channel_trace_test.cc b/test/core/channel/channel_trace_test.cc
index bbddee3f14..07b0e3de06 100644
--- a/test/core/channel/channel_trace_test.cc
+++ b/test/core/channel/channel_trace_test.cc
@@ -88,7 +88,7 @@ void AddSimpleTrace(ChannelTrace* tracer) {
void ValidateChannelTrace(ChannelTrace* tracer,
size_t expected_num_event_logged, size_t max_nodes) {
if (!max_nodes) return;
- grpc_json* json = tracer->RenderJSON();
+ grpc_json* json = tracer->RenderJson();
EXPECT_NE(json, nullptr);
char* json_str = grpc_json_dump_to_string(json, 0);
grpc_json_destroy(json);
diff --git a/test/core/channel/channelz_test.cc b/test/core/channel/channelz_test.cc
index 058eea914c..1ed36acb61 100644
--- a/test/core/channel/channelz_test.cc
+++ b/test/core/channel/channelz_test.cc
@@ -67,9 +67,39 @@ grpc_json* GetJsonChild(grpc_json* parent, const char* key) {
return nullptr;
}
+void ValidateJsonArraySize(grpc_json* json, const char* key,
+ size_t expected_size) {
+ grpc_json* arr = GetJsonChild(json, key);
+ if (expected_size == 0) {
+ ASSERT_EQ(arr, nullptr);
+ return;
+ }
+ ASSERT_NE(arr, nullptr);
+ ASSERT_EQ(arr->type, GRPC_JSON_ARRAY);
+ size_t count = 0;
+ for (grpc_json* child = arr->child; child != nullptr; child = child->next) {
+ ++count;
+ }
+ ASSERT_EQ(count, expected_size);
+}
+
+void ValidateGetTopChannels(size_t expected_channels) {
+ char* json_str = ChannelzRegistry::GetTopChannels(0);
+ grpc::testing::ValidateGetTopChannelsResponseProtoJsonTranslation(json_str);
+ grpc_json* parsed_json = grpc_json_parse_string(json_str);
+ // This check will naturally have to change when we support pagination.
+ // tracked: https://github.com/grpc/grpc/issues/16019.
+ ValidateJsonArraySize(parsed_json, "channel", expected_channels);
+ grpc_json* end = GetJsonChild(parsed_json, "end");
+ EXPECT_NE(end, nullptr);
+ EXPECT_EQ(end->type, GRPC_JSON_TRUE);
+ grpc_json_destroy(parsed_json);
+ gpr_free(json_str);
+}
+
class ChannelFixture {
public:
- ChannelFixture(int max_trace_nodes) {
+ ChannelFixture(int max_trace_nodes = 0) {
grpc_arg client_a[2];
client_a[0].type = GRPC_ARG_INTEGER;
client_a[0].key =
@@ -99,6 +129,10 @@ struct validate_channel_data_args {
void ValidateChildInteger(grpc_json* json, int64_t expect, const char* key) {
grpc_json* gotten_json = GetJsonChild(json, key);
+ if (expect == 0) {
+ ASSERT_EQ(gotten_json, nullptr);
+ return;
+ }
ASSERT_NE(gotten_json, nullptr);
int64_t gotten_number = (int64_t)strtol(gotten_json->value, nullptr, 0);
EXPECT_EQ(gotten_number, expect);
@@ -115,7 +149,7 @@ void ValidateCounters(char* json_str, validate_channel_data_args args) {
}
void ValidateChannel(ChannelNode* channel, validate_channel_data_args args) {
- char* json_str = channel->RenderJSON();
+ char* json_str = channel->RenderJsonString();
grpc::testing::ValidateChannelProtoJsonTranslation(json_str);
ValidateCounters(json_str, args);
gpr_free(json_str);
@@ -141,9 +175,7 @@ TEST_P(ChannelzChannelTest, BasicChannel) {
ChannelFixture channel(GetParam());
ChannelNode* channelz_channel =
grpc_channel_get_channelz_node(channel.channel());
- char* json_str = channelz_channel->RenderJSON();
- ValidateCounters(json_str, {0, 0, 0});
- gpr_free(json_str);
+ ValidateChannel(channelz_channel, {0, 0, 0});
}
TEST(ChannelzChannelTest, ChannelzDisabled) {
@@ -199,6 +231,45 @@ TEST_P(ChannelzChannelTest, LastCallStartedMillis) {
EXPECT_NE(millis1, millis4);
}
+TEST(ChannelzGetTopChannelsTest, BasicTest) {
+ grpc_core::ExecCtx exec_ctx;
+ ChannelFixture channel;
+ ValidateGetTopChannels(1);
+}
+
+TEST(ChannelzGetTopChannelsTest, NoChannelsTest) {
+ grpc_core::ExecCtx exec_ctx;
+ ValidateGetTopChannels(0);
+}
+
+TEST(ChannelzGetTopChannelsTest, ManyChannelsTest) {
+ grpc_core::ExecCtx exec_ctx;
+ ChannelFixture channels[10];
+ (void)channels; // suppress unused variable error
+ ValidateGetTopChannels(10);
+}
+
+TEST(ChannelzGetTopChannelsTest, InternalChannelTest) {
+ grpc_core::ExecCtx exec_ctx;
+ ChannelFixture channels[10];
+ (void)channels; // suppress unused variable error
+ // create an internal channel
+ grpc_arg client_a[2];
+ client_a[0].type = GRPC_ARG_INTEGER;
+ client_a[0].key =
+ const_cast<char*>(GRPC_ARG_CHANNELZ_CHANNEL_IS_INTERNAL_CHANNEL);
+ client_a[0].value.integer = 1;
+ client_a[1].type = GRPC_ARG_INTEGER;
+ client_a[1].key = const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ);
+ client_a[1].value.integer = true;
+ grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a};
+ grpc_channel* internal_channel =
+ grpc_insecure_channel_create("fake_target", &client_args, nullptr);
+ // The internal channel should not be returned from the request
+ ValidateGetTopChannels(10);
+ grpc_channel_destroy(internal_channel);
+}
+
INSTANTIATE_TEST_CASE_P(ChannelzChannelTestSweep, ChannelzChannelTest,
::testing::Values(0, 1, 2, 6, 10, 15));
diff --git a/test/core/end2end/tests/channelz.cc b/test/core/end2end/tests/channelz.cc
index eb052119ca..533703a2be 100644
--- a/test/core/end2end/tests/channelz.cc
+++ b/test/core/end2end/tests/channelz.cc
@@ -209,27 +209,27 @@ static void test_channelz(grpc_end2end_test_config config) {
grpc_channel_get_channelz_node(f.client);
GPR_ASSERT(channelz_channel != nullptr);
- char* json = channelz_channel->RenderJSON();
+ char* json = channelz_channel->RenderJsonString();
GPR_ASSERT(json != nullptr);
- GPR_ASSERT(nullptr != strstr(json, "\"callsStarted\":\"0\""));
- GPR_ASSERT(nullptr != strstr(json, "\"callsFailed\":\"0\""));
- GPR_ASSERT(nullptr != strstr(json, "\"callsSucceeded\":\"0\""));
+ // nothing is present yet
+ GPR_ASSERT(nullptr == strstr(json, "\"callsStarted\""));
+ GPR_ASSERT(nullptr == strstr(json, "\"callsFailed\""));
+ GPR_ASSERT(nullptr == strstr(json, "\"callsSucceeded\""));
gpr_free(json);
// one successful request
run_one_request(config, f, true);
- json = channelz_channel->RenderJSON();
+ json = channelz_channel->RenderJsonString();
GPR_ASSERT(json != nullptr);
GPR_ASSERT(nullptr != strstr(json, "\"callsStarted\":\"1\""));
- GPR_ASSERT(nullptr != strstr(json, "\"callsFailed\":\"0\""));
GPR_ASSERT(nullptr != strstr(json, "\"callsSucceeded\":\"1\""));
gpr_free(json);
// one failed request
run_one_request(config, f, false);
- json = channelz_channel->RenderJSON();
+ json = channelz_channel->RenderJsonString();
GPR_ASSERT(json != nullptr);
gpr_log(GPR_INFO, "%s", json);
GPR_ASSERT(nullptr != strstr(json, "\"callsStarted\":\"2\""));
@@ -264,7 +264,7 @@ static void test_channelz_with_channel_trace(grpc_end2end_test_config config) {
grpc_channel_get_channelz_node(f.client);
GPR_ASSERT(channelz_channel != nullptr);
- char* json = channelz_channel->RenderJSON();
+ char* json = channelz_channel->RenderJsonString();
GPR_ASSERT(json != nullptr);
gpr_log(GPR_INFO, "%s", json);
GPR_ASSERT(nullptr != strstr(json, "\"trace\""));
diff --git a/test/cpp/util/channel_trace_proto_helper.cc b/test/cpp/util/channel_trace_proto_helper.cc
index ee310784c2..137f278640 100644
--- a/test/cpp/util/channel_trace_proto_helper.cc
+++ b/test/cpp/util/channel_trace_proto_helper.cc
@@ -64,13 +64,17 @@ void VaidateProtoJsonTranslation(char* json_c_str) {
} // namespace
-void ValidateChannelTraceProtoJsonTranslation(char* tracer_json_c_str) {
- VaidateProtoJsonTranslation<grpc::channelz::v1::ChannelTrace>(
- tracer_json_c_str);
+void ValidateChannelTraceProtoJsonTranslation(char* json_c_str) {
+ VaidateProtoJsonTranslation<grpc::channelz::v1::ChannelTrace>(json_c_str);
}
-void ValidateChannelProtoJsonTranslation(char* channel_json_c_str) {
- VaidateProtoJsonTranslation<grpc::channelz::v1::Channel>(channel_json_c_str);
+void ValidateChannelProtoJsonTranslation(char* json_c_str) {
+ VaidateProtoJsonTranslation<grpc::channelz::v1::Channel>(json_c_str);
+}
+
+void ValidateGetTopChannelsResponseProtoJsonTranslation(char* json_c_str) {
+ VaidateProtoJsonTranslation<grpc::channelz::v1::GetTopChannelsResponse>(
+ json_c_str);
}
} // namespace testing
diff --git a/test/cpp/util/channel_trace_proto_helper.h b/test/cpp/util/channel_trace_proto_helper.h
index d1a3603372..74c15f04f1 100644
--- a/test/cpp/util/channel_trace_proto_helper.h
+++ b/test/cpp/util/channel_trace_proto_helper.h
@@ -22,8 +22,9 @@
namespace grpc {
namespace testing {
-void ValidateChannelTraceProtoJsonTranslation(char* tracer_json_c_str);
-void ValidateChannelProtoJsonTranslation(char* channel_json_c_str);
+void ValidateChannelTraceProtoJsonTranslation(char* json_c_str);
+void ValidateChannelProtoJsonTranslation(char* json_c_str);
+void ValidateGetTopChannelsResponseProtoJsonTranslation(char* json_c_str);
} // namespace testing
} // namespace grpc