xref: /aosp_15_r20/external/grpc-grpc/test/cpp/end2end/counted_service.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 // Copyright 2017 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_TEST_CPP_END2END_COUNTED_SERVICE_H
18 #define GRPC_TEST_CPP_END2END_COUNTED_SERVICE_H
19 
20 #include "src/core/lib/gprpp/sync.h"
21 
22 namespace grpc {
23 namespace testing {
24 
25 // A wrapper around an RPC service implementation that provides request and
26 // response counting.
27 template <typename ServiceType>
28 class CountedService : public ServiceType {
29  public:
request_count()30   size_t request_count() {
31     grpc_core::MutexLock lock(&mu_);
32     return request_count_;
33   }
34 
response_count()35   size_t response_count() {
36     grpc_core::MutexLock lock(&mu_);
37     return response_count_;
38   }
39 
IncreaseResponseCount()40   void IncreaseResponseCount() {
41     grpc_core::MutexLock lock(&mu_);
42     ++response_count_;
43   }
IncreaseRequestCount()44   void IncreaseRequestCount() {
45     grpc_core::MutexLock lock(&mu_);
46     ++request_count_;
47   }
48 
ResetCounters()49   void ResetCounters() {
50     grpc_core::MutexLock lock(&mu_);
51     request_count_ = 0;
52     response_count_ = 0;
53   }
54 
55  private:
56   grpc_core::Mutex mu_;
57   size_t request_count_ ABSL_GUARDED_BY(mu_) = 0;
58   size_t response_count_ ABSL_GUARDED_BY(mu_) = 0;
59 };
60 
61 }  // namespace testing
62 }  // namespace grpc
63 
64 #endif  // GRPC_TEST_CPP_END2END_COUNTED_SERVICE_H
65