| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2017 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/channelz.h" |
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <memory> |
| 25 |
|
| 26 |
#include "absl/status/status.h" |
| 27 |
#include "absl/status/statusor.h" |
| 28 |
#include "gtest/gtest.h" |
| 29 |
|
| 30 |
#include <grpc/grpc.h> |
| 31 |
#include <grpc/grpc_security.h> |
| 32 |
#include <grpc/support/alloc.h> |
| 33 |
#include <grpc/support/time.h> |
| 34 |
|
| 35 |
#include "src/core/lib/channel/channel_args.h" |
| 36 |
#include "src/core/lib/channel/channelz_registry.h" |
| 37 |
#include "src/core/lib/gpr/useful.h" |
| 38 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 39 |
#include "src/core/lib/json/json.h" |
| 40 |
#include "src/core/lib/surface/channel.h" |
| 41 |
#include "src/core/lib/surface/server.h" |
| 42 |
#include "test/core/util/test_config.h" |
| 43 |
#include "test/cpp/util/channel_trace_proto_helper.h" |
| 44 |
|
| 45 |
namespace grpc_core { |
| 46 |
namespace channelz { |
| 47 |
namespace testing { |
| 48 |
|
| 49 |
// testing peer to access channel internals |
| 50 |
class CallCountingHelperPeer { |
| 51 |
public: |
| 52 |
explicit CallCountingHelperPeer(CallCountingHelper* node) : node_(node) {} |
| 53 |
|
| 54 |
gpr_timespec last_call_started_time() const { |
| 55 |
CallCountingHelper::CounterData data; |
| 56 |
node_->CollectData(&data); |
| 57 |
return gpr_cycle_counter_to_time(data.last_call_started_cycle); |
| 58 |
} |
| 59 |
|
| 60 |
private: |
| 61 |
CallCountingHelper* node_; |
| 62 |
}; |
| 63 |
|
| 64 |
namespace { |
| 65 |
|
| 66 |
std::vector<intptr_t> GetUuidListFromArray(const Json::Array& arr) { |
| 67 |
std::vector<intptr_t> uuids; |
| 68 |
for (const Json& value : arr) { |
| 69 |
EXPECT_EQ(value.type(), Json::Type::OBJECT); |
| 70 |
if (value.type() != Json::Type::OBJECT) continue; |
| 71 |
const Json::Object& object = value.object_value(); |
| 72 |
auto it = object.find("ref"); |
| 73 |
EXPECT_NE(it, object.end()); |
| 74 |
if (it == object.end()) continue; |
| 75 |
EXPECT_EQ(it->second.type(), Json::Type::OBJECT); |
| 76 |
if (it->second.type() != Json::Type::OBJECT) continue; |
| 77 |
const Json::Object& ref_object = it->second.object_value(); |
| 78 |
it = ref_object.find("channelId"); |
| 79 |
EXPECT_NE(it, ref_object.end()); |
| 80 |
if (it != ref_object.end()) { |
| 81 |
uuids.push_back(atoi(it->second.string_value().c_str())); |
| 82 |
} |
| 83 |
} |
| 84 |
return uuids; |
| 85 |
} |
| 86 |
|
| 87 |
void ValidateJsonArraySize(const Json& array, size_t expected) { |
| 88 |
if (expected == 0) { |
| 89 |
ASSERT_EQ(array.type(), Json::Type::JSON_NULL); |
| 90 |
} else { |
| 91 |
ASSERT_EQ(array.type(), Json::Type::ARRAY); |
| 92 |
EXPECT_EQ(array.array_value().size(), expected); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
void ValidateJsonEnd(const Json& json, bool end) { |
| 97 |
auto it = json.object_value().find("end"); |
| 98 |
if (end) { |
| 99 |
ASSERT_NE(it, json.object_value().end()); |
| 100 |
EXPECT_EQ(it->second.type(), Json::Type::JSON_TRUE); |
| 101 |
} else { |
| 102 |
ASSERT_EQ(it, json.object_value().end()); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void ValidateGetTopChannels(size_t expected_channels) { |
| 107 |
std::string json_str = ChannelzRegistry::GetTopChannels(0); |
| 108 |
grpc::testing::ValidateGetTopChannelsResponseProtoJsonTranslation( |
| 109 |
json_str.c_str()); |
| 110 |
auto parsed_json = Json::Parse(json_str); |
| 111 |
ASSERT_TRUE(parsed_json.ok()) << parsed_json.status(); |
| 112 |
ASSERT_EQ(parsed_json->type(), Json::Type::OBJECT); |
| 113 |
// This check will naturally have to change when we support pagination. |
| 114 |
// tracked: https://github.com/grpc/grpc/issues/16019. |
| 115 |
ValidateJsonArraySize((*parsed_json->mutable_object())["channel"], |
| 116 |
expected_channels); |
| 117 |
ValidateJsonEnd(*parsed_json, true); |
| 118 |
// Also check that the core API formats this correctly. |
| 119 |
char* core_api_json_str = grpc_channelz_get_top_channels(0); |
| 120 |
grpc::testing::ValidateGetTopChannelsResponseProtoJsonTranslation( |
| 121 |
core_api_json_str); |
| 122 |
gpr_free(core_api_json_str); |
| 123 |
} |
| 124 |
|
| 125 |
void ValidateGetServers(size_t expected_servers) { |
| 126 |
std::string json_str = ChannelzRegistry::GetServers(0); |
| 127 |
grpc::testing::ValidateGetServersResponseProtoJsonTranslation( |
| 128 |
json_str.c_str()); |
| 129 |
auto parsed_json = Json::Parse(json_str); |
| 130 |
ASSERT_TRUE(parsed_json.ok()) << parsed_json.status(); |
| 131 |
ASSERT_EQ(parsed_json->type(), Json::Type::OBJECT); |
| 132 |
// This check will naturally have to change when we support pagination. |
| 133 |
// tracked: https://github.com/grpc/grpc/issues/16019. |
| 134 |
ValidateJsonArraySize((*parsed_json->mutable_object())["server"], |
| 135 |
expected_servers); |
| 136 |
ValidateJsonEnd(*parsed_json, true); |
| 137 |
// Also check that the core API formats this correctly. |
| 138 |
char* core_api_json_str = grpc_channelz_get_servers(0); |
| 139 |
grpc::testing::ValidateGetServersResponseProtoJsonTranslation( |
| 140 |
core_api_json_str); |
| 141 |
gpr_free(core_api_json_str); |
| 142 |
} |
| 143 |
|
| 144 |
class ChannelFixture { |
| 145 |
public: |
| 146 |
explicit ChannelFixture(int max_tracer_event_memory = 0) { |
| 147 |
grpc_arg client_a[] = { |
| 148 |
grpc_channel_arg_integer_create( |
| 149 |
const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE), |
| 150 |
max_tracer_event_memory), |
| 151 |
grpc_channel_arg_integer_create( |
| 152 |
const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), true)}; |
| 153 |
grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a}; |
| 154 |
grpc_channel_credentials* creds = grpc_insecure_credentials_create(); |
| 155 |
channel_ = grpc_channel_create("fake_target", creds, &client_args); |
| 156 |
grpc_channel_credentials_release(creds); |
| 157 |
} |
| 158 |
|
| 159 |
~ChannelFixture() { grpc_channel_destroy(channel_); } |
| 160 |
|
| 161 |
grpc_channel* channel() { return channel_; } |
| 162 |
|
| 163 |
private: |
| 164 |
grpc_channel* channel_; |
| 165 |
}; |
| 166 |
|
| 167 |
class ServerFixture { |
| 168 |
public: |
| 169 |
explicit ServerFixture(int max_tracer_event_memory = 0) { |
| 170 |
grpc_arg server_a[] = { |
| 171 |
grpc_channel_arg_integer_create( |
| 172 |
const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE), |
| 173 |
max_tracer_event_memory), |
| 174 |
grpc_channel_arg_integer_create( |
| 175 |
const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), true), |
| 176 |
}; |
| 177 |
grpc_channel_args server_args = {GPR_ARRAY_SIZE(server_a), server_a}; |
| 178 |
server_ = grpc_server_create(&server_args, nullptr); |
| 179 |
} |
| 180 |
|
| 181 |
~ServerFixture() { grpc_server_destroy(server_); } |
| 182 |
|
| 183 |
grpc_server* server() const { return server_; } |
| 184 |
|
| 185 |
private: |
| 186 |
grpc_server* server_; |
| 187 |
}; |
| 188 |
|
| 189 |
struct ValidateChannelDataArgs { |
| 190 |
int64_t calls_started; |
| 191 |
int64_t calls_failed; |
| 192 |
int64_t calls_succeeded; |
| 193 |
}; |
| 194 |
|
| 195 |
void ValidateChildInteger(const Json::Object& object, const std::string& key, |
| 196 |
int64_t expected) { |
| 197 |
auto it = object.find(key); |
| 198 |
if (expected == 0) { |
| 199 |
ASSERT_EQ(it, object.end()); |
| 200 |
return; |
| 201 |
} |
| 202 |
ASSERT_NE(it, object.end()); |
| 203 |
ASSERT_EQ(it->second.type(), Json::Type::STRING); |
| 204 |
int64_t gotten_number = static_cast<int64_t>( |
| 205 |
strtol(it->second.string_value().c_str(), nullptr, 0)); |
| 206 |
EXPECT_EQ(gotten_number, expected); |
| 207 |
} |
| 208 |
|
| 209 |
void ValidateCounters(const std::string& json_str, |
| 210 |
const ValidateChannelDataArgs& args) { |
| 211 |
auto json = Json::Parse(json_str); |
| 212 |
ASSERT_TRUE(json.ok()) << json.status(); |
| 213 |
ASSERT_EQ(json->type(), Json::Type::OBJECT); |
| 214 |
Json::Object* object = json->mutable_object(); |
| 215 |
Json& data = (*object)["data"]; |
| 216 |
ASSERT_EQ(data.type(), Json::Type::OBJECT); |
| 217 |
ValidateChildInteger(data.object_value(), "callsStarted", args.calls_started); |
| 218 |
ValidateChildInteger(data.object_value(), "callsFailed", args.calls_failed); |
| 219 |
ValidateChildInteger(data.object_value(), "callsSucceeded", |
| 220 |
args.calls_succeeded); |
| 221 |
} |
| 222 |
|
| 223 |
void ValidateChannel(ChannelNode* channel, |
| 224 |
const ValidateChannelDataArgs& args) { |
| 225 |
std::string json_str = channel->RenderJsonString(); |
| 226 |
grpc::testing::ValidateChannelProtoJsonTranslation(json_str.c_str()); |
| 227 |
ValidateCounters(json_str, args); |
| 228 |
// also check that the core API formats this the correct way |
| 229 |
char* core_api_json_str = grpc_channelz_get_channel(channel->uuid()); |
| 230 |
grpc::testing::ValidateGetChannelResponseProtoJsonTranslation( |
| 231 |
core_api_json_str); |
| 232 |
gpr_free(core_api_json_str); |
| 233 |
} |
| 234 |
|
| 235 |
void ValidateServer(ServerNode* server, const ValidateChannelDataArgs& args) { |
| 236 |
std::string json_str = server->RenderJsonString(); |
| 237 |
grpc::testing::ValidateServerProtoJsonTranslation(json_str.c_str()); |
| 238 |
ValidateCounters(json_str, args); |
| 239 |
// also check that the core API formats this the correct way |
| 240 |
char* core_api_json_str = grpc_channelz_get_server(server->uuid()); |
| 241 |
grpc::testing::ValidateGetServerResponseProtoJsonTranslation( |
| 242 |
core_api_json_str); |
| 243 |
gpr_free(core_api_json_str); |
| 244 |
} |
| 245 |
|
| 246 |
gpr_timespec GetLastCallStartedTime(CallCountingHelper* channel) { |
| 247 |
CallCountingHelperPeer peer(channel); |
| 248 |
return peer.last_call_started_time(); |
| 249 |
} |
| 250 |
|
| 251 |
void ChannelzSleep(int64_t sleep_us) { |
| 252 |
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 253 |
gpr_time_from_micros(sleep_us, GPR_TIMESPAN))); |
| 254 |
ExecCtx::Get()->InvalidateNow(); |
| 255 |
} |
| 256 |
|
| 257 |
} // anonymous namespace |
| 258 |
|
| 259 |
class ChannelzChannelTest : public ::testing::TestWithParam<size_t> {}; |
| 260 |
|
| 261 |
TEST_P(ChannelzChannelTest, BasicChannel) { |
| 262 |
ExecCtx exec_ctx; |
| 263 |
ChannelFixture channel(GetParam()); |
| 264 |
ChannelNode* channelz_channel = |
| 265 |
grpc_channel_get_channelz_node(channel.channel()); |
| 266 |
ValidateChannel(channelz_channel, {0, 0, 0}); |
| 267 |
} |
| 268 |
|
| 269 |
TEST(ChannelzChannelTest, ChannelzDisabled) { |
| 270 |
ExecCtx exec_ctx; |
| 271 |
// explicitly disable channelz |
| 272 |
grpc_arg arg[] = { |
| 273 |
grpc_channel_arg_integer_create( |
| 274 |
const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE), |
| 275 |
0), |
| 276 |
grpc_channel_arg_integer_create( |
| 277 |
const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), false)}; |
| 278 |
grpc_channel_args args = {GPR_ARRAY_SIZE(arg), arg}; |
| 279 |
grpc_channel_credentials* creds = grpc_insecure_credentials_create(); |
| 280 |
grpc_channel* channel = grpc_channel_create("fake_target", creds, &args); |
| 281 |
grpc_channel_credentials_release(creds); |
| 282 |
ChannelNode* channelz_channel = grpc_channel_get_channelz_node(channel); |
| 283 |
ASSERT_EQ(channelz_channel, nullptr); |
| 284 |
grpc_channel_destroy(channel); |
| 285 |
} |
| 286 |
|
| 287 |
TEST_P(ChannelzChannelTest, BasicChannelAPIFunctionality) { |
| 288 |
ExecCtx exec_ctx; |
| 289 |
ChannelFixture channel(GetParam()); |
| 290 |
ChannelNode* channelz_channel = |
| 291 |
grpc_channel_get_channelz_node(channel.channel()); |
| 292 |
channelz_channel->RecordCallStarted(); |
| 293 |
channelz_channel->RecordCallFailed(); |
| 294 |
channelz_channel->RecordCallSucceeded(); |
| 295 |
ValidateChannel(channelz_channel, {1, 1, 1}); |
| 296 |
channelz_channel->RecordCallStarted(); |
| 297 |
channelz_channel->RecordCallFailed(); |
| 298 |
channelz_channel->RecordCallSucceeded(); |
| 299 |
channelz_channel->RecordCallStarted(); |
| 300 |
channelz_channel->RecordCallFailed(); |
| 301 |
channelz_channel->RecordCallSucceeded(); |
| 302 |
ValidateChannel(channelz_channel, {3, 3, 3}); |
| 303 |
} |
| 304 |
|
| 305 |
TEST_P(ChannelzChannelTest, LastCallStartedTime) { |
| 306 |
ExecCtx exec_ctx; |
| 307 |
CallCountingHelper counter; |
| 308 |
// start a call to set the last call started timestamp |
| 309 |
counter.RecordCallStarted(); |
| 310 |
gpr_timespec time1 = GetLastCallStartedTime(&counter); |
| 311 |
// time gone by should not affect the timestamp |
| 312 |
ChannelzSleep(100); |
| 313 |
gpr_timespec time2 = GetLastCallStartedTime(&counter); |
| 314 |
EXPECT_EQ(gpr_time_cmp(time1, time2), 0); |
| 315 |
// calls succeeded or failed should not affect the timestamp |
| 316 |
ChannelzSleep(100); |
| 317 |
counter.RecordCallFailed(); |
| 318 |
counter.RecordCallSucceeded(); |
| 319 |
gpr_timespec time3 = GetLastCallStartedTime(&counter); |
| 320 |
EXPECT_EQ(gpr_time_cmp(time1, time3), 0); |
| 321 |
// another call started should affect the timestamp |
| 322 |
// sleep for extra long to avoid flakes (since we cache Now()) |
| 323 |
ChannelzSleep(5000); |
| 324 |
counter.RecordCallStarted(); |
| 325 |
gpr_timespec time4 = GetLastCallStartedTime(&counter); |
| 326 |
EXPECT_NE(gpr_time_cmp(time1, time4), 0); |
| 327 |
} |
| 328 |
|
| 329 |
class ChannelzRegistryBasedTest : public ::testing::TestWithParam<size_t> { |
| 330 |
protected: |
| 331 |
// ensure we always have a fresh registry for tests. |
| 332 |
void SetUp() override { ChannelzRegistry::TestOnlyReset(); } |
| 333 |
|
| 334 |
void TearDown() override { ChannelzRegistry::TestOnlyReset(); } |
| 335 |
}; |
| 336 |
|
| 337 |
TEST_F(ChannelzRegistryBasedTest, BasicGetTopChannelsTest) { |
| 338 |
ExecCtx exec_ctx; |
| 339 |
ChannelFixture channel; |
| 340 |
ValidateGetTopChannels(1); |
| 341 |
} |
| 342 |
|
| 343 |
TEST_F(ChannelzRegistryBasedTest, NoChannelsTest) { |
| 344 |
ExecCtx exec_ctx; |
| 345 |
ValidateGetTopChannels(0); |
| 346 |
} |
| 347 |
|
| 348 |
TEST_F(ChannelzRegistryBasedTest, ManyChannelsTest) { |
| 349 |
ExecCtx exec_ctx; |
| 350 |
ChannelFixture channels[10]; |
| 351 |
(void)channels; // suppress unused variable error |
| 352 |
ValidateGetTopChannels(10); |
| 353 |
} |
| 354 |
|
| 355 |
TEST_F(ChannelzRegistryBasedTest, GetTopChannelsPagination) { |
| 356 |
ExecCtx exec_ctx; |
| 357 |
// This is over the pagination limit. |
| 358 |
ChannelFixture channels[150]; |
| 359 |
(void)channels; // suppress unused variable error |
| 360 |
std::string json_str = ChannelzRegistry::GetTopChannels(0); |
| 361 |
grpc::testing::ValidateGetTopChannelsResponseProtoJsonTranslation( |
| 362 |
json_str.c_str()); |
| 363 |
auto parsed_json = Json::Parse(json_str); |
| 364 |
ASSERT_TRUE(parsed_json.ok()) << parsed_json.status(); |
| 365 |
ASSERT_EQ(parsed_json->type(), Json::Type::OBJECT); |
| 366 |
// 100 is the pagination limit. |
| 367 |
ValidateJsonArraySize((*parsed_json->mutable_object())["channel"], 100); |
| 368 |
ValidateJsonEnd(*parsed_json, false); |
| 369 |
// Now we get the rest. |
| 370 |
json_str = ChannelzRegistry::GetTopChannels(101); |
| 371 |
grpc::testing::ValidateGetTopChannelsResponseProtoJsonTranslation( |
| 372 |
json_str.c_str()); |
| 373 |
parsed_json = Json::Parse(json_str); |
| 374 |
ASSERT_TRUE(parsed_json.ok()) << parsed_json.status(); |
| 375 |
ASSERT_EQ(parsed_json->type(), Json::Type::OBJECT); |
| 376 |
ValidateJsonArraySize((*parsed_json->mutable_object())["channel"], 50); |
| 377 |
ValidateJsonEnd(*parsed_json, true); |
| 378 |
} |
| 379 |
|
| 380 |
TEST_F(ChannelzRegistryBasedTest, GetTopChannelsUuidCheck) { |
| 381 |
const intptr_t kNumChannels = 50; |
| 382 |
ExecCtx exec_ctx; |
| 383 |
ChannelFixture channels[kNumChannels]; |
| 384 |
(void)channels; // suppress unused variable error |
| 385 |
std::string json_str = ChannelzRegistry::GetTopChannels(0); |
| 386 |
auto parsed_json = Json::Parse(json_str); |
| 387 |
ASSERT_TRUE(parsed_json.ok()) << parsed_json.status(); |
| 388 |
ASSERT_EQ(parsed_json->type(), Json::Type::OBJECT); |
| 389 |
Json& array = (*parsed_json->mutable_object())["channel"]; |
| 390 |
ValidateJsonArraySize(array, kNumChannels); |
| 391 |
std::vector<intptr_t> uuids = GetUuidListFromArray(array.array_value()); |
| 392 |
for (int i = 0; i < kNumChannels; ++i) { |
| 393 |
EXPECT_EQ(i + 1, uuids[i]); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
TEST_F(ChannelzRegistryBasedTest, GetTopChannelsMiddleUuidCheck) { |
| 398 |
const intptr_t kNumChannels = 50; |
| 399 |
const intptr_t kMidQuery = 40; |
| 400 |
ExecCtx exec_ctx; |
| 401 |
ChannelFixture channels[kNumChannels]; |
| 402 |
(void)channels; // suppress unused variable error |
| 403 |
// Only query for the end of the channels. |
| 404 |
std::string json_str = ChannelzRegistry::GetTopChannels(kMidQuery); |
| 405 |
auto parsed_json = Json::Parse(json_str); |
| 406 |
ASSERT_TRUE(parsed_json.ok()) << parsed_json.status(); |
| 407 |
ASSERT_EQ(parsed_json->type(), Json::Type::OBJECT); |
| 408 |
Json& array = (*parsed_json->mutable_object())["channel"]; |
| 409 |
ValidateJsonArraySize(array, kNumChannels - kMidQuery + 1); |
| 410 |
std::vector<intptr_t> uuids = GetUuidListFromArray(array.array_value()); |
| 411 |
for (size_t i = 0; i < uuids.size(); ++i) { |
| 412 |
EXPECT_EQ(static_cast<intptr_t>(kMidQuery + i), uuids[i]); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
TEST_F(ChannelzRegistryBasedTest, GetTopChannelsNoHitUuid) { |
| 417 |
ExecCtx exec_ctx; |
| 418 |
ChannelFixture pre_channels[40]; // will take uuid[1, 40] |
| 419 |
(void)pre_channels; // suppress unused variable error |
| 420 |
ServerFixture servers[10]; // will take uuid[41, 50] |
| 421 |
(void)servers; // suppress unused variable error |
| 422 |
ChannelFixture channels[10]; // will take uuid[51, 60] |
| 423 |
(void)channels; // suppress unused variable error |
| 424 |
// Query in the middle of the server channels. |
| 425 |
std::string json_str = ChannelzRegistry::GetTopChannels(45); |
| 426 |
auto parsed_json = Json::Parse(json_str); |
| 427 |
ASSERT_TRUE(parsed_json.ok()) << parsed_json.status(); |
| 428 |
ASSERT_EQ(parsed_json->type(), Json::Type::OBJECT); |
| 429 |
Json& array = (*parsed_json->mutable_object())["channel"]; |
| 430 |
ValidateJsonArraySize(array, 10); |
| 431 |
std::vector<intptr_t> uuids = GetUuidListFromArray(array.array_value()); |
| 432 |
for (size_t i = 0; i < uuids.size(); ++i) { |
| 433 |
EXPECT_EQ(static_cast<intptr_t>(51 + i), uuids[i]); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
TEST_F(ChannelzRegistryBasedTest, GetTopChannelsMoreGaps) { |
| 438 |
ExecCtx exec_ctx; |
| 439 |
ChannelFixture channel_with_uuid1; |
| 440 |
{ ServerFixture channel_with_uuid2; } |
| 441 |
ChannelFixture channel_with_uuid3; |
| 442 |
{ ServerFixture server_with_uuid4; } |
| 443 |
ChannelFixture channel_with_uuid5; |
| 444 |
// Current state of list: [1, NULL, 3, NULL, 5] |
| 445 |
std::string json_str = ChannelzRegistry::GetTopChannels(2); |
| 446 |
auto parsed_json = Json::Parse(json_str); |
| 447 |
ASSERT_TRUE(parsed_json.ok()) << parsed_json.status(); |
| 448 |
ASSERT_EQ(parsed_json->type(), Json::Type::OBJECT); |
| 449 |
Json array = (*parsed_json->mutable_object())["channel"]; |
| 450 |
ValidateJsonArraySize(array, 2); |
| 451 |
std::vector<intptr_t> uuids = GetUuidListFromArray(array.array_value()); |
| 452 |
EXPECT_EQ(static_cast<intptr_t>(3), uuids[0]); |
| 453 |
EXPECT_EQ(static_cast<intptr_t>(5), uuids[1]); |
| 454 |
json_str = ChannelzRegistry::GetTopChannels(4); |
| 455 |
parsed_json = Json::Parse(json_str); |
| 456 |
ASSERT_TRUE(parsed_json.ok()) << parsed_json.status(); |
| 457 |
ASSERT_EQ(parsed_json->type(), Json::Type::OBJECT); |
| 458 |
array = (*parsed_json->mutable_object())["channel"]; |
| 459 |
ValidateJsonArraySize(array, 1); |
| 460 |
uuids = GetUuidListFromArray(array.array_value()); |
| 461 |
EXPECT_EQ(static_cast<intptr_t>(5), uuids[0]); |
| 462 |
} |
| 463 |
|
| 464 |
TEST_F(ChannelzRegistryBasedTest, GetTopChannelsUuidAfterCompaction) { |
| 465 |
const intptr_t kLoopIterations = 50; |
| 466 |
ExecCtx exec_ctx; |
| 467 |
std::vector<std::unique_ptr<ChannelFixture>> even_channels; |
| 468 |
{ |
| 469 |
// these will delete and unregister themselves after this block. |
| 470 |
std::vector<std::unique_ptr<ChannelFixture>> odd_channels; |
| 471 |
for (int i = 0; i < kLoopIterations; i++) { |
| 472 |
odd_channels.push_back(std::make_unique<ChannelFixture>()); |
| 473 |
even_channels.push_back(std::make_unique<ChannelFixture>()); |
| 474 |
} |
| 475 |
} |
| 476 |
std::string json_str = ChannelzRegistry::GetTopChannels(0); |
| 477 |
auto parsed_json = Json::Parse(json_str); |
| 478 |
ASSERT_TRUE(parsed_json.ok()) << parsed_json.status(); |
| 479 |
ASSERT_EQ(parsed_json->type(), Json::Type::OBJECT); |
| 480 |
Json& array = (*parsed_json->mutable_object())["channel"]; |
| 481 |
ValidateJsonArraySize(array, kLoopIterations); |
| 482 |
std::vector<intptr_t> uuids = GetUuidListFromArray(array.array_value()); |
| 483 |
for (int i = 0; i < kLoopIterations; ++i) { |
| 484 |
// only the even uuids will still be present. |
| 485 |
EXPECT_EQ((i + 1) * 2, uuids[i]); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
TEST_F(ChannelzRegistryBasedTest, InternalChannelTest) { |
| 490 |
ExecCtx exec_ctx; |
| 491 |
ChannelFixture channels[10]; |
| 492 |
(void)channels; // suppress unused variable error |
| 493 |
// create an internal channel |
| 494 |
grpc_arg client_a[] = { |
| 495 |
grpc_channel_arg_integer_create( |
| 496 |
const_cast<char*>(GRPC_ARG_CHANNELZ_IS_INTERNAL_CHANNEL), 1), |
| 497 |
grpc_channel_arg_integer_create( |
| 498 |
const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), true), |
| 499 |
}; |
| 500 |
grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a}; |
| 501 |
grpc_channel_credentials* creds = grpc_insecure_credentials_create(); |
| 502 |
grpc_channel* internal_channel = |
| 503 |
grpc_channel_create("fake_target", creds, &client_args); |
| 504 |
grpc_channel_credentials_release(creds); |
| 505 |
// The internal channel should not be returned from the request |
| 506 |
ValidateGetTopChannels(10); |
| 507 |
grpc_channel_destroy(internal_channel); |
| 508 |
} |
| 509 |
|
| 510 |
TEST(ChannelzServerTest, BasicServerAPIFunctionality) { |
| 511 |
ExecCtx exec_ctx; |
| 512 |
ServerFixture server(10); |
| 513 |
ServerNode* channelz_server = Server::FromC(server.server())->channelz_node(); |
| 514 |
channelz_server->RecordCallStarted(); |
| 515 |
channelz_server->RecordCallFailed(); |
| 516 |
channelz_server->RecordCallSucceeded(); |
| 517 |
ValidateServer(channelz_server, {1, 1, 1}); |
| 518 |
channelz_server->RecordCallStarted(); |
| 519 |
channelz_server->RecordCallFailed(); |
| 520 |
channelz_server->RecordCallSucceeded(); |
| 521 |
channelz_server->RecordCallStarted(); |
| 522 |
channelz_server->RecordCallFailed(); |
| 523 |
channelz_server->RecordCallSucceeded(); |
| 524 |
ValidateServer(channelz_server, {3, 3, 3}); |
| 525 |
} |
| 526 |
|
| 527 |
TEST_F(ChannelzRegistryBasedTest, BasicGetServersTest) { |
| 528 |
ExecCtx exec_ctx; |
| 529 |
ServerFixture server; |
| 530 |
ValidateGetServers(1); |
| 531 |
} |
| 532 |
|
| 533 |
TEST_F(ChannelzRegistryBasedTest, NoServersTest) { |
| 534 |
ExecCtx exec_ctx; |
| 535 |
ValidateGetServers(0); |
| 536 |
} |
| 537 |
|
| 538 |
TEST_F(ChannelzRegistryBasedTest, ManyServersTest) { |
| 539 |
ExecCtx exec_ctx; |
| 540 |
ServerFixture servers[10]; |
| 541 |
(void)servers; // suppress unused variable error |
| 542 |
ValidateGetServers(10); |
| 543 |
} |
| 544 |
|
| 545 |
INSTANTIATE_TEST_SUITE_P(ChannelzChannelTestSweep, ChannelzChannelTest, |
| 546 |
::testing::Values(0, 8, 64, 1024, 1024 * 1024)); |
| 547 |
|
| 548 |
} // namespace testing |
| 549 |
} // namespace channelz |
| 550 |
} // namespace grpc_core |
| 551 |
|
| 552 |
int main(int argc, char** argv) { |
| 553 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 554 |
grpc_init(); |
| 555 |
::testing::InitGoogleTest(&argc, argv); |
| 556 |
int ret = RUN_ALL_TESTS(); |
| 557 |
grpc_shutdown(); |
| 558 |
return ret; |
| 559 |
} |