aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/channel/channelz_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'test/core/channel/channelz_test.cc')
-rw-r--r--test/core/channel/channelz_test.cc173
1 files changed, 146 insertions, 27 deletions
diff --git a/test/core/channel/channelz_test.cc b/test/core/channel/channelz_test.cc
index 5ead12e870..5f27320512 100644
--- a/test/core/channel/channelz_test.cc
+++ b/test/core/channel/channelz_test.cc
@@ -85,6 +85,19 @@ void ValidateJsonArraySize(grpc_json* json, const char* key,
EXPECT_EQ(count, expected_size);
}
+std::vector<intptr_t> GetUuidListFromArray(grpc_json* arr) {
+ EXPECT_EQ(arr->type, GRPC_JSON_ARRAY);
+ std::vector<intptr_t> uuids;
+ for (grpc_json* child = arr->child; child != nullptr; child = child->next) {
+ grpc_json* it = GetJsonChild(child, "ref");
+ EXPECT_NE(it, nullptr);
+ it = GetJsonChild(it, "channelId");
+ EXPECT_NE(it, nullptr);
+ uuids.push_back(atoi(it->value));
+ }
+ return uuids;
+}
+
void ValidateGetTopChannels(size_t expected_channels) {
char* json_str = ChannelzRegistry::GetTopChannels(0);
grpc::testing::ValidateGetTopChannelsResponseProtoJsonTranslation(json_str);
@@ -226,19 +239,7 @@ void ChannelzSleep(int64_t sleep_us) {
} // anonymous namespace
-class ChannelzChannelTest : public ::testing::TestWithParam<size_t> {
- protected:
- // ensure we always have a fresh registry for tests.
- void SetUp() override {
- ChannelzRegistry::Shutdown();
- ChannelzRegistry::Init();
- }
-
- void TearDown() override {
- ChannelzRegistry::Shutdown();
- ChannelzRegistry::Init();
- }
-};
+class ChannelzChannelTest : public ::testing::TestWithParam<size_t> {};
TEST_P(ChannelzChannelTest, BasicChannel) {
grpc_core::ExecCtx exec_ctx;
@@ -307,25 +308,39 @@ TEST_P(ChannelzChannelTest, LastCallStartedMillis) {
EXPECT_NE(millis1, millis4);
}
-TEST(ChannelzGetTopChannelsTest, BasicGetTopChannelsTest) {
+class ChannelzRegistryBasedTest : public ::testing::TestWithParam<size_t> {
+ protected:
+ // ensure we always have a fresh registry for tests.
+ void SetUp() override {
+ ChannelzRegistry::Shutdown();
+ ChannelzRegistry::Init();
+ }
+
+ void TearDown() override {
+ ChannelzRegistry::Shutdown();
+ ChannelzRegistry::Init();
+ }
+};
+
+TEST_F(ChannelzRegistryBasedTest, BasicGetTopChannelsTest) {
grpc_core::ExecCtx exec_ctx;
ChannelFixture channel;
ValidateGetTopChannels(1);
}
-TEST(ChannelzGetTopChannelsTest, NoChannelsTest) {
+TEST_F(ChannelzRegistryBasedTest, NoChannelsTest) {
grpc_core::ExecCtx exec_ctx;
ValidateGetTopChannels(0);
}
-TEST(ChannelzGetTopChannelsTest, ManyChannelsTest) {
+TEST_F(ChannelzRegistryBasedTest, ManyChannelsTest) {
grpc_core::ExecCtx exec_ctx;
ChannelFixture channels[10];
(void)channels; // suppress unused variable error
ValidateGetTopChannels(10);
}
-TEST(ChannelzGetTopChannelsTest, GetTopChannelsPagination) {
+TEST_F(ChannelzRegistryBasedTest, GetTopChannelsPagination) {
grpc_core::ExecCtx exec_ctx;
// this is over the pagination limit.
ChannelFixture channels[150];
@@ -351,7 +366,116 @@ TEST(ChannelzGetTopChannelsTest, GetTopChannelsPagination) {
gpr_free(json_str);
}
-TEST(ChannelzGetTopChannelsTest, InternalChannelTest) {
+TEST_F(ChannelzRegistryBasedTest, GetTopChannelsUuidCheck) {
+ const intptr_t kNumChannels = 50;
+ grpc_core::ExecCtx exec_ctx;
+ ChannelFixture channels[kNumChannels];
+ (void)channels; // suppress unused variable error
+ char* json_str = ChannelzRegistry::GetTopChannels(0);
+ grpc_json* parsed_json = grpc_json_parse_string(json_str);
+ ValidateJsonArraySize(parsed_json, "channel", kNumChannels);
+ grpc_json* json_channels = GetJsonChild(parsed_json, "channel");
+ std::vector<intptr_t> uuids = GetUuidListFromArray(json_channels);
+ for (int i = 0; i < kNumChannels; ++i) {
+ EXPECT_EQ(i + 1, uuids[i]);
+ }
+ grpc_json_destroy(parsed_json);
+ gpr_free(json_str);
+}
+
+TEST_F(ChannelzRegistryBasedTest, GetTopChannelsMiddleUuidCheck) {
+ const intptr_t kNumChannels = 50;
+ const intptr_t kMidQuery = 40;
+ grpc_core::ExecCtx exec_ctx;
+ ChannelFixture channels[kNumChannels];
+ (void)channels; // suppress unused variable error
+ // only query for the end of the channels
+ char* json_str = ChannelzRegistry::GetTopChannels(kMidQuery);
+ grpc_json* parsed_json = grpc_json_parse_string(json_str);
+ ValidateJsonArraySize(parsed_json, "channel", kNumChannels - kMidQuery + 1);
+ grpc_json* json_channels = GetJsonChild(parsed_json, "channel");
+ std::vector<intptr_t> uuids = GetUuidListFromArray(json_channels);
+ for (size_t i = 0; i < uuids.size(); ++i) {
+ EXPECT_EQ(static_cast<intptr_t>(kMidQuery + i), uuids[i]);
+ }
+ grpc_json_destroy(parsed_json);
+ gpr_free(json_str);
+}
+
+TEST_F(ChannelzRegistryBasedTest, GetTopChannelsNoHitUuid) {
+ grpc_core::ExecCtx exec_ctx;
+ ChannelFixture pre_channels[40]; // will take uuid[1, 40]
+ (void)pre_channels; // suppress unused variable error
+ ServerFixture servers[10]; // will take uuid[41, 50]
+ (void)servers; // suppress unused variable error
+ ChannelFixture channels[10]; // will take uuid[51, 60]
+ (void)channels; // suppress unused variable error
+ // query in the middle of the server channels
+ char* json_str = ChannelzRegistry::GetTopChannels(45);
+ grpc_json* parsed_json = grpc_json_parse_string(json_str);
+ ValidateJsonArraySize(parsed_json, "channel", 10);
+ grpc_json* json_channels = GetJsonChild(parsed_json, "channel");
+ std::vector<intptr_t> uuids = GetUuidListFromArray(json_channels);
+ for (size_t i = 0; i < uuids.size(); ++i) {
+ EXPECT_EQ(static_cast<intptr_t>(51 + i), uuids[i]);
+ }
+ grpc_json_destroy(parsed_json);
+ gpr_free(json_str);
+}
+
+TEST_F(ChannelzRegistryBasedTest, GetTopChannelsMoreGaps) {
+ grpc_core::ExecCtx exec_ctx;
+ ChannelFixture channel_with_uuid1;
+ { ServerFixture channel_with_uuid2; }
+ ChannelFixture channel_with_uuid3;
+ { ServerFixture server_with_uuid4; }
+ ChannelFixture channel_with_uuid5;
+ // Current state of list: [1, NULL, 3, NULL, 5]
+ char* json_str = ChannelzRegistry::GetTopChannels(2);
+ grpc_json* parsed_json = grpc_json_parse_string(json_str);
+ ValidateJsonArraySize(parsed_json, "channel", 2);
+ grpc_json* json_channels = GetJsonChild(parsed_json, "channel");
+ std::vector<intptr_t> uuids = GetUuidListFromArray(json_channels);
+ EXPECT_EQ(static_cast<intptr_t>(3), uuids[0]);
+ EXPECT_EQ(static_cast<intptr_t>(5), uuids[1]);
+ grpc_json_destroy(parsed_json);
+ gpr_free(json_str);
+ json_str = ChannelzRegistry::GetTopChannels(4);
+ parsed_json = grpc_json_parse_string(json_str);
+ ValidateJsonArraySize(parsed_json, "channel", 1);
+ json_channels = GetJsonChild(parsed_json, "channel");
+ uuids = GetUuidListFromArray(json_channels);
+ EXPECT_EQ(static_cast<intptr_t>(5), uuids[0]);
+ grpc_json_destroy(parsed_json);
+ gpr_free(json_str);
+}
+
+TEST_F(ChannelzRegistryBasedTest, GetTopChannelsUuidAfterCompaction) {
+ const intptr_t kLoopIterations = 50;
+ grpc_core::ExecCtx exec_ctx;
+ std::vector<UniquePtr<ChannelFixture>> even_channels;
+ {
+ // these will delete and unregister themselves after this block.
+ std::vector<UniquePtr<ChannelFixture>> odd_channels;
+ for (int i = 0; i < kLoopIterations; i++) {
+ odd_channels.push_back(MakeUnique<ChannelFixture>());
+ even_channels.push_back(MakeUnique<ChannelFixture>());
+ }
+ }
+ char* json_str = ChannelzRegistry::GetTopChannels(0);
+ grpc_json* parsed_json = grpc_json_parse_string(json_str);
+ ValidateJsonArraySize(parsed_json, "channel", kLoopIterations);
+ grpc_json* json_channels = GetJsonChild(parsed_json, "channel");
+ std::vector<intptr_t> uuids = GetUuidListFromArray(json_channels);
+ for (int i = 0; i < kLoopIterations; ++i) {
+ // only the even uuids will still be present.
+ EXPECT_EQ((i + 1) * 2, uuids[i]);
+ }
+ grpc_json_destroy(parsed_json);
+ gpr_free(json_str);
+}
+
+TEST_F(ChannelzRegistryBasedTest, InternalChannelTest) {
grpc_core::ExecCtx exec_ctx;
ChannelFixture channels[10];
(void)channels; // suppress unused variable error
@@ -369,9 +493,7 @@ TEST(ChannelzGetTopChannelsTest, InternalChannelTest) {
grpc_channel_destroy(internal_channel);
}
-class ChannelzServerTest : public ::testing::TestWithParam<size_t> {};
-
-TEST_P(ChannelzServerTest, BasicServerAPIFunctionality) {
+TEST(ChannelzServerTest, BasicServerAPIFunctionality) {
grpc_core::ExecCtx exec_ctx;
ServerFixture server(10);
ServerNode* channelz_server = grpc_server_get_channelz_node(server.server());
@@ -388,18 +510,18 @@ TEST_P(ChannelzServerTest, BasicServerAPIFunctionality) {
ValidateServer(channelz_server, {3, 3, 3});
}
-TEST(ChannelzGetServersTest, BasicGetServersTest) {
+TEST_F(ChannelzRegistryBasedTest, BasicGetServersTest) {
grpc_core::ExecCtx exec_ctx;
ServerFixture server;
ValidateGetServers(1);
}
-TEST(ChannelzGetServersTest, NoServersTest) {
+TEST_F(ChannelzRegistryBasedTest, NoServersTest) {
grpc_core::ExecCtx exec_ctx;
ValidateGetServers(0);
}
-TEST(ChannelzGetServersTest, ManyServersTest) {
+TEST_F(ChannelzRegistryBasedTest, ManyServersTest) {
grpc_core::ExecCtx exec_ctx;
ServerFixture servers[10];
(void)servers; // suppress unused variable error
@@ -409,9 +531,6 @@ TEST(ChannelzGetServersTest, ManyServersTest) {
INSTANTIATE_TEST_CASE_P(ChannelzChannelTestSweep, ChannelzChannelTest,
::testing::Values(0, 8, 64, 1024, 1024 * 1024));
-INSTANTIATE_TEST_CASE_P(ChannelzServerTestSweep, ChannelzServerTest,
- ::testing::Values(0, 8, 64, 1024, 1024 * 1024));
-
} // namespace testing
} // namespace channelz
} // namespace grpc_core