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