yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 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 | */ |
| 33 | |
| 34 | #include <memory> |
| 35 | #include <sstream> |
| 36 | |
| 37 | #include <grpc/grpc.h> |
| 38 | #include <grpc/support/log.h> |
| 39 | #include <gflags/gflags.h> |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame^] | 40 | #include <grpc++/channel.h> |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 41 | #include <grpc++/client_context.h> |
| 42 | #include <grpc++/status.h> |
| 43 | #include "test/cpp/util/create_test_channel.h" |
| 44 | #include "test/cpp/util/test_config.h" |
| 45 | #include "test/proto/test.grpc.pb.h" |
| 46 | #include "test/proto/empty.grpc.pb.h" |
| 47 | #include "test/proto/messages.grpc.pb.h" |
| 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."); |
| 51 | DEFINE_string(server_host, "127.0.0.1", "Server host to connect to"); |
| 52 | |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame^] | 53 | using grpc::Channel; |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 54 | using grpc::ClientContext; |
| 55 | using grpc::CreateTestChannel; |
| 56 | using grpc::Status; |
| 57 | using grpc::testing::Empty; |
| 58 | using grpc::testing::ReconnectInfo; |
| 59 | using grpc::testing::ReconnectService; |
| 60 | |
| 61 | int main(int argc, char** argv) { |
| 62 | grpc::testing::InitTest(&argc, &argv, true); |
| 63 | GPR_ASSERT(FLAGS_server_control_port); |
| 64 | GPR_ASSERT(FLAGS_server_retry_port); |
| 65 | |
| 66 | std::ostringstream server_address; |
| 67 | server_address << FLAGS_server_host << ':' << FLAGS_server_control_port; |
| 68 | std::unique_ptr<ReconnectService::Stub> control_stub( |
| 69 | ReconnectService::NewStub( |
| 70 | CreateTestChannel(server_address.str(), false))); |
| 71 | ClientContext start_context; |
| 72 | Empty empty_request; |
| 73 | Empty empty_response; |
| 74 | Status start_status = |
| 75 | control_stub->Start(&start_context, empty_request, &empty_response); |
| 76 | GPR_ASSERT(start_status.ok()); |
| 77 | |
| 78 | gpr_log(GPR_INFO, "Starting connections with retries."); |
| 79 | server_address.str(""); |
| 80 | server_address << FLAGS_server_host << ':' << FLAGS_server_retry_port; |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame^] | 81 | std::shared_ptr<Channel> retry_channel = |
yang-g | c9c69e2 | 2015-07-24 14:38:26 -0700 | [diff] [blame] | 82 | CreateTestChannel(server_address.str(), true); |
| 83 | // About 13 retries. |
| 84 | const int kDeadlineSeconds = 540; |
| 85 | // Use any rpc to test retry. |
| 86 | std::unique_ptr<ReconnectService::Stub> retry_stub( |
| 87 | ReconnectService::NewStub(retry_channel)); |
| 88 | ClientContext retry_context; |
| 89 | retry_context.set_deadline(std::chrono::system_clock::now() + |
| 90 | std::chrono::seconds(kDeadlineSeconds)); |
| 91 | Status retry_status = |
| 92 | retry_stub->Start(&retry_context, empty_request, &empty_response); |
| 93 | GPR_ASSERT(retry_status.error_code() == grpc::StatusCode::DEADLINE_EXCEEDED); |
| 94 | gpr_log(GPR_INFO, "Done retrying, getting final data from server"); |
| 95 | |
| 96 | ClientContext stop_context; |
| 97 | ReconnectInfo response; |
| 98 | Status stop_status = |
| 99 | control_stub->Stop(&stop_context, empty_request, &response); |
| 100 | GPR_ASSERT(stop_status.ok()); |
| 101 | GPR_ASSERT(response.passed() == true); |
| 102 | return 0; |
| 103 | } |