yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
Craig Tiller | c6611ef | 2016-03-02 17:43:09 -0800 | [diff] [blame] | 33 | |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 34 | #include <memory> |
| 35 | #include <sstream> |
| 36 | |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 37 | #include <gflags/gflags.h> |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 38 | #include <grpc++/channel.h> |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 39 | #include <grpc++/client_context.h> |
Aaron Isotton | 24e69bf | 2016-02-26 11:53:22 -0800 | [diff] [blame] | 40 | #include <grpc++/support/channel_arguments.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 41 | #include <grpc/grpc.h> |
| 42 | #include <grpc/support/log.h> |
yang-g | d0084c2 | 2017-03-06 11:23:35 -0800 | [diff] [blame^] | 43 | #include "src/proto/grpc/testing/empty.pb.h" |
| 44 | #include "src/proto/grpc/testing/messages.pb.h" |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 45 | #include "src/proto/grpc/testing/test.grpc.pb.h" |
| 46 | #include "test/cpp/util/create_test_channel.h" |
| 47 | #include "test/cpp/util/test_config.h" |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 48 | |
| 49 | DEFINE_int32(server_control_port, 0, "Server port for control rpcs."); |
| 50 | DEFINE_int32(server_retry_port, 0, "Server port for testing reconnection."); |
Paul Marks | 3a5bba0 | 2017-02-07 16:28:09 -0800 | [diff] [blame] | 51 | DEFINE_string(server_host, "localhost", "Server host to connect to"); |
Aaron Isotton | 24e69bf | 2016-02-26 11:53:22 -0800 | [diff] [blame] | 52 | DEFINE_int32(max_reconnect_backoff_ms, 0, |
| 53 | "Maximum backoff time, or 0 for default."); |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 54 | |
Aaron Isotton | 24e69bf | 2016-02-26 11:53:22 -0800 | [diff] [blame] | 55 | using grpc::CallCredentials; |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 56 | using grpc::Channel; |
Aaron Isotton | 24e69bf | 2016-02-26 11:53:22 -0800 | [diff] [blame] | 57 | using grpc::ChannelArguments; |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 58 | using grpc::ClientContext; |
| 59 | using grpc::CreateTestChannel; |
| 60 | using grpc::Status; |
| 61 | using grpc::testing::Empty; |
| 62 | using grpc::testing::ReconnectInfo; |
Aaron Isotton | 24e69bf | 2016-02-26 11:53:22 -0800 | [diff] [blame] | 63 | using grpc::testing::ReconnectParams; |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 64 | using grpc::testing::ReconnectService; |
| 65 | |
| 66 | int main(int argc, char** argv) { |
| 67 | grpc::testing::InitTest(&argc, &argv, true); |
| 68 | GPR_ASSERT(FLAGS_server_control_port); |
| 69 | GPR_ASSERT(FLAGS_server_retry_port); |
| 70 | |
| 71 | std::ostringstream server_address; |
| 72 | server_address << FLAGS_server_host << ':' << FLAGS_server_control_port; |
| 73 | std::unique_ptr<ReconnectService::Stub> control_stub( |
| 74 | ReconnectService::NewStub( |
| 75 | CreateTestChannel(server_address.str(), false))); |
| 76 | ClientContext start_context; |
Aaron Isotton | 24e69bf | 2016-02-26 11:53:22 -0800 | [diff] [blame] | 77 | ReconnectParams reconnect_params; |
| 78 | reconnect_params.set_max_reconnect_backoff_ms(FLAGS_max_reconnect_backoff_ms); |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 79 | Empty empty_response; |
| 80 | Status start_status = |
Aaron Isotton | 24e69bf | 2016-02-26 11:53:22 -0800 | [diff] [blame] | 81 | control_stub->Start(&start_context, reconnect_params, &empty_response); |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 82 | GPR_ASSERT(start_status.ok()); |
| 83 | |
| 84 | gpr_log(GPR_INFO, "Starting connections with retries."); |
| 85 | server_address.str(""); |
| 86 | server_address << FLAGS_server_host << ':' << FLAGS_server_retry_port; |
Aaron Isotton | 24e69bf | 2016-02-26 11:53:22 -0800 | [diff] [blame] | 87 | ChannelArguments channel_args; |
| 88 | if (FLAGS_max_reconnect_backoff_ms > 0) { |
| 89 | channel_args.SetInt(GRPC_ARG_MAX_RECONNECT_BACKOFF_MS, |
| 90 | FLAGS_max_reconnect_backoff_ms); |
| 91 | } |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 92 | std::shared_ptr<Channel> retry_channel = |
Aaron Isotton | 24e69bf | 2016-02-26 11:53:22 -0800 | [diff] [blame] | 93 | CreateTestChannel(server_address.str(), "foo.test.google.fr", true, false, |
| 94 | std::shared_ptr<CallCredentials>(), channel_args); |
| 95 | |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 96 | // About 13 retries. |
| 97 | const int kDeadlineSeconds = 540; |
| 98 | // Use any rpc to test retry. |
| 99 | std::unique_ptr<ReconnectService::Stub> retry_stub( |
| 100 | ReconnectService::NewStub(retry_channel)); |
| 101 | ClientContext retry_context; |
| 102 | retry_context.set_deadline(std::chrono::system_clock::now() + |
| 103 | std::chrono::seconds(kDeadlineSeconds)); |
| 104 | Status retry_status = |
Aaron Isotton | 24e69bf | 2016-02-26 11:53:22 -0800 | [diff] [blame] | 105 | retry_stub->Start(&retry_context, reconnect_params, &empty_response); |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 106 | GPR_ASSERT(retry_status.error_code() == grpc::StatusCode::DEADLINE_EXCEEDED); |
| 107 | gpr_log(GPR_INFO, "Done retrying, getting final data from server"); |
| 108 | |
| 109 | ClientContext stop_context; |
| 110 | ReconnectInfo response; |
Aaron Isotton | 24e69bf | 2016-02-26 11:53:22 -0800 | [diff] [blame] | 111 | Status stop_status = control_stub->Stop(&stop_context, Empty(), &response); |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 112 | GPR_ASSERT(stop_status.ok()); |
| 113 | GPR_ASSERT(response.passed() == true); |
Aaron Isotton | 24e69bf | 2016-02-26 11:53:22 -0800 | [diff] [blame] | 114 | gpr_log(GPR_INFO, "Passed"); |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 115 | return 0; |
| 116 | } |