• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Abseil 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 //     https://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 ABSL_STRINGS_CORDZ_TEST_HELPERS_H_
16 #define ABSL_STRINGS_CORDZ_TEST_HELPERS_H_
17 
18 #include <utility>
19 
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "absl/base/config.h"
23 #include "absl/base/macros.h"
24 #include "absl/base/nullability.h"
25 #include "absl/strings/cord.h"
26 #include "absl/strings/internal/cord_internal.h"
27 #include "absl/strings/internal/cordz_info.h"
28 #include "absl/strings/internal/cordz_sample_token.h"
29 #include "absl/strings/internal/cordz_statistics.h"
30 #include "absl/strings/internal/cordz_update_tracker.h"
31 #include "absl/strings/str_cat.h"
32 
33 namespace absl {
34 ABSL_NAMESPACE_BEGIN
35 
36 // Returns the CordzInfo for the cord, or nullptr if the cord is not sampled.
GetCordzInfoForTesting(const Cord & cord)37 inline absl::Nullable<const cord_internal::CordzInfo*> GetCordzInfoForTesting(
38     const Cord& cord) {
39   if (!cord.contents_.is_tree()) return nullptr;
40   return cord.contents_.cordz_info();
41 }
42 
43 // Returns true if the provided cordz_info is in the list of sampled cords.
44 inline bool CordzInfoIsListed(
45     absl::Nonnull<const cord_internal::CordzInfo*> cordz_info,
46     cord_internal::CordzSampleToken token = {}) {
47   for (const cord_internal::CordzInfo& info : token) {
48     if (cordz_info == &info) return true;
49   }
50   return false;
51 }
52 
53 // Matcher on Cord that verifies all of:
54 // - the cord is sampled
55 // - the CordzInfo of the cord is listed / discoverable.
56 // - the reported CordzStatistics match the cord's actual properties
57 // - the cord has an (initial) UpdateTracker count of 1 for `method`
58 MATCHER_P(HasValidCordzInfoOf, method, "CordzInfo matches cord") {
59   const cord_internal::CordzInfo* cord_info = GetCordzInfoForTesting(arg);
60   if (cord_info == nullptr) {
61     *result_listener << "cord is not sampled";
62     return false;
63   }
64   if (!CordzInfoIsListed(cord_info)) {
65     *result_listener << "cord is sampled, but not listed";
66     return false;
67   }
68   cord_internal::CordzStatistics stat = cord_info->GetCordzStatistics();
69   if (stat.size != arg.size()) {
70     *result_listener << "cordz size " << stat.size
71                      << " does not match cord size " << arg.size();
72     return false;
73   }
74   if (stat.update_tracker.Value(method) != 1) {
75     *result_listener << "Expected method count 1 for " << method << ", found "
76                      << stat.update_tracker.Value(method);
77     return false;
78   }
79   return true;
80 }
81 
82 // Matcher on Cord that verifies that the cord is sampled and that the CordzInfo
83 // update tracker has 'method' with a call count of 'n'
84 MATCHER_P2(CordzMethodCountEq, method, n,
85            absl::StrCat("CordzInfo method count equals ", n)) {
86   const cord_internal::CordzInfo* cord_info = GetCordzInfoForTesting(arg);
87   if (cord_info == nullptr) {
88     *result_listener << "cord is not sampled";
89     return false;
90   }
91   cord_internal::CordzStatistics stat = cord_info->GetCordzStatistics();
92   if (stat.update_tracker.Value(method) != n) {
93     *result_listener << "Expected method count " << n << " for " << method
94                      << ", found " << stat.update_tracker.Value(method);
95     return false;
96   }
97   return true;
98 }
99 
100 // Cordz will only update with a new rate once the previously scheduled event
101 // has fired. When we disable Cordz, a long delay takes place where we won't
102 // consider profiling new Cords. CordzSampleIntervalHelper will burn through
103 // that interval and allow for testing that assumes that the average sampling
104 // interval is a particular value.
105 class CordzSamplingIntervalHelper {
106  public:
CordzSamplingIntervalHelper(int32_t interval)107   explicit CordzSamplingIntervalHelper(int32_t interval)
108       : orig_mean_interval_(absl::cord_internal::get_cordz_mean_interval()) {
109     absl::cord_internal::set_cordz_mean_interval(interval);
110     absl::cord_internal::cordz_set_next_sample_for_testing(interval);
111   }
112 
~CordzSamplingIntervalHelper()113   ~CordzSamplingIntervalHelper() {
114     absl::cord_internal::set_cordz_mean_interval(orig_mean_interval_);
115     absl::cord_internal::cordz_set_next_sample_for_testing(orig_mean_interval_);
116   }
117 
118  private:
119   int32_t orig_mean_interval_;
120 };
121 
122 // Wrapper struct managing a small CordRep `rep`
123 struct TestCordRep {
124   absl::Nonnull<cord_internal::CordRepFlat*> rep;
125 
TestCordRepTestCordRep126   TestCordRep() {
127     rep = cord_internal::CordRepFlat::New(100);
128     rep->length = 100;
129     memset(rep->Data(), 1, 100);
130   }
~TestCordRepTestCordRep131   ~TestCordRep() { cord_internal::CordRep::Unref(rep); }
132 };
133 
134 // Wrapper struct managing a small CordRep `rep`, and
135 // an InlineData `data` initialized with that CordRep.
136 struct TestCordData {
137   TestCordRep rep;
138   cord_internal::InlineData data{rep.rep};
139 };
140 
141 // Creates a Cord that is not sampled
142 template <typename... Args>
UnsampledCord(Args...args)143 Cord UnsampledCord(Args... args) {
144   CordzSamplingIntervalHelper never(9999);
145   Cord cord(std::forward<Args>(args)...);
146   ABSL_ASSERT(GetCordzInfoForTesting(cord) == nullptr);
147   return cord;
148 }
149 
150 ABSL_NAMESPACE_END
151 }  // namespace absl
152 
153 #endif  // ABSL_STRINGS_CORDZ_TEST_HELPERS_H_
154