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 <gflags/gflags.h>
20 #include <iostream>
21 #include <memory>
22 #include <sstream>
23 #include <string>
24
25 #include <grpc/support/log.h>
26 #include <grpcpp/channel.h>
27 #include <grpcpp/client_context.h>
28 #include <grpcpp/create_channel.h>
29
30 #include "src/proto/grpc/testing/echo.grpc.pb.h"
31 #include "test/cpp/util/test_config.h"
32
33 DEFINE_string(address, "", "Address to connect to");
34 DEFINE_string(mode, "", "Test mode to use");
35
36 using grpc::testing::EchoRequest;
37 using grpc::testing::EchoResponse;
38
main(int argc,char ** argv)39 int main(int argc, char** argv) {
40 grpc::testing::InitTest(&argc, &argv, true);
41 auto stub = grpc::testing::EchoTestService::NewStub(
42 grpc::CreateChannel(FLAGS_address, grpc::InsecureChannelCredentials()));
43
44 EchoRequest request;
45 EchoResponse response;
46 grpc::ClientContext context;
47 context.set_wait_for_ready(true);
48
49 if (FLAGS_mode == "bidi") {
50 auto stream = stub->BidiStream(&context);
51 for (int i = 0;; i++) {
52 std::ostringstream msg;
53 msg << "Hello " << i;
54 request.set_message(msg.str());
55 GPR_ASSERT(stream->Write(request));
56 GPR_ASSERT(stream->Read(&response));
57 GPR_ASSERT(response.message() == request.message());
58 }
59 } else if (FLAGS_mode == "response") {
60 EchoRequest request;
61 request.set_message("Hello");
62 auto stream = stub->ResponseStream(&context, request);
63 for (;;) {
64 GPR_ASSERT(stream->Read(&response));
65 }
66 } else {
67 gpr_log(GPR_ERROR, "invalid test mode '%s'", FLAGS_mode.c_str());
68 return 1;
69 }
70
71 return 0;
72 }
73