• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2016 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 #ifndef GRPC_TEST_CPP_END2END_TEST_SERVICE_IMPL_H
19 #define GRPC_TEST_CPP_END2END_TEST_SERVICE_IMPL_H
20 
21 #include <memory>
22 #include <mutex>
23 
24 #include <grpc/grpc.h>
25 #include <grpcpp/server_context.h>
26 
27 #include "src/proto/grpc/testing/echo.grpc.pb.h"
28 
29 namespace grpc {
30 namespace testing {
31 
32 const int kServerDefaultResponseStreamsToSend = 3;
33 const char* const kServerResponseStreamsToSend = "server_responses_to_send";
34 const char* const kServerTryCancelRequest = "server_try_cancel";
35 const char* const kDebugInfoTrailerKey = "debug-info-bin";
36 const char* const kServerFinishAfterNReads = "server_finish_after_n_reads";
37 const char* const kServerUseCoalescingApi = "server_use_coalescing_api";
38 
39 typedef enum {
40   DO_NOT_CANCEL = 0,
41   CANCEL_BEFORE_PROCESSING,
42   CANCEL_DURING_PROCESSING,
43   CANCEL_AFTER_PROCESSING
44 } ServerTryCancelRequestPhase;
45 
46 class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
47  public:
TestServiceImpl()48   TestServiceImpl() : signal_client_(false), host_() {}
TestServiceImpl(const grpc::string & host)49   explicit TestServiceImpl(const grpc::string& host)
50       : signal_client_(false), host_(new grpc::string(host)) {}
51 
52   Status Echo(ServerContext* context, const EchoRequest* request,
53               EchoResponse* response) override;
54 
55   // Unimplemented is left unimplemented to test the returned error.
56 
57   Status RequestStream(ServerContext* context,
58                        ServerReader<EchoRequest>* reader,
59                        EchoResponse* response) override;
60 
61   Status ResponseStream(ServerContext* context, const EchoRequest* request,
62                         ServerWriter<EchoResponse>* writer) override;
63 
64   Status BidiStream(
65       ServerContext* context,
66       ServerReaderWriter<EchoResponse, EchoRequest>* stream) override;
67 
signal_client()68   bool signal_client() {
69     std::unique_lock<std::mutex> lock(mu_);
70     return signal_client_;
71   }
72 
73  private:
74   int GetIntValueFromMetadata(
75       const char* key,
76       const std::multimap<grpc::string_ref, grpc::string_ref>& metadata,
77       int default_value);
78 
79   void ServerTryCancel(ServerContext* context);
80 
81  private:
82   bool signal_client_;
83   std::mutex mu_;
84   std::unique_ptr<grpc::string> host_;
85 };
86 
87 }  // namespace testing
88 }  // namespace grpc
89 
90 #endif  // GRPC_TEST_CPP_END2END_TEST_SERVICE_IMPL_H
91