Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:41:02

0001 // Copyright 2021 The Abseil Authors
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //     https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_STRINGS_CORDZ_TEST_HELPERS_H_
0016 #define ABSL_STRINGS_CORDZ_TEST_HELPERS_H_
0017 
0018 #include <utility>
0019 
0020 #include "gmock/gmock.h"
0021 #include "gtest/gtest.h"
0022 #include "absl/base/config.h"
0023 #include "absl/base/macros.h"
0024 #include "absl/base/nullability.h"
0025 #include "absl/strings/cord.h"
0026 #include "absl/strings/internal/cord_internal.h"
0027 #include "absl/strings/internal/cordz_info.h"
0028 #include "absl/strings/internal/cordz_sample_token.h"
0029 #include "absl/strings/internal/cordz_statistics.h"
0030 #include "absl/strings/internal/cordz_update_tracker.h"
0031 #include "absl/strings/str_cat.h"
0032 
0033 namespace absl {
0034 ABSL_NAMESPACE_BEGIN
0035 
0036 // Returns the CordzInfo for the cord, or nullptr if the cord is not sampled.
0037 inline absl::Nullable<const cord_internal::CordzInfo*> GetCordzInfoForTesting(
0038     const Cord& cord) {
0039   if (!cord.contents_.is_tree()) return nullptr;
0040   return cord.contents_.cordz_info();
0041 }
0042 
0043 // Returns true if the provided cordz_info is in the list of sampled cords.
0044 inline bool CordzInfoIsListed(
0045     absl::Nonnull<const cord_internal::CordzInfo*> cordz_info,
0046     cord_internal::CordzSampleToken token = {}) {
0047   for (const cord_internal::CordzInfo& info : token) {
0048     if (cordz_info == &info) return true;
0049   }
0050   return false;
0051 }
0052 
0053 // Matcher on Cord that verifies all of:
0054 // - the cord is sampled
0055 // - the CordzInfo of the cord is listed / discoverable.
0056 // - the reported CordzStatistics match the cord's actual properties
0057 // - the cord has an (initial) UpdateTracker count of 1 for `method`
0058 MATCHER_P(HasValidCordzInfoOf, method, "CordzInfo matches cord") {
0059   const cord_internal::CordzInfo* cord_info = GetCordzInfoForTesting(arg);
0060   if (cord_info == nullptr) {
0061     *result_listener << "cord is not sampled";
0062     return false;
0063   }
0064   if (!CordzInfoIsListed(cord_info)) {
0065     *result_listener << "cord is sampled, but not listed";
0066     return false;
0067   }
0068   cord_internal::CordzStatistics stat = cord_info->GetCordzStatistics();
0069   if (stat.size != arg.size()) {
0070     *result_listener << "cordz size " << stat.size
0071                      << " does not match cord size " << arg.size();
0072     return false;
0073   }
0074   if (stat.update_tracker.Value(method) != 1) {
0075     *result_listener << "Expected method count 1 for " << method << ", found "
0076                      << stat.update_tracker.Value(method);
0077     return false;
0078   }
0079   return true;
0080 }
0081 
0082 // Matcher on Cord that verifies that the cord is sampled and that the CordzInfo
0083 // update tracker has 'method' with a call count of 'n'
0084 MATCHER_P2(CordzMethodCountEq, method, n,
0085            absl::StrCat("CordzInfo method count equals ", n)) {
0086   const cord_internal::CordzInfo* cord_info = GetCordzInfoForTesting(arg);
0087   if (cord_info == nullptr) {
0088     *result_listener << "cord is not sampled";
0089     return false;
0090   }
0091   cord_internal::CordzStatistics stat = cord_info->GetCordzStatistics();
0092   if (stat.update_tracker.Value(method) != n) {
0093     *result_listener << "Expected method count " << n << " for " << method
0094                      << ", found " << stat.update_tracker.Value(method);
0095     return false;
0096   }
0097   return true;
0098 }
0099 
0100 // Cordz will only update with a new rate once the previously scheduled event
0101 // has fired. When we disable Cordz, a long delay takes place where we won't
0102 // consider profiling new Cords. CordzSampleIntervalHelper will burn through
0103 // that interval and allow for testing that assumes that the average sampling
0104 // interval is a particular value.
0105 class CordzSamplingIntervalHelper {
0106  public:
0107   explicit CordzSamplingIntervalHelper(int32_t interval)
0108       : orig_mean_interval_(absl::cord_internal::get_cordz_mean_interval()) {
0109     absl::cord_internal::set_cordz_mean_interval(interval);
0110     absl::cord_internal::cordz_set_next_sample_for_testing(interval);
0111   }
0112 
0113   ~CordzSamplingIntervalHelper() {
0114     absl::cord_internal::set_cordz_mean_interval(orig_mean_interval_);
0115     absl::cord_internal::cordz_set_next_sample_for_testing(orig_mean_interval_);
0116   }
0117 
0118  private:
0119   int32_t orig_mean_interval_;
0120 };
0121 
0122 // Wrapper struct managing a small CordRep `rep`
0123 struct TestCordRep {
0124   absl::Nonnull<cord_internal::CordRepFlat*> rep;
0125 
0126   TestCordRep() {
0127     rep = cord_internal::CordRepFlat::New(100);
0128     rep->length = 100;
0129     memset(rep->Data(), 1, 100);
0130   }
0131   ~TestCordRep() { cord_internal::CordRep::Unref(rep); }
0132 };
0133 
0134 // Wrapper struct managing a small CordRep `rep`, and
0135 // an InlineData `data` initialized with that CordRep.
0136 struct TestCordData {
0137   TestCordRep rep;
0138   cord_internal::InlineData data{rep.rep};
0139 };
0140 
0141 // Creates a Cord that is not sampled
0142 template <typename... Args>
0143 Cord UnsampledCord(Args... args) {
0144   CordzSamplingIntervalHelper never(9999);
0145   Cord cord(std::forward<Args>(args)...);
0146   ABSL_ASSERT(GetCordzInfoForTesting(cord) == nullptr);
0147   return cord;
0148 }
0149 
0150 ABSL_NAMESPACE_END
0151 }  // namespace absl
0152 
0153 #endif  // ABSL_STRINGS_CORDZ_TEST_HELPERS_H_