• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gflags/gflags.h>
23  #include <grpc/grpc.h>
24  #include <grpc/support/log.h>
25  #include <grpcpp/channel.h>
26  #include <grpcpp/client_context.h>
27  #include <grpcpp/support/channel_arguments.h>
28  #include "src/proto/grpc/testing/empty.pb.h"
29  #include "src/proto/grpc/testing/messages.pb.h"
30  #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"
33  
34  DEFINE_int32(server_control_port, 0, "Server port for control rpcs.");
35  DEFINE_int32(server_retry_port, 0, "Server port for testing reconnection.");
36  DEFINE_string(server_host, "localhost", "Server host to connect to");
37  DEFINE_int32(max_reconnect_backoff_ms, 0,
38               "Maximum backoff time, or 0 for default.");
39  
40  using grpc::CallCredentials;
41  using grpc::Channel;
42  using grpc::ChannelArguments;
43  using grpc::ClientContext;
44  using grpc::CreateTestChannel;
45  using grpc::Status;
46  using grpc::testing::Empty;
47  using grpc::testing::INSECURE;
48  using grpc::testing::ReconnectInfo;
49  using grpc::testing::ReconnectParams;
50  using grpc::testing::ReconnectService;
51  using grpc::testing::TLS;
52  
main(int argc,char ** argv)53  int main(int argc, char** argv) {
54    grpc::testing::InitTest(&argc, &argv, true);
55    GPR_ASSERT(FLAGS_server_control_port);
56    GPR_ASSERT(FLAGS_server_retry_port);
57  
58    std::ostringstream server_address;
59    server_address << FLAGS_server_host << ':' << FLAGS_server_control_port;
60    std::unique_ptr<ReconnectService::Stub> control_stub(
61        ReconnectService::NewStub(
62            CreateTestChannel(server_address.str(), INSECURE)));
63    ClientContext start_context;
64    ReconnectParams reconnect_params;
65    reconnect_params.set_max_reconnect_backoff_ms(FLAGS_max_reconnect_backoff_ms);
66    Empty empty_response;
67    Status start_status =
68        control_stub->Start(&start_context, reconnect_params, &empty_response);
69    GPR_ASSERT(start_status.ok());
70  
71    gpr_log(GPR_INFO, "Starting connections with retries.");
72    server_address.str("");
73    server_address << FLAGS_server_host << ':' << FLAGS_server_retry_port;
74    ChannelArguments channel_args;
75    if (FLAGS_max_reconnect_backoff_ms > 0) {
76      channel_args.SetInt(GRPC_ARG_MAX_RECONNECT_BACKOFF_MS,
77                          FLAGS_max_reconnect_backoff_ms);
78    }
79    std::shared_ptr<Channel> retry_channel =
80        CreateTestChannel(server_address.str(), "foo.test.google.fr", TLS, false,
81                          std::shared_ptr<CallCredentials>(), channel_args);
82  
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, reconnect_params, &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 = control_stub->Stop(&stop_context, Empty(), &response);
99    GPR_ASSERT(stop_status.ok());
100    GPR_ASSERT(response.passed() == true);
101    gpr_log(GPR_INFO, "Passed");
102    return 0;
103  }
104