diff options
author | 2015-08-09 09:21:01 -0700 | |
---|---|---|
committer | 2015-08-09 09:21:01 -0700 | |
commit | 49a513031880a6303273f3fe3cec837225a5ad78 (patch) | |
tree | 0140ee377eb87732cdb6072576751d98f8c9d8cc /test/cpp/interop | |
parent | 7c0d914cce379f14a1adfae9374641967c45d7b2 (diff) | |
parent | 10494fcb61d638682fb8e5d28356a1f5125e8d0a (diff) |
Merge branch 'compression-accept-encoding' into compression-interop
Diffstat (limited to 'test/cpp/interop')
-rw-r--r-- | test/cpp/interop/client.cc | 6 | ||||
-rw-r--r-- | test/cpp/interop/client_helper.cc | 3 | ||||
-rw-r--r-- | test/cpp/interop/interop_client.cc | 19 | ||||
-rw-r--r-- | test/cpp/interop/interop_client.h | 1 | ||||
-rw-r--r-- | test/cpp/interop/reconnect_interop_client.cc | 103 | ||||
-rw-r--r-- | test/cpp/interop/reconnect_interop_server.cc | 190 | ||||
-rw-r--r-- | test/cpp/interop/server.cc | 7 |
7 files changed, 326 insertions, 3 deletions
diff --git a/test/cpp/interop/client.cc b/test/cpp/interop/client.cc index d0393fafb2..ebc5cfc85a 100644 --- a/test/cpp/interop/client.cc +++ b/test/cpp/interop/client.cc @@ -70,6 +70,7 @@ DEFINE_string(test_case, "large_unary", "jwt_token_creds: large_unary with JWT token auth; " "oauth2_auth_token: raw oauth2 access token auth; " "per_rpc_creds: raw oauth2 access token on a single rpc; " + "status_code_and_message: verify status code & message; " "all : all of above."); DEFINE_string(default_service_account, "", "Email of GCE default service account"); @@ -82,7 +83,7 @@ using grpc::testing::GetServiceAccountJsonKey; int main(int argc, char** argv) { grpc::testing::InitTest(&argc, &argv, true); - + gpr_log(GPR_INFO, "Testing these cases: %s", FLAGS_test_case.c_str()); int ret = 0; grpc::testing::InteropClient client( CreateChannelForTestCase(FLAGS_test_case)); @@ -121,6 +122,8 @@ int main(int argc, char** argv) { } else if (FLAGS_test_case == "per_rpc_creds") { grpc::string json_key = GetServiceAccountJsonKey(); client.DoPerRpcCreds(json_key, FLAGS_oauth_scope); + } else if (FLAGS_test_case == "status_code_and_message") { + client.DoStatusWithMessage(); } else if (FLAGS_test_case == "all") { client.DoEmpty(); client.DoLargeUnary(); @@ -131,6 +134,7 @@ int main(int argc, char** argv) { client.DoCancelAfterBegin(); client.DoCancelAfterFirstResponse(); client.DoTimeoutOnSleepingServer(); + client.DoStatusWithMessage(); // service_account_creds and jwt_token_creds can only run with ssl. if (FLAGS_enable_ssl) { grpc::string json_key = GetServiceAccountJsonKey(); diff --git a/test/cpp/interop/client_helper.cc b/test/cpp/interop/client_helper.cc index cdc04b0bce..65fdc63b43 100644 --- a/test/cpp/interop/client_helper.cc +++ b/test/cpp/interop/client_helper.cc @@ -128,7 +128,8 @@ std::shared_ptr<ChannelInterface> CreateChannelForTestCase( GPR_ASSERT(FLAGS_enable_ssl); grpc::string json_key = GetServiceAccountJsonKey(); std::chrono::seconds token_lifetime = std::chrono::hours(1); - creds = JWTCredentials(json_key, token_lifetime.count()); + creds = + ServiceAccountJWTAccessCredentials(json_key, token_lifetime.count()); return CreateTestChannel(host_port, FLAGS_server_host_override, FLAGS_enable_ssl, FLAGS_use_prod_roots, creds); } else if (test_case == "oauth2_auth_token") { diff --git a/test/cpp/interop/interop_client.cc b/test/cpp/interop/interop_client.cc index 5af4824833..8e2d778cff 100644 --- a/test/cpp/interop/interop_client.cc +++ b/test/cpp/interop/interop_client.cc @@ -512,5 +512,24 @@ void InteropClient::DoTimeoutOnSleepingServer() { gpr_log(GPR_INFO, "Pingpong streaming timeout done."); } +void InteropClient::DoStatusWithMessage() { + gpr_log(GPR_INFO, "Sending RPC with a request for status code 2 and message"); + std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_)); + + ClientContext context; + SimpleRequest request; + SimpleResponse response; + EchoStatus *requested_status = request.mutable_response_status(); + requested_status->set_code(grpc::StatusCode::UNKNOWN); + grpc::string test_msg = "This is a test message"; + requested_status->set_message(test_msg); + + Status s = stub->UnaryCall(&context, request, &response); + + GPR_ASSERT(s.error_code() == grpc::StatusCode::UNKNOWN); + GPR_ASSERT(s.error_message() == test_msg); + gpr_log(GPR_INFO, "Done testing Status and Message"); +} + } // namespace testing } // namespace grpc diff --git a/test/cpp/interop/interop_client.h b/test/cpp/interop/interop_client.h index bf8188325e..6e26c49e5d 100644 --- a/test/cpp/interop/interop_client.h +++ b/test/cpp/interop/interop_client.h @@ -60,6 +60,7 @@ class InteropClient { void DoCancelAfterBegin(); void DoCancelAfterFirstResponse(); void DoTimeoutOnSleepingServer(); + void DoStatusWithMessage(); // Auth tests. // username is a string containing the user email void DoJwtTokenCreds(const grpc::string& username); diff --git a/test/cpp/interop/reconnect_interop_client.cc b/test/cpp/interop/reconnect_interop_client.cc new file mode 100644 index 0000000000..65f098050e --- /dev/null +++ b/test/cpp/interop/reconnect_interop_client.cc @@ -0,0 +1,103 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include <memory> +#include <sstream> + +#include <grpc/grpc.h> +#include <grpc/support/log.h> +#include <gflags/gflags.h> +#include <grpc++/channel_interface.h> +#include <grpc++/client_context.h> +#include <grpc++/status.h> +#include "test/cpp/util/create_test_channel.h" +#include "test/cpp/util/test_config.h" +#include "test/proto/test.grpc.pb.h" +#include "test/proto/empty.grpc.pb.h" +#include "test/proto/messages.grpc.pb.h" + +DEFINE_int32(server_control_port, 0, "Server port for control rpcs."); +DEFINE_int32(server_retry_port, 0, "Server port for testing reconnection."); +DEFINE_string(server_host, "127.0.0.1", "Server host to connect to"); + +using grpc::ChannelInterface; +using grpc::ClientContext; +using grpc::CreateTestChannel; +using grpc::Status; +using grpc::testing::Empty; +using grpc::testing::ReconnectInfo; +using grpc::testing::ReconnectService; + +int main(int argc, char** argv) { + grpc::testing::InitTest(&argc, &argv, true); + GPR_ASSERT(FLAGS_server_control_port); + GPR_ASSERT(FLAGS_server_retry_port); + + std::ostringstream server_address; + server_address << FLAGS_server_host << ':' << FLAGS_server_control_port; + std::unique_ptr<ReconnectService::Stub> control_stub( + ReconnectService::NewStub( + CreateTestChannel(server_address.str(), false))); + ClientContext start_context; + Empty empty_request; + Empty empty_response; + Status start_status = + control_stub->Start(&start_context, empty_request, &empty_response); + GPR_ASSERT(start_status.ok()); + + gpr_log(GPR_INFO, "Starting connections with retries."); + server_address.str(""); + server_address << FLAGS_server_host << ':' << FLAGS_server_retry_port; + std::shared_ptr<ChannelInterface> retry_channel = + CreateTestChannel(server_address.str(), true); + // About 13 retries. + const int kDeadlineSeconds = 540; + // Use any rpc to test retry. + std::unique_ptr<ReconnectService::Stub> retry_stub( + ReconnectService::NewStub(retry_channel)); + ClientContext retry_context; + retry_context.set_deadline(std::chrono::system_clock::now() + + std::chrono::seconds(kDeadlineSeconds)); + Status retry_status = + retry_stub->Start(&retry_context, empty_request, &empty_response); + GPR_ASSERT(retry_status.error_code() == grpc::StatusCode::DEADLINE_EXCEEDED); + gpr_log(GPR_INFO, "Done retrying, getting final data from server"); + + ClientContext stop_context; + ReconnectInfo response; + Status stop_status = + control_stub->Stop(&stop_context, empty_request, &response); + GPR_ASSERT(stop_status.ok()); + GPR_ASSERT(response.passed() == true); + return 0; +} diff --git a/test/cpp/interop/reconnect_interop_server.cc b/test/cpp/interop/reconnect_interop_server.cc new file mode 100644 index 0000000000..8bc51aa52e --- /dev/null +++ b/test/cpp/interop/reconnect_interop_server.cc @@ -0,0 +1,190 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include <condition_variable> +#include <memory> +#include <mutex> +#include <sstream> + +#include <signal.h> +#include <unistd.h> + +#include <gflags/gflags.h> +#include <grpc/grpc.h> +#include <grpc/support/log.h> +#include <grpc++/config.h> +#include <grpc++/server.h> +#include <grpc++/server_builder.h> +#include <grpc++/server_context.h> +#include <grpc++/server_credentials.h> +#include <grpc++/status.h> +#include "test/core/util/reconnect_server.h" +#include "test/cpp/util/test_config.h" +#include "test/proto/test.grpc.pb.h" +#include "test/proto/empty.grpc.pb.h" +#include "test/proto/messages.grpc.pb.h" + +DEFINE_int32(control_port, 0, "Server port for controlling the server."); +DEFINE_int32(retry_port, 0, + "Server port for raw tcp connections. All incoming " + "connections will be closed immediately."); + +using grpc::Server; +using grpc::ServerBuilder; +using grpc::ServerContext; +using grpc::ServerCredentials; +using grpc::ServerReader; +using grpc::ServerReaderWriter; +using grpc::ServerWriter; +using grpc::SslServerCredentialsOptions; +using grpc::Status; +using grpc::testing::Empty; +using grpc::testing::ReconnectService; +using grpc::testing::ReconnectInfo; + +static bool got_sigint = false; + +class ReconnectServiceImpl : public ReconnectService::Service { + public: + explicit ReconnectServiceImpl(int retry_port) + : retry_port_(retry_port), serving_(false), shutdown_(false) { + reconnect_server_init(&tcp_server_); + } + + ~ReconnectServiceImpl() { + if (tcp_server_.tcp_server) { + reconnect_server_destroy(&tcp_server_); + } + } + + void Poll(int seconds) { reconnect_server_poll(&tcp_server_, seconds); } + + Status Start(ServerContext* context, const Empty* request, Empty* response) { + std::unique_lock<std::mutex> lock(mu_); + while (serving_ && !shutdown_) { + cv_.wait(lock); + } + if (shutdown_) { + return Status(grpc::StatusCode::UNAVAILABLE, "shutting down"); + } + serving_ = true; + lock.unlock(); + + if (!tcp_server_.tcp_server) { + reconnect_server_start(&tcp_server_, retry_port_); + } else { + reconnect_server_clear_timestamps(&tcp_server_); + } + return Status::OK; + } + + Status Stop(ServerContext* context, const Empty* request, + ReconnectInfo* response) { + // extract timestamps and set response + Verify(response); + reconnect_server_clear_timestamps(&tcp_server_); + std::lock_guard<std::mutex> lock(mu_); + serving_ = false; + cv_.notify_one(); + return Status::OK; + } + + void Verify(ReconnectInfo* response) { + double expected_backoff = 1000.0; + const double kTransmissionDelay = 100.0; + const double kBackoffMultiplier = 1.6; + const double kJitterFactor = 0.2; + const int kMaxBackoffMs = 120 * 1000; + bool passed = true; + for (timestamp_list* cur = tcp_server_.head; cur && cur->next; + cur = cur->next) { + double backoff = gpr_time_to_millis( + gpr_time_sub(cur->next->timestamp, cur->timestamp)); + double min_backoff = expected_backoff * (1 - kJitterFactor); + double max_backoff = expected_backoff * (1 + kJitterFactor); + if (backoff < min_backoff - kTransmissionDelay || + backoff > max_backoff + kTransmissionDelay) { + passed = false; + } + response->add_backoff_ms(static_cast<gpr_int32>(backoff)); + expected_backoff *= kBackoffMultiplier; + expected_backoff = + expected_backoff > kMaxBackoffMs ? kMaxBackoffMs : expected_backoff; + } + response->set_passed(passed); + } + + void Shutdown() { + std::lock_guard<std::mutex> lock(mu_); + shutdown_ = true; + cv_.notify_all(); + } + + private: + int retry_port_; + reconnect_server tcp_server_; + bool serving_; + bool shutdown_; + std::mutex mu_; + std::condition_variable cv_; +}; + +void RunServer() { + std::ostringstream server_address; + server_address << "0.0.0.0:" << FLAGS_control_port; + ReconnectServiceImpl service(FLAGS_retry_port); + + ServerBuilder builder; + builder.RegisterService(&service); + builder.AddListeningPort(server_address.str(), + grpc::InsecureServerCredentials()); + std::unique_ptr<Server> server(builder.BuildAndStart()); + gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str()); + while (!got_sigint) { + service.Poll(5); + } + service.Shutdown(); +} + +static void sigint_handler(int x) { got_sigint = true; } + +int main(int argc, char** argv) { + grpc::testing::InitTest(&argc, &argv, true); + signal(SIGINT, sigint_handler); + + GPR_ASSERT(FLAGS_control_port != 0); + GPR_ASSERT(FLAGS_retry_port != 0); + RunServer(); + + return 0; +} diff --git a/test/cpp/interop/server.cc b/test/cpp/interop/server.cc index 6c429b3393..0097d1678c 100644 --- a/test/cpp/interop/server.cc +++ b/test/cpp/interop/server.cc @@ -150,7 +150,12 @@ class TestServiceImpl : public TestService::Service { } const gpr_uint32 client_accept_encodings_bitset = inspector.GetEncodingsAcceptedByClient(); - gpr_log(GPR_INFO, "%d", GPR_BITCOUNT(client_accept_encodings_bitset)); + + if (request->has_response_status()) { + return Status(static_cast<grpc::StatusCode> + (request->response_status().code()), + request->response_status().message()); + } return Status::OK; } |