24 #include <gtest/gtest.h>
62 std::vector<intptr_t> GetUuidListFromArray(
const Json::Array& arr) {
63 std::vector<intptr_t> uuids;
68 auto it =
object.find(
"ref");
70 if (
it ==
object.
end())
continue;
74 it = ref_object.find(
"channelId");
76 if (
it != ref_object.end()) {
77 uuids.push_back(atoi(
it->second.string_value().c_str()));
83 void ValidateJsonArraySize(
const Json&
array,
size_t expected) {
92 void ValidateJsonEnd(
const Json& json,
bool end) {
93 auto it = json.object_value().find(
"end");
102 void ValidateGetTopChannels(
size_t expected_channels) {
112 ValidateJsonArraySize((*parsed_json.mutable_object())[
"channel"],
114 ValidateJsonEnd(parsed_json,
true);
122 void ValidateGetServers(
size_t expected_servers) {
132 ValidateJsonArraySize((*parsed_json.mutable_object())[
"server"],
134 ValidateJsonEnd(parsed_json,
true);
142 class ChannelFixture {
144 explicit ChannelFixture(
int max_tracer_event_memory = 0) {
148 max_tracer_event_memory),
165 class ServerFixture {
167 explicit ServerFixture(
int max_tracer_event_memory = 0) {
171 max_tracer_event_memory),
187 struct ValidateChannelDataArgs {
195 auto it =
object.find(
key);
203 strtol(
it->second.string_value().c_str(),
nullptr, 0));
208 const ValidateChannelDataArgs&
args) {
216 ValidateChildInteger(
data.object_value(),
"callsStarted",
args.calls_started);
217 ValidateChildInteger(
data.object_value(),
"callsFailed",
args.calls_failed);
218 ValidateChildInteger(
data.object_value(),
"callsSucceeded",
219 args.calls_succeeded);
222 void ValidateChannel(ChannelNode*
channel,
223 const ValidateChannelDataArgs&
args) {
226 ValidateCounters(json_str,
args);
234 void ValidateServer(ServerNode*
server,
const ValidateChannelDataArgs&
args) {
237 ValidateCounters(json_str,
args);
246 CallCountingHelperPeer peer(
channel);
247 return peer.last_call_started_time();
250 void ChannelzSleep(
int64_t sleep_us) {
262 ChannelFixture
channel(GetParam());
265 ValidateChannel(channelz_channel, {0, 0, 0});
288 ChannelFixture
channel(GetParam());
294 ValidateChannel(channelz_channel, {1, 1, 1});
301 ValidateChannel(channelz_channel, {3, 3, 3});
339 ValidateGetTopChannels(1);
344 ValidateGetTopChannels(0);
349 ChannelFixture channels[10];
351 ValidateGetTopChannels(10);
357 ChannelFixture channels[150];
367 ValidateJsonArraySize((*parsed_json.
mutable_object())[
"channel"], 100);
368 ValidateJsonEnd(parsed_json,
false);
377 ValidateJsonArraySize((*parsed_json.
mutable_object())[
"channel"], 50);
378 ValidateJsonEnd(parsed_json,
true);
384 ChannelFixture channels[kNumChannels];
392 ValidateJsonArraySize(
array, kNumChannels);
393 std::vector<intptr_t> uuids = GetUuidListFromArray(
array.array_value());
394 for (
int i = 0;
i < kNumChannels; ++
i) {
403 ChannelFixture channels[kNumChannels];
412 ValidateJsonArraySize(
array, kNumChannels - kMidQuery + 1);
413 std::vector<intptr_t> uuids = GetUuidListFromArray(
array.array_value());
414 for (
size_t i = 0;
i < uuids.size(); ++
i) {
421 ChannelFixture pre_channels[40];
425 ChannelFixture channels[10];
434 ValidateJsonArraySize(
array, 10);
435 std::vector<intptr_t> uuids = GetUuidListFromArray(
array.array_value());
436 for (
size_t i = 0;
i < uuids.size(); ++
i) {
443 ChannelFixture channel_with_uuid1;
444 { ServerFixture channel_with_uuid2; }
445 ChannelFixture channel_with_uuid3;
446 { ServerFixture server_with_uuid4; }
447 ChannelFixture channel_with_uuid5;
455 ValidateJsonArraySize(
array, 2);
456 std::vector<intptr_t> uuids = GetUuidListFromArray(
array.array_value());
465 ValidateJsonArraySize(
array, 1);
466 uuids = GetUuidListFromArray(
array.array_value());
471 const intptr_t kLoopIterations = 50;
473 std::vector<std::unique_ptr<ChannelFixture>> even_channels;
476 std::vector<std::unique_ptr<ChannelFixture>> odd_channels;
477 for (
int i = 0;
i < kLoopIterations;
i++) {
478 odd_channels.push_back(absl::make_unique<ChannelFixture>());
479 even_channels.push_back(absl::make_unique<ChannelFixture>());
488 ValidateJsonArraySize(
array, kLoopIterations);
489 std::vector<intptr_t> uuids = GetUuidListFromArray(
array.array_value());
490 for (
int i = 0;
i < kLoopIterations; ++
i) {
498 ChannelFixture channels[10];
513 ValidateGetTopChannels(10);
517 TEST(ChannelzServerTest, BasicServerAPIFunctionality) {
524 ValidateServer(channelz_server, {1, 1, 1});
531 ValidateServer(channelz_server, {3, 3, 3});
537 ValidateGetServers(1);
542 ValidateGetServers(0);
549 ValidateGetServers(10);
559 int main(
int argc,
char** argv) {