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