File indexing completed on 2025-12-16 09:41:02
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
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
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
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
0054
0055
0056
0057
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
0083
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
0101
0102
0103
0104
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
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
0135
0136 struct TestCordData {
0137 TestCordRep rep;
0138 cord_internal::InlineData data{rep.rep};
0139 };
0140
0141
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 }
0152
0153 #endif