Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/common/stats/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ envoy_cc_test(
name = "stats_impl_test",
srcs = ["stats_impl_test.cc"],
deps = [
"//source/common/common:hex_lib",
"//source/common/stats:stats_lib",
"//test/mocks/stats:stats_mocks",
"//test/test_common:logging_lib",
Expand Down
27 changes: 27 additions & 0 deletions test/common/stats/stats_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "envoy/config/metrics/v2/stats.pb.h"
#include "envoy/stats/stats_macros.h"

#include "common/common/hex.h"
#include "common/config/well_known_names.h"
#include "common/stats/stats_impl.h"

Expand Down Expand Up @@ -477,6 +478,32 @@ TEST(TagProducerTest, CheckConstructor) {
"No regex specified for tag specifier and no default regex for name: 'test_extractor'");
}

// Check consistency of internal stat representation
TEST(RawStatDataTest, Consistency) {
HeapRawStatDataAllocator alloc;
// Generate a stat, encode it to hex, and take the hash of that hex string. We expect the hash to
// vary only when the internal representation of a stat has been intentionally changed, in which
// case SharedMemory::VERSION should be incremented as well.
uint64_t expected_hash = 1874506077228772558;
uint64_t max_name_length = RawStatData::maxNameLength();

const std::string name_1(max_name_length, 'A');
RawStatData* stat_1 = alloc.alloc(name_1);
std::string stat_hex_dump_1 =
Hex::encode(reinterpret_cast<uint8_t*>(stat_1), sizeof(RawStatData) + max_name_length);
EXPECT_EQ(HashUtil::xxHash64(stat_hex_dump_1), expected_hash);
alloc.free(*stat_1);

// If a stat name is truncated, we expect that its internal representation is the same as if it
// had been initialized with the already-truncated name.
const std::string name_2(max_name_length + 1, 'A');
RawStatData* stat_2 = alloc.alloc(name_2);
std::string stat_hex_dump_2 =
Hex::encode(reinterpret_cast<uint8_t*>(stat_2), sizeof(RawStatData) + max_name_length);
EXPECT_EQ(HashUtil::xxHash64(stat_hex_dump_2), expected_hash);
alloc.free(*stat_2);
}

// Validate truncation behavior of RawStatData.
TEST(RawStatDataTest, Truncate) {
HeapRawStatDataAllocator alloc;
Expand Down