channelz_registry_test.cc
Go to the documentation of this file.
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 
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include <gtest/gtest.h>
25 
26 #include <grpc/grpc.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
30 
36 #include "src/core/lib/json/json.h"
39 
40 namespace grpc_core {
41 namespace channelz {
42 namespace testing {
43 
45  protected:
46  // ensure we always have a fresh registry for tests.
47  void SetUp() override { ChannelzRegistry::TestOnlyReset(); }
48 };
49 
51  return MakeRefCounted<ListenSocketNode>("test", "test");
52 }
53 
54 TEST_F(ChannelzRegistryTest, UuidStartsAboveZeroTest) {
55  RefCountedPtr<BaseNode> channelz_channel = CreateTestNode();
56  intptr_t uuid = channelz_channel->uuid();
57  EXPECT_GT(uuid, 0) << "First uuid chose must be greater than zero. Zero if "
58  "reserved according to "
59  "https://github.com/grpc/proposal/blob/master/"
60  "A14-channelz.md";
61 }
62 
63 TEST_F(ChannelzRegistryTest, UuidsAreIncreasing) {
64  std::vector<RefCountedPtr<BaseNode>> channelz_channels;
65  channelz_channels.reserve(10);
66  for (int i = 0; i < 10; ++i) {
67  channelz_channels.push_back(CreateTestNode());
68  }
69  for (size_t i = 1; i < channelz_channels.size(); ++i) {
70  EXPECT_LT(channelz_channels[i - 1]->uuid(), channelz_channels[i]->uuid())
71  << "Uuids must always be increasing";
72  }
73 }
74 
75 TEST_F(ChannelzRegistryTest, RegisterGetTest) {
76  RefCountedPtr<BaseNode> channelz_channel = CreateTestNode();
77  RefCountedPtr<BaseNode> retrieved =
78  ChannelzRegistry::Get(channelz_channel->uuid());
79  EXPECT_EQ(channelz_channel, retrieved);
80 }
81 
82 TEST_F(ChannelzRegistryTest, RegisterManyItems) {
83  std::vector<RefCountedPtr<BaseNode>> channelz_channels;
84  for (int i = 0; i < 100; i++) {
85  channelz_channels.push_back(CreateTestNode());
86  RefCountedPtr<BaseNode> retrieved =
87  ChannelzRegistry::Get(channelz_channels[i]->uuid());
88  EXPECT_EQ(channelz_channels[i], retrieved);
89  }
90 }
91 
92 TEST_F(ChannelzRegistryTest, NullIfNotPresentTest) {
93  RefCountedPtr<BaseNode> channelz_channel = CreateTestNode();
94  // try to pull out a uuid that does not exist.
95  RefCountedPtr<BaseNode> nonexistant =
96  ChannelzRegistry::Get(channelz_channel->uuid() + 1);
97  EXPECT_EQ(nonexistant, nullptr);
98  RefCountedPtr<BaseNode> retrieved =
99  ChannelzRegistry::Get(channelz_channel->uuid());
100  EXPECT_EQ(channelz_channel, retrieved);
101 }
102 
103 TEST_F(ChannelzRegistryTest, TestUnregistration) {
104  const int kLoopIterations = 100;
105  // These channels will stay in the registry for the duration of the test.
106  std::vector<RefCountedPtr<BaseNode>> even_channels;
107  even_channels.reserve(kLoopIterations);
108  std::vector<intptr_t> odd_uuids;
109  odd_uuids.reserve(kLoopIterations);
110  {
111  // These channels will unregister themselves at the end of this block.
112  std::vector<RefCountedPtr<BaseNode>> odd_channels;
113  odd_channels.reserve(kLoopIterations);
114  for (int i = 0; i < kLoopIterations; i++) {
115  even_channels.push_back(CreateTestNode());
116  odd_channels.push_back(CreateTestNode());
117  odd_uuids.push_back(odd_channels[i]->uuid());
118  }
119  }
120  // Check that the even channels are present and the odd channels are not.
121  for (int i = 0; i < kLoopIterations; i++) {
122  RefCountedPtr<BaseNode> retrieved =
123  ChannelzRegistry::Get(even_channels[i]->uuid());
124  EXPECT_EQ(even_channels[i], retrieved);
125  retrieved = ChannelzRegistry::Get(odd_uuids[i]);
126  EXPECT_EQ(retrieved, nullptr);
127  }
128  // Add more channels and verify that they get added correctly, to make
129  // sure that the unregistration didn't leave the registry in a weird state.
130  std::vector<RefCountedPtr<BaseNode>> more_channels;
131  more_channels.reserve(kLoopIterations);
132  for (int i = 0; i < kLoopIterations; i++) {
133  more_channels.push_back(CreateTestNode());
134  RefCountedPtr<BaseNode> retrieved =
135  ChannelzRegistry::Get(more_channels[i]->uuid());
136  EXPECT_EQ(more_channels[i], retrieved);
137  }
138 }
139 
140 } // namespace testing
141 } // namespace channelz
142 } // namespace grpc_core
143 
144 int main(int argc, char** argv) {
145  grpc::testing::TestEnvironment env(&argc, argv);
146  ::testing::InitGoogleTest(&argc, argv);
147  int ret = RUN_ALL_TESTS();
148  return ret;
149 }
main
int main(int argc, char **argv)
Definition: channelz_registry_test.cc:144
testing
Definition: aws_request_signer_test.cc:25
log.h
grpc_core::channelz::ChannelzRegistry::Get
static RefCountedPtr< BaseNode > Get(intptr_t uuid)
Definition: channelz_registry.h:43
generate.env
env
Definition: generate.py:37
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::channelz::testing::TEST_F
TEST_F(ChannelzRegistryTest, UuidStartsAboveZeroTest)
Definition: channelz_registry_test.cc:54
string.h
EXPECT_GT
#define EXPECT_GT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2036
useful.h
grpc_core::channelz::testing::CreateTestNode
static RefCountedPtr< BaseNode > CreateTestNode()
Definition: channelz_registry_test.cc:50
grpc_core::channelz::ChannelzRegistry::TestOnlyReset
static void TestOnlyReset()
Definition: channelz_registry.h:64
channelz.h
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
memory.h
string_util.h
channelz_registry.h
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
grpc.h
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
json.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::channelz::testing::ChannelzRegistryTest
Definition: channelz_registry_test.cc:44
test_config.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_core::channelz::testing::ChannelzRegistryTest::SetUp
void SetUp() override
Definition: channelz_registry_test.cc:47
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
alloc.h
EXPECT_LT
#define EXPECT_LT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2032
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
exec_ctx.h
channelz
void channelz(grpc_end2end_test_config config)
Definition: test/core/end2end/tests/channelz.cc:318
channel_trace.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
channel.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:53