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