blob: 675c6ff07398ca515475057fe7e3dc182cf08612 [file] [log] [blame]
yang-gc9c69e22015-07-24 14:38:26 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * 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 */
33
34#include <memory>
35#include <sstream>
36
37#include <grpc/grpc.h>
38#include <grpc/support/log.h>
39#include <gflags/gflags.h>
yang-g8c2be9f2015-08-19 16:28:09 -070040#include <grpc++/channel.h>
yang-gc9c69e22015-07-24 14:38:26 -070041#include <grpc++/client_context.h>
42#include <grpc++/status.h>
43#include "test/cpp/util/create_test_channel.h"
44#include "test/cpp/util/test_config.h"
45#include "test/proto/test.grpc.pb.h"
46#include "test/proto/empty.grpc.pb.h"
47#include "test/proto/messages.grpc.pb.h"
48
49DEFINE_int32(server_control_port, 0, "Server port for control rpcs.");
50DEFINE_int32(server_retry_port, 0, "Server port for testing reconnection.");
51DEFINE_string(server_host, "127.0.0.1", "Server host to connect to");
52
yang-g8c2be9f2015-08-19 16:28:09 -070053using grpc::Channel;
yang-gc9c69e22015-07-24 14:38:26 -070054using grpc::ClientContext;
55using grpc::CreateTestChannel;
56using grpc::Status;
57using grpc::testing::Empty;
58using grpc::testing::ReconnectInfo;
59using grpc::testing::ReconnectService;
60
61int main(int argc, char** argv) {
62 grpc::testing::InitTest(&argc, &argv, true);
63 GPR_ASSERT(FLAGS_server_control_port);
64 GPR_ASSERT(FLAGS_server_retry_port);
65
66 std::ostringstream server_address;
67 server_address << FLAGS_server_host << ':' << FLAGS_server_control_port;
68 std::unique_ptr<ReconnectService::Stub> control_stub(
69 ReconnectService::NewStub(
70 CreateTestChannel(server_address.str(), false)));
71 ClientContext start_context;
72 Empty empty_request;
73 Empty empty_response;
74 Status start_status =
75 control_stub->Start(&start_context, empty_request, &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 << FLAGS_server_host << ':' << FLAGS_server_retry_port;
yang-g8c2be9f2015-08-19 16:28:09 -070081 std::shared_ptr<Channel> retry_channel =
yang-gc9c69e22015-07-24 14:38:26 -070082 CreateTestChannel(server_address.str(), true);
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, empty_request, &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 =
99 control_stub->Stop(&stop_context, empty_request, &response);
100 GPR_ASSERT(stop_status.ok());
101 GPR_ASSERT(response.passed() == true);
102 return 0;
103}