counted_service.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #ifndef GRPC_TEST_CPP_END2END_COUNTED_SERVICE_H
  17. #define GRPC_TEST_CPP_END2END_COUNTED_SERVICE_H
  18. #include "src/core/lib/gprpp/sync.h"
  19. namespace grpc {
  20. namespace testing {
  21. // A wrapper around an RPC service implementation that provides request and
  22. // response counting.
  23. template <typename ServiceType>
  24. class CountedService : public ServiceType {
  25. public:
  26. size_t request_count() {
  27. grpc_core::MutexLock lock(&mu_);
  28. return request_count_;
  29. }
  30. size_t response_count() {
  31. grpc_core::MutexLock lock(&mu_);
  32. return response_count_;
  33. }
  34. void IncreaseResponseCount() {
  35. grpc_core::MutexLock lock(&mu_);
  36. ++response_count_;
  37. }
  38. void IncreaseRequestCount() {
  39. grpc_core::MutexLock lock(&mu_);
  40. ++request_count_;
  41. }
  42. void ResetCounters() {
  43. grpc_core::MutexLock lock(&mu_);
  44. request_count_ = 0;
  45. response_count_ = 0;
  46. }
  47. private:
  48. grpc_core::Mutex mu_;
  49. size_t request_count_ ABSL_GUARDED_BY(mu_) = 0;
  50. size_t response_count_ ABSL_GUARDED_BY(mu_) = 0;
  51. };
  52. } // namespace testing
  53. } // namespace grpc
  54. #endif // GRPC_TEST_CPP_END2END_COUNTED_SERVICE_H