1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // 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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // 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.
16 //
17 //
18
19 #include <memory>
20 #include <sstream>
21
22 #include "absl/flags/flag.h"
23
24 #include <grpc/grpc.h>
25 #include <grpc/support/log.h>
26 #include <grpcpp/channel.h>
27 #include <grpcpp/client_context.h>
28 #include <grpcpp/support/channel_arguments.h>
29
30 #include "src/core/lib/gprpp/crash.h"
31 #include "src/proto/grpc/testing/empty.pb.h"
32 #include "src/proto/grpc/testing/messages.pb.h"
33 #include "src/proto/grpc/testing/test.grpc.pb.h"
34 #include "test/cpp/util/create_test_channel.h"
35 #include "test/cpp/util/test_config.h"
36
37 ABSL_FLAG(int32_t, server_control_port, 0, "Server port for control rpcs.");
38 ABSL_FLAG(int32_t, server_retry_port, 0,
39 "Server port for testing reconnection.");
40 ABSL_FLAG(std::string, server_host, "localhost", "Server host to connect to");
41 // TODO(Capstan): Consider using absl::Duration
42 ABSL_FLAG(int32_t, max_reconnect_backoff_ms, 0,
43 "Maximum backoff time, or 0 for default.");
44
45 using grpc::CallCredentials;
46 using grpc::Channel;
47 using grpc::ChannelArguments;
48 using grpc::ClientContext;
49 using grpc::CreateTestChannel;
50 using grpc::Status;
51 using grpc::testing::Empty;
52 using grpc::testing::INSECURE;
53 using grpc::testing::ReconnectInfo;
54 using grpc::testing::ReconnectParams;
55 using grpc::testing::ReconnectService;
56 using grpc::testing::TLS;
57
main(int argc,char ** argv)58 int main(int argc, char** argv) {
59 grpc::testing::InitTest(&argc, &argv, true);
60 GPR_ASSERT(absl::GetFlag(FLAGS_server_control_port));
61 GPR_ASSERT(absl::GetFlag(FLAGS_server_retry_port));
62
63 std::ostringstream server_address;
64 server_address << absl::GetFlag(FLAGS_server_host) << ':'
65 << absl::GetFlag(FLAGS_server_control_port);
66 std::unique_ptr<ReconnectService::Stub> control_stub(
67 ReconnectService::NewStub(
68 CreateTestChannel(server_address.str(), INSECURE)));
69 ClientContext start_context;
70 ReconnectParams reconnect_params;
71 reconnect_params.set_max_reconnect_backoff_ms(
72 absl::GetFlag(FLAGS_max_reconnect_backoff_ms));
73 Empty empty_response;
74 Status start_status =
75 control_stub->Start(&start_context, reconnect_params, &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 << absl::GetFlag(FLAGS_server_host) << ':'
81 << absl::GetFlag(FLAGS_server_retry_port);
82 ChannelArguments channel_args;
83 if (absl::GetFlag(FLAGS_max_reconnect_backoff_ms) > 0) {
84 channel_args.SetInt(GRPC_ARG_MAX_RECONNECT_BACKOFF_MS,
85 absl::GetFlag(FLAGS_max_reconnect_backoff_ms));
86 }
87 std::shared_ptr<Channel> retry_channel =
88 CreateTestChannel(server_address.str(), "foo.test.google.fr", TLS, false,
89 std::shared_ptr<CallCredentials>(), channel_args);
90
91 // About 13 retries.
92 const int kDeadlineSeconds = 540;
93 // Use any rpc to test retry.
94 std::unique_ptr<ReconnectService::Stub> retry_stub(
95 ReconnectService::NewStub(retry_channel));
96 ClientContext retry_context;
97 retry_context.set_deadline(std::chrono::system_clock::now() +
98 std::chrono::seconds(kDeadlineSeconds));
99 Status retry_status =
100 retry_stub->Start(&retry_context, reconnect_params, &empty_response);
101 GPR_ASSERT(retry_status.error_code() == grpc::StatusCode::DEADLINE_EXCEEDED);
102 gpr_log(GPR_INFO, "Done retrying, getting final data from server");
103
104 ClientContext stop_context;
105 ReconnectInfo response;
106 Status stop_status = control_stub->Stop(&stop_context, Empty(), &response);
107 GPR_ASSERT(stop_status.ok());
108 GPR_ASSERT(response.passed() == true);
109 gpr_log(GPR_INFO, "Passed");
110 return 0;
111 }
112