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