diff options
author | heretic <heretic@yandex-team.ru> | 2022-02-10 16:45:46 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:46 +0300 |
commit | 81eddc8c0b55990194e112b02d127b87d54164a9 (patch) | |
tree | 9142afc54d335ea52910662635b898e79e192e49 /contrib/libs/grpc/test/cpp/util/channelz_sampler_test.cc | |
parent | 397cbe258b9e064f49c4ca575279f02f39fef76e (diff) | |
download | ydb-81eddc8c0b55990194e112b02d127b87d54164a9.tar.gz |
Restoring authorship annotation for <heretic@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/libs/grpc/test/cpp/util/channelz_sampler_test.cc')
-rw-r--r-- | contrib/libs/grpc/test/cpp/util/channelz_sampler_test.cc | 352 |
1 files changed, 176 insertions, 176 deletions
diff --git a/contrib/libs/grpc/test/cpp/util/channelz_sampler_test.cc b/contrib/libs/grpc/test/cpp/util/channelz_sampler_test.cc index 02e44f7931..d81dbb0d05 100644 --- a/contrib/libs/grpc/test/cpp/util/channelz_sampler_test.cc +++ b/contrib/libs/grpc/test/cpp/util/channelz_sampler_test.cc @@ -1,176 +1,176 @@ -/* - * - * Copyright 2016 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#include <stdlib.h> -#include <unistd.h> - -#include <cstdlib> -#include <iostream> -#include <memory> -#include <util/generic/string.h> -#include <thread> - -#include "grpc/grpc.h" -#include "grpc/support/alloc.h" -#include "grpc/support/port_platform.h" -#include "grpcpp/channel.h" -#include "grpcpp/client_context.h" -#include "grpcpp/create_channel.h" -#include "grpcpp/ext/channelz_service_plugin.h" -#include "grpcpp/grpcpp.h" -#include "grpcpp/security/credentials.h" -#include "grpcpp/security/server_credentials.h" -#include "grpcpp/server.h" -#include "grpcpp/server_builder.h" -#include "grpcpp/server_context.h" -#include "gtest/gtest.h" -#include "src/core/lib/gpr/env.h" -#include "src/cpp/server/channelz/channelz_service.h" -#include "src/proto/grpc/testing/test.grpc.pb.h" -#include "test/core/util/test_config.h" -#include "test/cpp/util/subprocess.h" -#include "test/cpp/util/test_credentials_provider.h" - -static TString g_root; - -namespace { -using grpc::ClientContext; -using grpc::Server; -using grpc::ServerBuilder; -using grpc::ServerContext; -using grpc::Status; -} // namespace - -// Test variables -TString server_address("0.0.0.0:10000"); -TString custom_credentials_type("INSECURE_CREDENTIALS"); -TString sampling_times = "2"; -TString sampling_interval_seconds = "3"; -TString output_json("output.json"); - -// Creata an echo server -class EchoServerImpl final : public grpc::testing::TestService::Service { - Status EmptyCall(::grpc::ServerContext* context, - const grpc::testing::Empty* request, - grpc::testing::Empty* response) { - return Status::OK; - } -}; - -// Run client in a thread -void RunClient(const TString& client_id, gpr_event* done_ev) { - grpc::ChannelArguments channel_args; - std::shared_ptr<grpc::ChannelCredentials> channel_creds = - grpc::testing::GetCredentialsProvider()->GetChannelCredentials( - custom_credentials_type, &channel_args); - std::unique_ptr<grpc::testing::TestService::Stub> stub = - grpc::testing::TestService::NewStub( - grpc::CreateChannel(server_address, channel_creds)); - gpr_log(GPR_INFO, "Client %s is echoing!", client_id.c_str()); - while (true) { - if (gpr_event_wait(done_ev, grpc_timeout_seconds_to_deadline(1)) != - nullptr) { - return; - } - grpc::testing::Empty request; - grpc::testing::Empty response; - ClientContext context; - Status status = stub->EmptyCall(&context, request, &response); - if (!status.ok()) { - gpr_log(GPR_ERROR, "Client echo failed."); - GPR_ASSERT(0); - } - } -} - -// Create the channelz to test the connection to the server -bool WaitForConnection(int wait_server_seconds) { - grpc::ChannelArguments channel_args; - std::shared_ptr<grpc::ChannelCredentials> channel_creds = - grpc::testing::GetCredentialsProvider()->GetChannelCredentials( - custom_credentials_type, &channel_args); - auto channel = grpc::CreateChannel(server_address, channel_creds); - return channel->WaitForConnected( - grpc_timeout_seconds_to_deadline(wait_server_seconds)); -} - -// Test the channelz sampler -TEST(ChannelzSamplerTest, SimpleTest) { - // start server - ::grpc::channelz::experimental::InitChannelzService(); - EchoServerImpl service; - grpc::ServerBuilder builder; - auto server_creds = - grpc::testing::GetCredentialsProvider()->GetServerCredentials( - custom_credentials_type); - builder.AddListeningPort(server_address, server_creds); - builder.RegisterService(&service); - std::unique_ptr<Server> server(builder.BuildAndStart()); - gpr_log(GPR_INFO, "Server listening on %s", server_address.c_str()); - const int kWaitForServerSeconds = 10; - ASSERT_TRUE(WaitForConnection(kWaitForServerSeconds)); - // client threads - gpr_event done_ev1, done_ev2; - gpr_event_init(&done_ev1); - gpr_event_init(&done_ev2); - std::thread client_thread_1(RunClient, "1", &done_ev1); - std::thread client_thread_2(RunClient, "2", &done_ev2); - // Run the channelz sampler - grpc::SubProcess* test_driver = new grpc::SubProcess( - {g_root + "/channelz_sampler", "--server_address=" + server_address, - "--custom_credentials_type=" + custom_credentials_type, - "--sampling_times=" + sampling_times, - "--sampling_interval_seconds=" + sampling_interval_seconds, - "--output_json=" + output_json}); - int status = test_driver->Join(); - if (WIFEXITED(status)) { - if (WEXITSTATUS(status)) { - gpr_log(GPR_ERROR, - "Channelz sampler test test-runner exited with code %d", - WEXITSTATUS(status)); - GPR_ASSERT(0); // log the line number of the assertion failure - } - } else if (WIFSIGNALED(status)) { - gpr_log(GPR_ERROR, "Channelz sampler test test-runner ended from signal %d", - WTERMSIG(status)); - GPR_ASSERT(0); - } else { - gpr_log(GPR_ERROR, - "Channelz sampler test test-runner ended with unknown status %d", - status); - GPR_ASSERT(0); - } - delete test_driver; - gpr_event_set(&done_ev1, (void*)1); - gpr_event_set(&done_ev2, (void*)1); - client_thread_1.join(); - client_thread_2.join(); -} - -int main(int argc, char** argv) { - grpc::testing::TestEnvironment env(argc, argv); - ::testing::InitGoogleTest(&argc, argv); - TString me = argv[0]; - auto lslash = me.rfind('/'); - if (lslash != TString::npos) { - g_root = me.substr(0, lslash); - } else { - g_root = "."; - } - int ret = RUN_ALL_TESTS(); - return ret; -} +/* + * + * Copyright 2016 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include <stdlib.h> +#include <unistd.h> + +#include <cstdlib> +#include <iostream> +#include <memory> +#include <util/generic/string.h> +#include <thread> + +#include "grpc/grpc.h" +#include "grpc/support/alloc.h" +#include "grpc/support/port_platform.h" +#include "grpcpp/channel.h" +#include "grpcpp/client_context.h" +#include "grpcpp/create_channel.h" +#include "grpcpp/ext/channelz_service_plugin.h" +#include "grpcpp/grpcpp.h" +#include "grpcpp/security/credentials.h" +#include "grpcpp/security/server_credentials.h" +#include "grpcpp/server.h" +#include "grpcpp/server_builder.h" +#include "grpcpp/server_context.h" +#include "gtest/gtest.h" +#include "src/core/lib/gpr/env.h" +#include "src/cpp/server/channelz/channelz_service.h" +#include "src/proto/grpc/testing/test.grpc.pb.h" +#include "test/core/util/test_config.h" +#include "test/cpp/util/subprocess.h" +#include "test/cpp/util/test_credentials_provider.h" + +static TString g_root; + +namespace { +using grpc::ClientContext; +using grpc::Server; +using grpc::ServerBuilder; +using grpc::ServerContext; +using grpc::Status; +} // namespace + +// Test variables +TString server_address("0.0.0.0:10000"); +TString custom_credentials_type("INSECURE_CREDENTIALS"); +TString sampling_times = "2"; +TString sampling_interval_seconds = "3"; +TString output_json("output.json"); + +// Creata an echo server +class EchoServerImpl final : public grpc::testing::TestService::Service { + Status EmptyCall(::grpc::ServerContext* context, + const grpc::testing::Empty* request, + grpc::testing::Empty* response) { + return Status::OK; + } +}; + +// Run client in a thread +void RunClient(const TString& client_id, gpr_event* done_ev) { + grpc::ChannelArguments channel_args; + std::shared_ptr<grpc::ChannelCredentials> channel_creds = + grpc::testing::GetCredentialsProvider()->GetChannelCredentials( + custom_credentials_type, &channel_args); + std::unique_ptr<grpc::testing::TestService::Stub> stub = + grpc::testing::TestService::NewStub( + grpc::CreateChannel(server_address, channel_creds)); + gpr_log(GPR_INFO, "Client %s is echoing!", client_id.c_str()); + while (true) { + if (gpr_event_wait(done_ev, grpc_timeout_seconds_to_deadline(1)) != + nullptr) { + return; + } + grpc::testing::Empty request; + grpc::testing::Empty response; + ClientContext context; + Status status = stub->EmptyCall(&context, request, &response); + if (!status.ok()) { + gpr_log(GPR_ERROR, "Client echo failed."); + GPR_ASSERT(0); + } + } +} + +// Create the channelz to test the connection to the server +bool WaitForConnection(int wait_server_seconds) { + grpc::ChannelArguments channel_args; + std::shared_ptr<grpc::ChannelCredentials> channel_creds = + grpc::testing::GetCredentialsProvider()->GetChannelCredentials( + custom_credentials_type, &channel_args); + auto channel = grpc::CreateChannel(server_address, channel_creds); + return channel->WaitForConnected( + grpc_timeout_seconds_to_deadline(wait_server_seconds)); +} + +// Test the channelz sampler +TEST(ChannelzSamplerTest, SimpleTest) { + // start server + ::grpc::channelz::experimental::InitChannelzService(); + EchoServerImpl service; + grpc::ServerBuilder builder; + auto server_creds = + grpc::testing::GetCredentialsProvider()->GetServerCredentials( + custom_credentials_type); + builder.AddListeningPort(server_address, server_creds); + builder.RegisterService(&service); + std::unique_ptr<Server> server(builder.BuildAndStart()); + gpr_log(GPR_INFO, "Server listening on %s", server_address.c_str()); + const int kWaitForServerSeconds = 10; + ASSERT_TRUE(WaitForConnection(kWaitForServerSeconds)); + // client threads + gpr_event done_ev1, done_ev2; + gpr_event_init(&done_ev1); + gpr_event_init(&done_ev2); + std::thread client_thread_1(RunClient, "1", &done_ev1); + std::thread client_thread_2(RunClient, "2", &done_ev2); + // Run the channelz sampler + grpc::SubProcess* test_driver = new grpc::SubProcess( + {g_root + "/channelz_sampler", "--server_address=" + server_address, + "--custom_credentials_type=" + custom_credentials_type, + "--sampling_times=" + sampling_times, + "--sampling_interval_seconds=" + sampling_interval_seconds, + "--output_json=" + output_json}); + int status = test_driver->Join(); + if (WIFEXITED(status)) { + if (WEXITSTATUS(status)) { + gpr_log(GPR_ERROR, + "Channelz sampler test test-runner exited with code %d", + WEXITSTATUS(status)); + GPR_ASSERT(0); // log the line number of the assertion failure + } + } else if (WIFSIGNALED(status)) { + gpr_log(GPR_ERROR, "Channelz sampler test test-runner ended from signal %d", + WTERMSIG(status)); + GPR_ASSERT(0); + } else { + gpr_log(GPR_ERROR, + "Channelz sampler test test-runner ended with unknown status %d", + status); + GPR_ASSERT(0); + } + delete test_driver; + gpr_event_set(&done_ev1, (void*)1); + gpr_event_set(&done_ev2, (void*)1); + client_thread_1.join(); + client_thread_2.join(); +} + +int main(int argc, char** argv) { + grpc::testing::TestEnvironment env(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + TString me = argv[0]; + auto lslash = me.rfind('/'); + if (lslash != TString::npos) { + g_root = me.substr(0, lslash); + } else { + g_root = "."; + } + int ret = RUN_ALL_TESTS(); + return ret; +} |