blob: 01d985068dcb05662cf1f0d251bd8ffcbfd2246d [file] [log] [blame]
yang-gc9c69e22015-07-24 14:38:26 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
yang-gc9c69e22015-07-24 14:38:26 -07004 * 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 Tillerc6611ef2016-03-02 17:43:09 -080033
yang-gc9c69e22015-07-24 14:38:26 -070034#include <memory>
35#include <sstream>
36
yang-gc9c69e22015-07-24 14:38:26 -070037#include <gflags/gflags.h>
yang-g8c2be9f2015-08-19 16:28:09 -070038#include <grpc++/channel.h>
yang-gc9c69e22015-07-24 14:38:26 -070039#include <grpc++/client_context.h>
Aaron Isotton24e69bf2016-02-26 11:53:22 -080040#include <grpc++/support/channel_arguments.h>
Craig Tillerf40df232016-03-25 13:38:14 -070041#include <grpc/grpc.h>
42#include <grpc/support/log.h>
yang-gd0084c22017-03-06 11:23:35 -080043#include "src/proto/grpc/testing/empty.pb.h"
44#include "src/proto/grpc/testing/messages.pb.h"
Craig Tillerf40df232016-03-25 13:38:14 -070045#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-gc9c69e22015-07-24 14:38:26 -070048
49DEFINE_int32(server_control_port, 0, "Server port for control rpcs.");
50DEFINE_int32(server_retry_port, 0, "Server port for testing reconnection.");
Paul Marks3a5bba02017-02-07 16:28:09 -080051DEFINE_string(server_host, "localhost", "Server host to connect to");
Aaron Isotton24e69bf2016-02-26 11:53:22 -080052DEFINE_int32(max_reconnect_backoff_ms, 0,
53 "Maximum backoff time, or 0 for default.");
yang-gc9c69e22015-07-24 14:38:26 -070054
Aaron Isotton24e69bf2016-02-26 11:53:22 -080055using grpc::CallCredentials;
yang-g8c2be9f2015-08-19 16:28:09 -070056using grpc::Channel;
Aaron Isotton24e69bf2016-02-26 11:53:22 -080057using grpc::ChannelArguments;
yang-gc9c69e22015-07-24 14:38:26 -070058using grpc::ClientContext;
59using grpc::CreateTestChannel;
60using grpc::Status;
61using grpc::testing::Empty;
62using grpc::testing::ReconnectInfo;
Aaron Isotton24e69bf2016-02-26 11:53:22 -080063using grpc::testing::ReconnectParams;
yang-gc9c69e22015-07-24 14:38:26 -070064using grpc::testing::ReconnectService;
65
66int 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 Isotton24e69bf2016-02-26 11:53:22 -080077 ReconnectParams reconnect_params;
78 reconnect_params.set_max_reconnect_backoff_ms(FLAGS_max_reconnect_backoff_ms);
yang-gc9c69e22015-07-24 14:38:26 -070079 Empty empty_response;
80 Status start_status =
Aaron Isotton24e69bf2016-02-26 11:53:22 -080081 control_stub->Start(&start_context, reconnect_params, &empty_response);
yang-gc9c69e22015-07-24 14:38:26 -070082 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 Isotton24e69bf2016-02-26 11:53:22 -080087 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-g8c2be9f2015-08-19 16:28:09 -070092 std::shared_ptr<Channel> retry_channel =
Aaron Isotton24e69bf2016-02-26 11:53:22 -080093 CreateTestChannel(server_address.str(), "foo.test.google.fr", true, false,
94 std::shared_ptr<CallCredentials>(), channel_args);
95
yang-gc9c69e22015-07-24 14:38:26 -070096 // 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 Isotton24e69bf2016-02-26 11:53:22 -0800105 retry_stub->Start(&retry_context, reconnect_params, &empty_response);
yang-gc9c69e22015-07-24 14:38:26 -0700106 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 Isotton24e69bf2016-02-26 11:53:22 -0800111 Status stop_status = control_stub->Stop(&stop_context, Empty(), &response);
yang-gc9c69e22015-07-24 14:38:26 -0700112 GPR_ASSERT(stop_status.ok());
113 GPR_ASSERT(response.passed() == true);
Aaron Isotton24e69bf2016-02-26 11:53:22 -0800114 gpr_log(GPR_INFO, "Passed");
yang-gc9c69e22015-07-24 14:38:26 -0700115 return 0;
116}