xref: /aosp_15_r20/external/grpc-grpc/test/cpp/interop/pre_stop_hook_server.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2023 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 #ifndef GRPC_TEST_CPP_INTEROP_PRE_STOP_HOOK_SERVER_H
20 #define GRPC_TEST_CPP_INTEROP_PRE_STOP_HOOK_SERVER_H
21 
22 #include <grpc/support/port_platform.h>
23 #include <grpcpp/server.h>
24 
25 #include "src/core/lib/gprpp/sync.h"
26 #include "src/proto/grpc/testing/messages.pb.h"
27 #include "src/proto/grpc/testing/test.grpc.pb.h"
28 
29 namespace grpc {
30 namespace testing {
31 
32 class HookServiceImpl final : public HookService::CallbackService {
33  public:
34   ServerUnaryReactor* Hook(CallbackServerContext* context,
35                            const Empty* /* request */,
36                            Empty* /* reply */) override;
37 
38   ServerUnaryReactor* SetReturnStatus(CallbackServerContext* context,
39                                       const SetReturnStatusRequest* request,
40                                       Empty* /* reply */) override;
41 
42   ServerUnaryReactor* ClearReturnStatus(CallbackServerContext* context,
43                                         const Empty* request,
44                                         Empty* /* reply */) override;
45 
46   void AddReturnStatus(const Status& status);
47 
48   bool TestOnlyExpectRequests(size_t expected_requests_count,
49                               const absl::Duration& timeout);
50 
51   void Stop();
52 
53  private:
54   void MatchRequestsAndStatuses() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
55 
56   grpc_core::Mutex mu_;
57   grpc_core::CondVar request_var_ ABSL_GUARDED_BY(&mu_);
58   std::vector<ServerUnaryReactor*> pending_requests_ ABSL_GUARDED_BY(&mu_);
59   std::vector<Status> pending_statuses_ ABSL_GUARDED_BY(&mu_);
60   absl::optional<Status> respond_all_status_ ABSL_GUARDED_BY(&mu_);
61 };
62 
63 // Implementation of the pre-stop hook server. An instance is created to start
64 // a server and destroyed to stop one.
65 class PreStopHookServer;
66 
67 // Interface for interacting with PreStopHookServer. Provides operations
68 // required by the protocol, such as start, stop and return from the call.
69 class PreStopHookServerManager {
70  public:
71   Status Start(int port, size_t timeout_s);
72   Status Stop();
73   void Return(StatusCode code, absl::string_view description);
74   // Suspends the thread until there are pending requests. Returns false
75   // if the necessary number of requests have not been received before the
76   // timeout.
77   bool TestOnlyExpectRequests(
78       size_t expected_requests_count,
79       const absl::Duration& timeout = absl::Seconds(15));
80 
81  private:
82   // Custom deleter so we don't have to include PreStopHookServer in this header
83   struct PreStopHookServerDeleter {
84     void operator()(PreStopHookServer* server);
85   };
86 
87   std::unique_ptr<PreStopHookServer, PreStopHookServerDeleter> server_;
88 };
89 
90 }  // namespace testing
91 }  // namespace grpc
92 #endif  // GRPC_TEST_CPP_INTEROP_PRE_STOP_HOOK_SERVER_H
93