Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
yashykt committed Nov 12, 2024
1 parent c1e4af6 commit bdd2367
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/core/telemetry/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ class GlobalStatsPluginRegistry {
return false;
}

size_t size() const { return plugins_state_.size(); }

// Registers a callback to be used to populate callback metrics.
// The callback will update the specified metrics. The callback
// will be invoked no more often than min_interval. Multiple callbacks may
Expand Down
33 changes: 33 additions & 0 deletions test/core/telemetry/metrics_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "src/core/telemetry/metrics.h"

#include <memory>
#include <thread>

#include "absl/log/log.h"
#include "gmock/gmock.h"
Expand Down Expand Up @@ -648,6 +649,38 @@ TEST_F(MetricsTest, FindInstrumentByName) {
::testing::Eq(uint64_counter_handle.index))));
}

TEST_F(MetricsTest, ParallelStatsPluginRegistrationAndLookup) {
std::vector<std::thread> register_threads;
std::vector<std::thread> lookup_threads;
register_threads.reserve(100);
lookup_threads.reserve(100);
// 100 threads that register 100 stats plugins each
for (int i = 0; i < 100; ++i) {
register_threads.emplace_back([] {
for (int j = 0; j < 100; ++j) {
grpc_core::FakeStatsPluginBuilder().BuildAndRegister();
}
});
}
// 100 threads that keep looking up stats plugins till they see 10000 stats
// plugins
for (int i = 0; i < 100; ++i) {
lookup_threads.emplace_back([this] {
while (GlobalStatsPluginRegistry::GetStatsPluginsForChannel(
StatsPluginChannelScope("", "", endpoint_config_))
.size() < 10000) {
};
});
}
for (int i = 0; i < 100; ++i) {
register_threads[i].join();
lookup_threads[i].join();
}
EXPECT_THAT(GlobalStatsPluginRegistry::GetStatsPluginsForChannel(
StatsPluginChannelScope("", "", endpoint_config_)),
::testing::SizeIs(10000));
}

using MetricsDeathTest = MetricsTest;

TEST_F(MetricsDeathTest, RegisterTheSameMetricNameWouldCrash) {
Expand Down

0 comments on commit bdd2367

Please sign in to comment.