ncteisen | 16b42ca | 2018-05-11 11:59:38 -0400 | [diff] [blame] | 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 | |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 22 | #include <grpc/grpc.h> |
ncteisen | 16b42ca | 2018-05-11 11:59:38 -0400 | [diff] [blame] | 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" |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 29 | #include "src/core/lib/channel/channelz.h" |
ncteisen | 16b42ca | 2018-05-11 11:59:38 -0400 | [diff] [blame] | 30 | #include "src/core/lib/channel/channelz_registry.h" |
| 31 | #include "src/core/lib/gpr/useful.h" |
ncteisen | 146c125 | 2018-05-11 13:38:55 -0400 | [diff] [blame] | 32 | #include "src/core/lib/gprpp/memory.h" |
ncteisen | 16b42ca | 2018-05-11 11:59:38 -0400 | [diff] [blame] | 33 | #include "src/core/lib/iomgr/exec_ctx.h" |
| 34 | #include "src/core/lib/json/json.h" |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 35 | #include "src/core/lib/surface/channel.h" |
ncteisen | 16b42ca | 2018-05-11 11:59:38 -0400 | [diff] [blame] | 36 | |
| 37 | #include "test/core/util/test_config.h" |
ncteisen | 16b42ca | 2018-05-11 11:59:38 -0400 | [diff] [blame] | 38 | |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | |
| 42 | namespace grpc_core { |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 43 | namespace channelz { |
ncteisen | 16b42ca | 2018-05-11 11:59:38 -0400 | [diff] [blame] | 44 | namespace testing { |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 45 | |
ncteisen | 146c125 | 2018-05-11 13:38:55 -0400 | [diff] [blame] | 46 | TEST(ChannelzRegistryTest, UuidStartsAboveZeroTest) { |
ncteisen | 5d373c4 | 2018-07-17 11:57:31 -0700 | [diff] [blame] | 47 | ChannelNode* channelz_channel = nullptr; |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 48 | intptr_t uuid = ChannelzRegistry::RegisterChannelNode(channelz_channel); |
ncteisen | 3a3bbaf | 2018-05-17 09:55:24 -0700 | [diff] [blame] | 49 | EXPECT_GT(uuid, 0) << "First uuid chose must be greater than zero. Zero if " |
ncteisen | 146c125 | 2018-05-11 13:38:55 -0400 | [diff] [blame] | 50 | "reserved according to " |
| 51 | "https://github.com/grpc/proposal/blob/master/" |
| 52 | "A14-channelz.md"; |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 53 | ChannelzRegistry::UnregisterChannelNode(uuid); |
ncteisen | 146c125 | 2018-05-11 13:38:55 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | TEST(ChannelzRegistryTest, UuidsAreIncreasing) { |
ncteisen | 5d373c4 | 2018-07-17 11:57:31 -0700 | [diff] [blame] | 57 | ChannelNode* channelz_channel = nullptr; |
ncteisen | 146c125 | 2018-05-11 13:38:55 -0400 | [diff] [blame] | 58 | std::vector<intptr_t> uuids; |
Noah Eisen | 58e0cbf | 2018-06-07 22:17:14 -0700 | [diff] [blame] | 59 | uuids.reserve(10); |
ncteisen | 146c125 | 2018-05-11 13:38:55 -0400 | [diff] [blame] | 60 | for (int i = 0; i < 10; ++i) { |
| 61 | // reregister the same object. It's ok since we are just testing uuids |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 62 | uuids.push_back(ChannelzRegistry::RegisterChannelNode(channelz_channel)); |
ncteisen | 146c125 | 2018-05-11 13:38:55 -0400 | [diff] [blame] | 63 | } |
| 64 | for (size_t i = 1; i < uuids.size(); ++i) { |
ncteisen | 3a3bbaf | 2018-05-17 09:55:24 -0700 | [diff] [blame] | 65 | EXPECT_LT(uuids[i - 1], uuids[i]) << "Uuids must always be increasing"; |
ncteisen | 146c125 | 2018-05-11 13:38:55 -0400 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | TEST(ChannelzRegistryTest, RegisterGetTest) { |
ncteisen | 5d373c4 | 2018-07-17 11:57:31 -0700 | [diff] [blame] | 70 | ChannelNode* channelz_channel = nullptr; |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 71 | intptr_t uuid = ChannelzRegistry::RegisterChannelNode(channelz_channel); |
| 72 | ChannelNode* retrieved = ChannelzRegistry::GetChannelNode(uuid); |
| 73 | EXPECT_EQ(channelz_channel, retrieved); |
ncteisen | 146c125 | 2018-05-11 13:38:55 -0400 | [diff] [blame] | 74 | } |
| 75 | |
ncteisen | aca5043 | 2018-07-10 18:50:45 -0700 | [diff] [blame] | 76 | TEST(ChannelzRegistryTest, RegisterManyItems) { |
ncteisen | 5d373c4 | 2018-07-17 11:57:31 -0700 | [diff] [blame] | 77 | ChannelNode* channelz_channel = nullptr; |
ncteisen | aca5043 | 2018-07-10 18:50:45 -0700 | [diff] [blame] | 78 | for (int i = 0; i < 100; i++) { |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 79 | intptr_t uuid = ChannelzRegistry::RegisterChannelNode(channelz_channel); |
| 80 | ChannelNode* retrieved = ChannelzRegistry::GetChannelNode(uuid); |
| 81 | EXPECT_EQ(channelz_channel, retrieved); |
ncteisen | aca5043 | 2018-07-10 18:50:45 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
ncteisen | 146c125 | 2018-05-11 13:38:55 -0400 | [diff] [blame] | 85 | TEST(ChannelzRegistryTest, NullIfNotPresentTest) { |
ncteisen | 5d373c4 | 2018-07-17 11:57:31 -0700 | [diff] [blame] | 86 | ChannelNode* channelz_channel = nullptr; |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 87 | intptr_t uuid = ChannelzRegistry::RegisterChannelNode(channelz_channel); |
ncteisen | 146c125 | 2018-05-11 13:38:55 -0400 | [diff] [blame] | 88 | // try to pull out a uuid that does not exist. |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 89 | ChannelNode* nonexistant = ChannelzRegistry::GetChannelNode(uuid + 1); |
ncteisen | 3a3bbaf | 2018-05-17 09:55:24 -0700 | [diff] [blame] | 90 | EXPECT_EQ(nonexistant, nullptr); |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 91 | ChannelNode* retrieved = ChannelzRegistry::GetChannelNode(uuid); |
| 92 | EXPECT_EQ(channelz_channel, retrieved); |
ncteisen | 146c125 | 2018-05-11 13:38:55 -0400 | [diff] [blame] | 93 | } |
ncteisen | 16b42ca | 2018-05-11 11:59:38 -0400 | [diff] [blame] | 94 | |
| 95 | } // namespace testing |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 96 | } // namespace channelz |
ncteisen | 16b42ca | 2018-05-11 11:59:38 -0400 | [diff] [blame] | 97 | } // namespace grpc_core |
| 98 | |
| 99 | int 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 | } |