xref: /aosp_15_r20/external/grpc-grpc/test/cpp/end2end/client_crash_test_server.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <string>
22 
23 #include "absl/flags/flag.h"
24 
25 #include <grpc/support/log.h>
26 #include <grpcpp/server.h>
27 #include <grpcpp/server_builder.h>
28 #include <grpcpp/server_context.h>
29 
30 #include "src/core/lib/gprpp/crash.h"
31 #include "src/proto/grpc/testing/echo.grpc.pb.h"
32 #include "test/cpp/util/test_config.h"
33 
34 ABSL_FLAG(std::string, address, "", "Address to bind to");
35 
36 using grpc::testing::EchoRequest;
37 using grpc::testing::EchoResponse;
38 
39 namespace grpc {
40 namespace testing {
41 
42 class ServiceImpl final : public grpc::testing::EchoTestService::Service {
BidiStream(ServerContext *,ServerReaderWriter<EchoResponse,EchoRequest> * stream)43   Status BidiStream(
44       ServerContext* /*context*/,
45       ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {
46     EchoRequest request;
47     EchoResponse response;
48     while (stream->Read(&request)) {
49       gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
50       response.set_message(request.message());
51       stream->Write(response);
52     }
53     return Status::OK;
54   }
55 };
56 
RunServer()57 void RunServer() {
58   ServiceImpl service;
59 
60   ServerBuilder builder;
61   builder.AddListeningPort(absl::GetFlag(FLAGS_address),
62                            grpc::InsecureServerCredentials());
63   builder.RegisterService(&service);
64   std::unique_ptr<Server> server(builder.BuildAndStart());
65   std::cout << "Server listening on " << absl::GetFlag(FLAGS_address)
66             << std::endl;
67   server->Wait();
68 }
69 }  // namespace testing
70 }  // namespace grpc
71 
main(int argc,char ** argv)72 int main(int argc, char** argv) {
73   grpc::testing::InitTest(&argc, &argv, true);
74   grpc::testing::RunServer();
75 
76   return 0;
77 }
78