blob: 9a451fa3f28b8fcc343e01737a664e736a579a84 [file] [log] [blame]
yang-gc9c69e22015-07-24 14:38:26 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
yang-gc9c69e22015-07-24 14:38:26 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
yang-gc9c69e22015-07-24 14:38:26 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
yang-gc9c69e22015-07-24 14:38:26 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
yang-gc9c69e22015-07-24 14:38:26 -070016 *
17 */
Craig Tillerc6611ef2016-03-02 17:43:09 -080018
yang-gc9c69e22015-07-24 14:38:26 -070019#include <memory>
20#include <sstream>
21
yang-gc9c69e22015-07-24 14:38:26 -070022#include <gflags/gflags.h>
Craig Tillerf40df232016-03-25 13:38:14 -070023#include <grpc/grpc.h>
24#include <grpc/support/log.h>
Vijay Paic90a8562018-03-08 21:20:24 -080025#include <grpcpp/channel.h>
26#include <grpcpp/client_context.h>
27#include <grpcpp/support/channel_arguments.h>
yang-gd0084c22017-03-06 11:23:35 -080028#include "src/proto/grpc/testing/empty.pb.h"
29#include "src/proto/grpc/testing/messages.pb.h"
Craig Tillerf40df232016-03-25 13:38:14 -070030#include "src/proto/grpc/testing/test.grpc.pb.h"
31#include "test/cpp/util/create_test_channel.h"
32#include "test/cpp/util/test_config.h"
yang-gc9c69e22015-07-24 14:38:26 -070033
34DEFINE_int32(server_control_port, 0, "Server port for control rpcs.");
35DEFINE_int32(server_retry_port, 0, "Server port for testing reconnection.");
Paul Marks3a5bba02017-02-07 16:28:09 -080036DEFINE_string(server_host, "localhost", "Server host to connect to");
Aaron Isotton24e69bf2016-02-26 11:53:22 -080037DEFINE_int32(max_reconnect_backoff_ms, 0,
38 "Maximum backoff time, or 0 for default.");
yang-gc9c69e22015-07-24 14:38:26 -070039
Aaron Isotton24e69bf2016-02-26 11:53:22 -080040using grpc::CallCredentials;
yang-g8c2be9f2015-08-19 16:28:09 -070041using grpc::Channel;
Aaron Isotton24e69bf2016-02-26 11:53:22 -080042using grpc::ChannelArguments;
yang-gc9c69e22015-07-24 14:38:26 -070043using grpc::ClientContext;
44using grpc::CreateTestChannel;
45using grpc::Status;
46using grpc::testing::Empty;
47using grpc::testing::ReconnectInfo;
Aaron Isotton24e69bf2016-02-26 11:53:22 -080048using grpc::testing::ReconnectParams;
yang-gc9c69e22015-07-24 14:38:26 -070049using grpc::testing::ReconnectService;
50
51int main(int argc, char** argv) {
52 grpc::testing::InitTest(&argc, &argv, true);
53 GPR_ASSERT(FLAGS_server_control_port);
54 GPR_ASSERT(FLAGS_server_retry_port);
55
56 std::ostringstream server_address;
57 server_address << FLAGS_server_host << ':' << FLAGS_server_control_port;
58 std::unique_ptr<ReconnectService::Stub> control_stub(
59 ReconnectService::NewStub(
60 CreateTestChannel(server_address.str(), false)));
61 ClientContext start_context;
Aaron Isotton24e69bf2016-02-26 11:53:22 -080062 ReconnectParams reconnect_params;
63 reconnect_params.set_max_reconnect_backoff_ms(FLAGS_max_reconnect_backoff_ms);
yang-gc9c69e22015-07-24 14:38:26 -070064 Empty empty_response;
65 Status start_status =
Aaron Isotton24e69bf2016-02-26 11:53:22 -080066 control_stub->Start(&start_context, reconnect_params, &empty_response);
yang-gc9c69e22015-07-24 14:38:26 -070067 GPR_ASSERT(start_status.ok());
68
69 gpr_log(GPR_INFO, "Starting connections with retries.");
70 server_address.str("");
71 server_address << FLAGS_server_host << ':' << FLAGS_server_retry_port;
Aaron Isotton24e69bf2016-02-26 11:53:22 -080072 ChannelArguments channel_args;
73 if (FLAGS_max_reconnect_backoff_ms > 0) {
74 channel_args.SetInt(GRPC_ARG_MAX_RECONNECT_BACKOFF_MS,
75 FLAGS_max_reconnect_backoff_ms);
76 }
yang-g8c2be9f2015-08-19 16:28:09 -070077 std::shared_ptr<Channel> retry_channel =
Aaron Isotton24e69bf2016-02-26 11:53:22 -080078 CreateTestChannel(server_address.str(), "foo.test.google.fr", true, false,
79 std::shared_ptr<CallCredentials>(), channel_args);
80
yang-gc9c69e22015-07-24 14:38:26 -070081 // About 13 retries.
82 const int kDeadlineSeconds = 540;
83 // Use any rpc to test retry.
84 std::unique_ptr<ReconnectService::Stub> retry_stub(
85 ReconnectService::NewStub(retry_channel));
86 ClientContext retry_context;
87 retry_context.set_deadline(std::chrono::system_clock::now() +
88 std::chrono::seconds(kDeadlineSeconds));
89 Status retry_status =
Aaron Isotton24e69bf2016-02-26 11:53:22 -080090 retry_stub->Start(&retry_context, reconnect_params, &empty_response);
yang-gc9c69e22015-07-24 14:38:26 -070091 GPR_ASSERT(retry_status.error_code() == grpc::StatusCode::DEADLINE_EXCEEDED);
92 gpr_log(GPR_INFO, "Done retrying, getting final data from server");
93
94 ClientContext stop_context;
95 ReconnectInfo response;
Aaron Isotton24e69bf2016-02-26 11:53:22 -080096 Status stop_status = control_stub->Stop(&stop_context, Empty(), &response);
yang-gc9c69e22015-07-24 14:38:26 -070097 GPR_ASSERT(stop_status.ok());
98 GPR_ASSERT(response.passed() == true);
Aaron Isotton24e69bf2016-02-26 11:53:22 -080099 gpr_log(GPR_INFO, "Passed");
yang-gc9c69e22015-07-24 14:38:26 -0700100 return 0;
101}