Debian Codegraph @

test/core/resource_quota/call_checker.h

raw
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
15 #ifndef LOCAL_GOOGLE_HOME_CTILLER_GRPC_TEST_CORE_RESOURCE_QUOTA_CALL_CHECKER_H_
16 #define LOCAL_GOOGLE_HOME_CTILLER_GRPC_TEST_CORE_RESOURCE_QUOTA_CALL_CHECKER_H_
17
18 #include <memory>
19
20 #include <grpc/support/log.h>
21
22 namespace grpc_core {
23 namespace testing {
24
25 // Utility to help check a function is called.
26 // Usage:
27 // auto checker = CallChecker::Make();
28 // auto f = [checker]() {
29 // checker.Called();
30 // };
31 // Will crash if: f never called, or f called more than once.
32 class CallChecker {
33 public:
34 explicit CallChecker(bool optional) : optional_(optional) {}
35 ~CallChecker() {
36 if (!optional_) GPR_ASSERT(called_);
37 }
38
39 void Called() {
40 GPR_ASSERT(!called_);
41 called_ = true;
42 }
43
44 static std::shared_ptr<CallChecker> Make() {
45 return std::make_shared<CallChecker>(false);
46 }
47
48 static std::shared_ptr<CallChecker> MakeOptional() {
49 return std::make_shared<CallChecker>(true);
50 }
51
52 private:
53 bool called_ = false;
54 const bool optional_ = false;
55 };
56
57 } // namespace testing
58 } // namespace grpc_core
59
60 #endif // LOCAL_GOOGLE_HOME_CTILLER_GRPC_TEST_CORE_RESOURCE_QUOTA_CALL_CHECKER_H_