call_checker.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2021 gRPC authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef LOCAL_GOOGLE_HOME_CTILLER_GRPC_TEST_CORE_RESOURCE_QUOTA_CALL_CHECKER_H_
  15. #define LOCAL_GOOGLE_HOME_CTILLER_GRPC_TEST_CORE_RESOURCE_QUOTA_CALL_CHECKER_H_
  16. #include <memory>
  17. #include <grpc/support/log.h>
  18. namespace grpc_core {
  19. namespace testing {
  20. // Utility to help check a function is called.
  21. // Usage:
  22. // auto checker = CallChecker::Make();
  23. // auto f = [checker]() {
  24. // checker.Called();
  25. // };
  26. // Will crash if: f never called, or f called more than once.
  27. class CallChecker {
  28. public:
  29. explicit CallChecker(bool optional) : optional_(optional) {}
  30. ~CallChecker() {
  31. if (!optional_) GPR_ASSERT(called_);
  32. }
  33. void Called() {
  34. GPR_ASSERT(!called_);
  35. called_ = true;
  36. }
  37. static std::shared_ptr<CallChecker> Make() {
  38. return std::make_shared<CallChecker>(false);
  39. }
  40. static std::shared_ptr<CallChecker> MakeOptional() {
  41. return std::make_shared<CallChecker>(true);
  42. }
  43. private:
  44. bool called_ = false;
  45. const bool optional_ = false;
  46. };
  47. } // namespace testing
  48. } // namespace grpc_core
  49. #endif // LOCAL_GOOGLE_HOME_CTILLER_GRPC_TEST_CORE_RESOURCE_QUOTA_CALL_CHECKER_H_