blob: 990cd3d205e44485247027eb3ab674a66c25cc7a [file] [log] [blame]
ncteisen16b42ca2018-05-11 11:59:38 -04001/*
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
ncteisen9a6c7222018-07-13 12:09:55 -070022#include <grpc/grpc.h>
ncteisen16b42ca2018-05-11 11:59:38 -040023#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"
ncteisen9a6c7222018-07-13 12:09:55 -070029#include "src/core/lib/channel/channelz.h"
ncteisen16b42ca2018-05-11 11:59:38 -040030#include "src/core/lib/channel/channelz_registry.h"
31#include "src/core/lib/gpr/useful.h"
ncteisen146c1252018-05-11 13:38:55 -040032#include "src/core/lib/gprpp/memory.h"
ncteisen16b42ca2018-05-11 11:59:38 -040033#include "src/core/lib/iomgr/exec_ctx.h"
34#include "src/core/lib/json/json.h"
ncteisen9a6c7222018-07-13 12:09:55 -070035#include "src/core/lib/surface/channel.h"
ncteisen16b42ca2018-05-11 11:59:38 -040036
37#include "test/core/util/test_config.h"
ncteisen16b42ca2018-05-11 11:59:38 -040038
39#include <stdlib.h>
40#include <string.h>
41
42namespace grpc_core {
ncteisen9a6c7222018-07-13 12:09:55 -070043namespace channelz {
ncteisen16b42ca2018-05-11 11:59:38 -040044namespace testing {
ncteisen9a6c7222018-07-13 12:09:55 -070045
ncteisen146c1252018-05-11 13:38:55 -040046TEST(ChannelzRegistryTest, UuidStartsAboveZeroTest) {
ncteisen5d373c42018-07-17 11:57:31 -070047 ChannelNode* channelz_channel = nullptr;
ncteisen9a6c7222018-07-13 12:09:55 -070048 intptr_t uuid = ChannelzRegistry::RegisterChannelNode(channelz_channel);
ncteisen3a3bbaf2018-05-17 09:55:24 -070049 EXPECT_GT(uuid, 0) << "First uuid chose must be greater than zero. Zero if "
ncteisen146c1252018-05-11 13:38:55 -040050 "reserved according to "
51 "https://github.com/grpc/proposal/blob/master/"
52 "A14-channelz.md";
ncteisen9a6c7222018-07-13 12:09:55 -070053 ChannelzRegistry::UnregisterChannelNode(uuid);
ncteisen146c1252018-05-11 13:38:55 -040054}
55
56TEST(ChannelzRegistryTest, UuidsAreIncreasing) {
ncteisen5d373c42018-07-17 11:57:31 -070057 ChannelNode* channelz_channel = nullptr;
ncteisen146c1252018-05-11 13:38:55 -040058 std::vector<intptr_t> uuids;
Noah Eisen58e0cbf2018-06-07 22:17:14 -070059 uuids.reserve(10);
ncteisen146c1252018-05-11 13:38:55 -040060 for (int i = 0; i < 10; ++i) {
61 // reregister the same object. It's ok since we are just testing uuids
ncteisen9a6c7222018-07-13 12:09:55 -070062 uuids.push_back(ChannelzRegistry::RegisterChannelNode(channelz_channel));
ncteisen146c1252018-05-11 13:38:55 -040063 }
64 for (size_t i = 1; i < uuids.size(); ++i) {
ncteisen3a3bbaf2018-05-17 09:55:24 -070065 EXPECT_LT(uuids[i - 1], uuids[i]) << "Uuids must always be increasing";
ncteisen146c1252018-05-11 13:38:55 -040066 }
67}
68
69TEST(ChannelzRegistryTest, RegisterGetTest) {
ncteisen5d373c42018-07-17 11:57:31 -070070 ChannelNode* channelz_channel = nullptr;
ncteisen9a6c7222018-07-13 12:09:55 -070071 intptr_t uuid = ChannelzRegistry::RegisterChannelNode(channelz_channel);
72 ChannelNode* retrieved = ChannelzRegistry::GetChannelNode(uuid);
73 EXPECT_EQ(channelz_channel, retrieved);
ncteisen146c1252018-05-11 13:38:55 -040074}
75
ncteisenaca50432018-07-10 18:50:45 -070076TEST(ChannelzRegistryTest, RegisterManyItems) {
ncteisen5d373c42018-07-17 11:57:31 -070077 ChannelNode* channelz_channel = nullptr;
ncteisenaca50432018-07-10 18:50:45 -070078 for (int i = 0; i < 100; i++) {
ncteisen9a6c7222018-07-13 12:09:55 -070079 intptr_t uuid = ChannelzRegistry::RegisterChannelNode(channelz_channel);
80 ChannelNode* retrieved = ChannelzRegistry::GetChannelNode(uuid);
81 EXPECT_EQ(channelz_channel, retrieved);
ncteisenaca50432018-07-10 18:50:45 -070082 }
83}
84
ncteisen146c1252018-05-11 13:38:55 -040085TEST(ChannelzRegistryTest, NullIfNotPresentTest) {
ncteisen5d373c42018-07-17 11:57:31 -070086 ChannelNode* channelz_channel = nullptr;
ncteisen9a6c7222018-07-13 12:09:55 -070087 intptr_t uuid = ChannelzRegistry::RegisterChannelNode(channelz_channel);
ncteisen146c1252018-05-11 13:38:55 -040088 // try to pull out a uuid that does not exist.
ncteisen9a6c7222018-07-13 12:09:55 -070089 ChannelNode* nonexistant = ChannelzRegistry::GetChannelNode(uuid + 1);
ncteisen3a3bbaf2018-05-17 09:55:24 -070090 EXPECT_EQ(nonexistant, nullptr);
ncteisen9a6c7222018-07-13 12:09:55 -070091 ChannelNode* retrieved = ChannelzRegistry::GetChannelNode(uuid);
92 EXPECT_EQ(channelz_channel, retrieved);
ncteisen146c1252018-05-11 13:38:55 -040093}
ncteisen16b42ca2018-05-11 11:59:38 -040094
95} // namespace testing
ncteisen9a6c7222018-07-13 12:09:55 -070096} // namespace channelz
ncteisen16b42ca2018-05-11 11:59:38 -040097} // namespace grpc_core
98
99int main(int argc, char** argv) {
100 grpc_test_init(argc, argv);
101 grpc_init();
102 ::testing::InitGoogleTest(&argc, argv);
103 int ret = RUN_ALL_TESTS();
104 grpc_shutdown();
105 return ret;
106}