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