channelz_registry_test.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include "src/core/lib/channel/channelz_registry.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <gtest/gtest.h>
  22. #include <grpc/grpc.h>
  23. #include <grpc/support/alloc.h>
  24. #include <grpc/support/log.h>
  25. #include <grpc/support/string_util.h>
  26. #include "src/core/lib/channel/channel_trace.h"
  27. #include "src/core/lib/channel/channelz.h"
  28. #include "src/core/lib/gpr/useful.h"
  29. #include "src/core/lib/gprpp/memory.h"
  30. #include "src/core/lib/iomgr/exec_ctx.h"
  31. #include "src/core/lib/json/json.h"
  32. #include "src/core/lib/surface/channel.h"
  33. #include "test/core/util/test_config.h"
  34. namespace grpc_core {
  35. namespace channelz {
  36. namespace testing {
  37. class ChannelzRegistryTest : public ::testing::Test {
  38. protected:
  39. // ensure we always have a fresh registry for tests.
  40. void SetUp() override { ChannelzRegistry::Init(); }
  41. void TearDown() override { ChannelzRegistry::Shutdown(); }
  42. };
  43. static RefCountedPtr<BaseNode> CreateTestNode() {
  44. return MakeRefCounted<ListenSocketNode>("test", "test");
  45. }
  46. TEST_F(ChannelzRegistryTest, UuidStartsAboveZeroTest) {
  47. RefCountedPtr<BaseNode> channelz_channel = CreateTestNode();
  48. intptr_t uuid = channelz_channel->uuid();
  49. EXPECT_GT(uuid, 0) << "First uuid chose must be greater than zero. Zero if "
  50. "reserved according to "
  51. "https://github.com/grpc/proposal/blob/master/"
  52. "A14-channelz.md";
  53. }
  54. TEST_F(ChannelzRegistryTest, UuidsAreIncreasing) {
  55. std::vector<RefCountedPtr<BaseNode>> channelz_channels;
  56. channelz_channels.reserve(10);
  57. for (int i = 0; i < 10; ++i) {
  58. channelz_channels.push_back(CreateTestNode());
  59. }
  60. for (size_t i = 1; i < channelz_channels.size(); ++i) {
  61. EXPECT_LT(channelz_channels[i - 1]->uuid(), channelz_channels[i]->uuid())
  62. << "Uuids must always be increasing";
  63. }
  64. }
  65. TEST_F(ChannelzRegistryTest, RegisterGetTest) {
  66. RefCountedPtr<BaseNode> channelz_channel = CreateTestNode();
  67. RefCountedPtr<BaseNode> retrieved =
  68. ChannelzRegistry::Get(channelz_channel->uuid());
  69. EXPECT_EQ(channelz_channel, retrieved);
  70. }
  71. TEST_F(ChannelzRegistryTest, RegisterManyItems) {
  72. std::vector<RefCountedPtr<BaseNode>> channelz_channels;
  73. for (int i = 0; i < 100; i++) {
  74. channelz_channels.push_back(CreateTestNode());
  75. RefCountedPtr<BaseNode> retrieved =
  76. ChannelzRegistry::Get(channelz_channels[i]->uuid());
  77. EXPECT_EQ(channelz_channels[i], retrieved);
  78. }
  79. }
  80. TEST_F(ChannelzRegistryTest, NullIfNotPresentTest) {
  81. RefCountedPtr<BaseNode> channelz_channel = CreateTestNode();
  82. // try to pull out a uuid that does not exist.
  83. RefCountedPtr<BaseNode> nonexistant =
  84. ChannelzRegistry::Get(channelz_channel->uuid() + 1);
  85. EXPECT_EQ(nonexistant, nullptr);
  86. RefCountedPtr<BaseNode> retrieved =
  87. ChannelzRegistry::Get(channelz_channel->uuid());
  88. EXPECT_EQ(channelz_channel, retrieved);
  89. }
  90. TEST_F(ChannelzRegistryTest, TestUnregistration) {
  91. const int kLoopIterations = 100;
  92. // These channels will stay in the registry for the duration of the test.
  93. std::vector<RefCountedPtr<BaseNode>> even_channels;
  94. even_channels.reserve(kLoopIterations);
  95. std::vector<intptr_t> odd_uuids;
  96. odd_uuids.reserve(kLoopIterations);
  97. {
  98. // These channels will unregister themselves at the end of this block.
  99. std::vector<RefCountedPtr<BaseNode>> odd_channels;
  100. odd_channels.reserve(kLoopIterations);
  101. for (int i = 0; i < kLoopIterations; i++) {
  102. even_channels.push_back(CreateTestNode());
  103. odd_channels.push_back(CreateTestNode());
  104. odd_uuids.push_back(odd_channels[i]->uuid());
  105. }
  106. }
  107. // Check that the even channels are present and the odd channels are not.
  108. for (int i = 0; i < kLoopIterations; i++) {
  109. RefCountedPtr<BaseNode> retrieved =
  110. ChannelzRegistry::Get(even_channels[i]->uuid());
  111. EXPECT_EQ(even_channels[i], retrieved);
  112. retrieved = ChannelzRegistry::Get(odd_uuids[i]);
  113. EXPECT_EQ(retrieved, nullptr);
  114. }
  115. // Add more channels and verify that they get added correctly, to make
  116. // sure that the unregistration didn't leave the registry in a weird state.
  117. std::vector<RefCountedPtr<BaseNode>> more_channels;
  118. more_channels.reserve(kLoopIterations);
  119. for (int i = 0; i < kLoopIterations; i++) {
  120. more_channels.push_back(CreateTestNode());
  121. RefCountedPtr<BaseNode> retrieved =
  122. ChannelzRegistry::Get(more_channels[i]->uuid());
  123. EXPECT_EQ(more_channels[i], retrieved);
  124. }
  125. }
  126. } // namespace testing
  127. } // namespace channelz
  128. } // namespace grpc_core
  129. int main(int argc, char** argv) {
  130. grpc::testing::TestEnvironment env(argc, argv);
  131. ::testing::InitGoogleTest(&argc, argv);
  132. int ret = RUN_ALL_TESTS();
  133. return ret;
  134. }