Debian Codegraph @

test/cpp/interop/reconnect_interop_client.cc

raw
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/proto/grpc/testing/empty.pb.h"
31 #include "src/proto/grpc/testing/messages.pb.h"
32 #include "src/proto/grpc/testing/test.grpc.pb.h"
33 #include "test/cpp/util/create_test_channel.h"
34 #include "test/cpp/util/test_config.h"
35
36 ABSL_FLAG(int32_t, server_control_port, 0, "Server port for control rpcs.");
37 ABSL_FLAG(int32_t, server_retry_port, 0,
38 "Server port for testing reconnection.");
39 ABSL_FLAG(std::string, server_host, "localhost", "Server host to connect to");
40 // TODO(Capstan): Consider using absl::Duration
41 ABSL_FLAG(int32_t, max_reconnect_backoff_ms, 0,
42 "Maximum backoff time, or 0 for default.");
43
44 using grpc::CallCredentials;
45 using grpc::Channel;
46 using grpc::ChannelArguments;
47 using grpc::ClientContext;
48 using grpc::CreateTestChannel;
49 using grpc::Status;
50 using grpc::testing::Empty;
51 using grpc::testing::INSECURE;
52 using grpc::testing::ReconnectInfo;
53 using grpc::testing::ReconnectParams;
54 using grpc::testing::ReconnectService;
55 using grpc::testing::TLS;
56
57 int main(int argc, char** argv) {
58 grpc::testing::InitTest(&argc, &argv, true);
59 GPR_ASSERT(absl::GetFlag(FLAGS_server_control_port));
60 GPR_ASSERT(absl::GetFlag(FLAGS_server_retry_port));
61
62 std::ostringstream server_address;
63 server_address << absl::GetFlag(FLAGS_server_host) << ':'
64 << absl::GetFlag(FLAGS_server_control_port);
65 std::unique_ptr<ReconnectService::Stub> control_stub(
66 ReconnectService::NewStub(
67 CreateTestChannel(server_address.str(), INSECURE)));
68 ClientContext start_context;
69 ReconnectParams reconnect_params;
70 reconnect_params.set_max_reconnect_backoff_ms(
71 absl::GetFlag(FLAGS_max_reconnect_backoff_ms));
72 Empty empty_response;
73 Status start_status =
74 control_stub->Start(&start_context, reconnect_params, &empty_response);
75 GPR_ASSERT(start_status.ok());
76
77 gpr_log(GPR_INFO, "Starting connections with retries.");
78 server_address.str("");
79 server_address << absl::GetFlag(FLAGS_server_host) << ':'
80 << absl::GetFlag(FLAGS_server_retry_port);
81 ChannelArguments channel_args;
82 if (absl::GetFlag(FLAGS_max_reconnect_backoff_ms) > 0) {
83 channel_args.SetInt(GRPC_ARG_MAX_RECONNECT_BACKOFF_MS,
84 absl::GetFlag(FLAGS_max_reconnect_backoff_ms));
85 }
86 std::shared_ptr<Channel> retry_channel =
87 CreateTestChannel(server_address.str(), "foo.test.google.fr", TLS, false,
88 std::shared_ptr<CallCredentials>(), channel_args);
89
90 // About 13 retries.
91 const int kDeadlineSeconds = 540;
92 // Use any rpc to test retry.
93 std::unique_ptr<ReconnectService::Stub> retry_stub(
94 ReconnectService::NewStub(retry_channel));
95 ClientContext retry_context;
96 retry_context.set_deadline(std::chrono::system_clock::now() +
97 std::chrono::seconds(kDeadlineSeconds));
98 Status retry_status =
99 retry_stub->Start(&retry_context, reconnect_params, &empty_response);
100 GPR_ASSERT(retry_status.error_code() == grpc::StatusCode::DEADLINE_EXCEEDED);
101 gpr_log(GPR_INFO, "Done retrying, getting final data from server");
102
103 ClientContext stop_context;
104 ReconnectInfo response;
105 Status stop_status = control_stub->Stop(&stop_context, Empty(), &response);
106 GPR_ASSERT(stop_status.ok());
107 GPR_ASSERT(response.passed() == true);
108 gpr_log(GPR_INFO, "Passed");
109 return 0;
110 }