channelz_sampler_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 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  */
19 
20 #include <stdlib.h>
21 #include <unistd.h>
22 
23 #include <cstdlib>
24 #include <iostream>
25 #include <memory>
26 #include <string>
27 #include <thread>
28 
29 #include "gtest/gtest.h"
30 
31 #include <grpc/grpc.h>
32 #include <grpc/support/alloc.h>
33 #include <grpcpp/channel.h>
34 #include <grpcpp/client_context.h>
35 #include <grpcpp/create_channel.h>
37 #include <grpcpp/grpcpp.h>
40 #include <grpcpp/server.h>
41 #include <grpcpp/server_builder.h>
42 #include <grpcpp/server_context.h>
43 
44 #include "src/core/lib/gpr/env.h"
46 #include "src/proto/grpc/testing/test.grpc.pb.h"
50 
52 
53 namespace {
55 using grpc::Server;
58 using grpc::Status;
59 } // namespace
60 
61 // Test variables
62 std::string server_address("0.0.0.0:10000");
63 std::string custom_credentials_type("INSECURE_CREDENTIALS");
66 std::string output_json("output.json");
67 
68 // Creata an echo server
69 class EchoServerImpl final : public grpc::testing::TestService::Service {
71  const grpc::testing::Empty* /*request*/,
72  grpc::testing::Empty* /*response*/) override {
73  return Status::OK;
74  }
75 };
76 
77 // Run client in a thread
78 void RunClient(const std::string& client_id, gpr_event* done_ev) {
79  grpc::ChannelArguments channel_args;
80  std::shared_ptr<grpc::ChannelCredentials> channel_creds =
82  custom_credentials_type, &channel_args);
83  std::unique_ptr<grpc::testing::TestService::Stub> stub =
84  grpc::testing::TestService::NewStub(
85  grpc::CreateChannel(server_address, channel_creds));
86  gpr_log(GPR_INFO, "Client %s is echoing!", client_id.c_str());
87  while (true) {
89  nullptr) {
90  return;
91  }
92  grpc::testing::Empty request;
93  grpc::testing::Empty response;
94  ClientContext context;
95  Status status = stub->EmptyCall(&context, request, &response);
96  if (!status.ok()) {
97  gpr_log(GPR_ERROR, "Client echo failed.");
98  GPR_ASSERT(0);
99  }
100  }
101 }
102 
103 // Create the channelz to test the connection to the server
104 bool WaitForConnection(int wait_server_seconds) {
105  grpc::ChannelArguments channel_args;
106  std::shared_ptr<grpc::ChannelCredentials> channel_creds =
108  custom_credentials_type, &channel_args);
109  auto channel = grpc::CreateChannel(server_address, channel_creds);
110  return channel->WaitForConnected(
111  grpc_timeout_seconds_to_deadline(wait_server_seconds));
112 }
113 
114 // Test the channelz sampler
115 TEST(ChannelzSamplerTest, SimpleTest) {
116  // start server
120  auto server_creds =
123  builder.AddListeningPort(server_address, server_creds);
124  builder.RegisterService(&service);
125  std::unique_ptr<Server> server(builder.BuildAndStart());
126  gpr_log(GPR_INFO, "Server listening on %s", server_address.c_str());
127  const int kWaitForServerSeconds = 10;
128  ASSERT_TRUE(WaitForConnection(kWaitForServerSeconds));
129  // client threads
130  gpr_event done_ev1, done_ev2;
131  gpr_event_init(&done_ev1);
132  gpr_event_init(&done_ev2);
133  std::thread client_thread_1(RunClient, "1", &done_ev1);
134  std::thread client_thread_2(RunClient, "2", &done_ev2);
135  // Run the channelz sampler
136  grpc::SubProcess* test_driver = new grpc::SubProcess(
137  {g_root + "/channelz_sampler", "--server_address=" + server_address,
138  "--custom_credentials_type=" + custom_credentials_type,
139  "--sampling_times=" + sampling_times,
140  "--sampling_interval_seconds=" + sampling_interval_seconds,
141  "--output_json=" + output_json});
142  int status = test_driver->Join();
143  if (WIFEXITED(status)) {
144  if (WEXITSTATUS(status)) {
146  "Channelz sampler test test-runner exited with code %d",
147  WEXITSTATUS(status));
148  GPR_ASSERT(0); // log the line number of the assertion failure
149  }
150  } else if (WIFSIGNALED(status)) {
151  gpr_log(GPR_ERROR, "Channelz sampler test test-runner ended from signal %d",
152  WTERMSIG(status));
153  GPR_ASSERT(0);
154  } else {
156  "Channelz sampler test test-runner ended with unknown status %d",
157  status);
158  GPR_ASSERT(0);
159  }
160  delete test_driver;
161  gpr_event_set(&done_ev1, reinterpret_cast<void*>(1));
162  gpr_event_set(&done_ev2, reinterpret_cast<void*>(1));
163  client_thread_1.join();
164  client_thread_2.join();
165 }
166 
167 int main(int argc, char** argv) {
168  grpc::testing::TestEnvironment env(&argc, argv);
169  ::testing::InitGoogleTest(&argc, argv);
170  std::string me = argv[0];
171  auto lslash = me.rfind('/');
172  if (lslash != std::string::npos) {
173  g_root = me.substr(0, lslash);
174  } else {
175  g_root = ".";
176  }
177  int ret = RUN_ALL_TESTS();
178  return ret;
179 }
test_credentials_provider.h
grpc::SubProcess::Join
int Join()
Definition: test/cpp/util/subprocess.cc:40
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
main
int main(int argc, char **argv)
Definition: channelz_sampler_test.cc:167
grpc_timeout_seconds_to_deadline
gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s)
Definition: test/core/util/test_config.cc:81
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
grpc::testing::CredentialsProvider::GetChannelCredentials
virtual std::shared_ptr< ChannelCredentials > GetChannelCredentials(const std::string &type, ChannelArguments *args)=0
generate.env
env
Definition: generate.py:37
benchmark.request
request
Definition: benchmark.py:77
RunClient
void RunClient(const std::string &client_id, gpr_event *done_ev)
Definition: channelz_sampler_test.cc:78
gpr_event_set
GPRAPI void gpr_event_set(gpr_event *ev, void *value)
Definition: sync.cc:59
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
framework.rpc.grpc_channelz.Server
Server
Definition: grpc_channelz.py:42
status
absl::Status status
Definition: rls.cc:251
OK
@ OK
Definition: cronet_status.h:43
env.h
async_greeter_client.stub
stub
Definition: hellostreamingworld/async_greeter_client.py:26
server_address
std::string server_address("0.0.0.0:10000")
channelz_service_plugin.h
server
std::unique_ptr< Server > server
Definition: channelz_service_test.cc:330
grpc::testing::CredentialsProvider::GetServerCredentials
virtual std::shared_ptr< ServerCredentials > GetServerCredentials(const std::string &type)=0
subprocess.h
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
channelz_service.h
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: grpcpp/server_builder.h:86
EchoServerImpl
Definition: channelz_sampler_test.cc:69
sampling_interval_seconds
std::string sampling_interval_seconds
Definition: channelz_sampler_test.cc:65
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
TEST
TEST(ChannelzSamplerTest, SimpleTest)
Definition: channelz_sampler_test.cc:115
grpc.h
custom_credentials_type
std::string custom_credentials_type("INSECURE_CREDENTIALS")
grpcpp.h
channel.h
grpc::SubProcess
Definition: test/cpp/util/subprocess.h:30
gpr_event_init
GPRAPI void gpr_event_init(gpr_event *ev)
Definition: sync.cc:54
grpc::CreateChannel
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
output_json
std::string output_json("output.json")
gpr_event_wait
GPRAPI void * gpr_event_wait(gpr_event *ev, gpr_timespec abs_deadline)
Definition: sync.cc:73
server_credentials.h
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
test_config.h
grpc::ChannelArguments
Definition: grpcpp/support/channel_arguments.h:39
gpr_event
Definition: impl/codegen/sync_generic.h:31
client_context.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
credentials.h
grpc::channelz::experimental::InitChannelzService
void InitChannelzService()
Definition: channelz_service_plugin.cc:74
EchoServerImpl::EmptyCall
Status EmptyCall(grpc::ServerContext *, const grpc::testing::Empty *, grpc::testing::Empty *) override
Definition: channelz_sampler_test.cc:70
grpc::testing::GetCredentialsProvider
CredentialsProvider * GetCredentialsProvider()
Definition: test_credentials_provider.cc:169
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
alloc.h
server_context.h
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
g_root
static std::string g_root
Definition: channelz_sampler_test.cc:51
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
server.h
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
WaitForConnection
bool WaitForConnection(int wait_server_seconds)
Definition: channelz_sampler_test.cc:104
sampling_times
std::string sampling_times
Definition: channelz_sampler_test.cc:64
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
server_builder.h
create_channel.h
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:53