• 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 <stdlib.h>
20 #include <string.h>
21 
22 #include <grpc/grpc.h>
23 #include <gtest/gtest.h>
24 
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 
28 #include "src/core/lib/channel/channel_trace.h"
29 #include "src/core/lib/channel/channelz.h"
30 #include "src/core/lib/channel/channelz_registry.h"
31 #include "src/core/lib/gpr/useful.h"
32 #include "src/core/lib/gprpp/memory.h"
33 #include "src/core/lib/iomgr/exec_ctx.h"
34 #include "src/core/lib/json/json.h"
35 #include "src/core/lib/surface/channel.h"
36 
37 #include "test/core/util/test_config.h"
38 
39 #include <stdlib.h>
40 #include <string.h>
41 
42 namespace grpc_core {
43 namespace channelz {
44 namespace testing {
45 
TEST(ChannelzRegistryTest,UuidStartsAboveZeroTest)46 TEST(ChannelzRegistryTest, UuidStartsAboveZeroTest) {
47   BaseNode* channelz_channel = nullptr;
48   intptr_t uuid = ChannelzRegistry::Register(channelz_channel);
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   ChannelzRegistry::Unregister(uuid);
54 }
55 
TEST(ChannelzRegistryTest,UuidsAreIncreasing)56 TEST(ChannelzRegistryTest, UuidsAreIncreasing) {
57   BaseNode* channelz_channel = nullptr;
58   std::vector<intptr_t> uuids;
59   uuids.reserve(10);
60   for (int i = 0; i < 10; ++i) {
61     // reregister the same object. It's ok since we are just testing uuids
62     uuids.push_back(ChannelzRegistry::Register(channelz_channel));
63   }
64   for (size_t i = 1; i < uuids.size(); ++i) {
65     EXPECT_LT(uuids[i - 1], uuids[i]) << "Uuids must always be increasing";
66   }
67 }
68 
TEST(ChannelzRegistryTest,RegisterGetTest)69 TEST(ChannelzRegistryTest, RegisterGetTest) {
70   // we hackily jam an intptr_t into this pointer to check for equality later
71   BaseNode* channelz_channel = (BaseNode*)42;
72   intptr_t uuid = ChannelzRegistry::Register(channelz_channel);
73   BaseNode* retrieved = ChannelzRegistry::Get(uuid);
74   EXPECT_EQ(channelz_channel, retrieved);
75 }
76 
TEST(ChannelzRegistryTest,RegisterManyItems)77 TEST(ChannelzRegistryTest, RegisterManyItems) {
78   // we hackily jam an intptr_t into this pointer to check for equality later
79   BaseNode* channelz_channel = (BaseNode*)42;
80   for (int i = 0; i < 100; i++) {
81     intptr_t uuid = ChannelzRegistry::Register(channelz_channel);
82     BaseNode* retrieved = ChannelzRegistry::Get(uuid);
83     EXPECT_EQ(channelz_channel, retrieved);
84   }
85 }
86 
TEST(ChannelzRegistryTest,NullIfNotPresentTest)87 TEST(ChannelzRegistryTest, NullIfNotPresentTest) {
88   // we hackily jam an intptr_t into this pointer to check for equality later
89   BaseNode* channelz_channel = (BaseNode*)42;
90   intptr_t uuid = ChannelzRegistry::Register(channelz_channel);
91   // try to pull out a uuid that does not exist.
92   BaseNode* nonexistant = ChannelzRegistry::Get(uuid + 1);
93   EXPECT_EQ(nonexistant, nullptr);
94   BaseNode* retrieved = ChannelzRegistry::Get(uuid);
95   EXPECT_EQ(channelz_channel, retrieved);
96 }
97 
98 }  // namespace testing
99 }  // namespace channelz
100 }  // namespace grpc_core
101 
main(int argc,char ** argv)102 int main(int argc, char** argv) {
103   grpc_test_init(argc, argv);
104   grpc_init();
105   ::testing::InitGoogleTest(&argc, argv);
106   int ret = RUN_ALL_TESTS();
107   grpc_shutdown();
108   return ret;
109 }
110