93 lines
3.3 KiB
C++
93 lines
3.3 KiB
C++
#include <Void/RPC/Observations.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
using namespace Void::RPC;
|
|
|
|
RpcObservation event(RpcObservationKind kind, std::size_t bytes = 0)
|
|
{
|
|
return RpcObservation{.kind = kind, .bytes = bytes};
|
|
}
|
|
} // namespace
|
|
|
|
Void::Testing::Test rpc_observations_ring_is_bounded_and_counted()
|
|
{
|
|
RpcObservations observations{RpcObservationLimits{.max_events = 4}};
|
|
Void::Testing::expect_eq(observations.capacity(), std::size_t{4});
|
|
|
|
for (int i = 0; i < 6; ++i) {
|
|
observations.record(event(RpcObservationKind::CallStarted));
|
|
}
|
|
Void::Testing::expect_eq(observations.size(), std::size_t{4});
|
|
Void::Testing::expect_eq(observations.dropped(), std::size_t{2});
|
|
Void::Testing::expect_eq(observations.total_recorded(), std::uint64_t{6});
|
|
|
|
// Drain preserves arrival order and clears the ring.
|
|
const auto drained = observations.drain();
|
|
Void::Testing::expect_eq(drained.size(), std::size_t{4});
|
|
for (const auto& entry : drained) {
|
|
Void::Testing::expect(entry.kind == RpcObservationKind::CallStarted);
|
|
}
|
|
Void::Testing::expect_eq(observations.size(), std::size_t{0});
|
|
Void::Testing::expect(observations.drain().empty());
|
|
|
|
// Dropped is cumulative across drains.
|
|
Void::Testing::expect_eq(observations.dropped(), std::size_t{2});
|
|
}
|
|
|
|
Void::Testing::Test rpc_observations_zero_capacity_drops_everything()
|
|
{
|
|
RpcObservations observations{RpcObservationLimits{.max_events = 0}};
|
|
observations.record(event(RpcObservationKind::StreamOpened));
|
|
Void::Testing::expect_eq(observations.size(), std::size_t{0});
|
|
Void::Testing::expect_eq(observations.dropped(), std::size_t{1});
|
|
Void::Testing::expect_eq(observations.total_recorded(), std::uint64_t{1});
|
|
}
|
|
|
|
Void::Testing::Test rpc_observations_aggregates_byte_totals()
|
|
{
|
|
RpcObservations observations;
|
|
observations.record(event(RpcObservationKind::BytesSent, 10));
|
|
observations.record(event(RpcObservationKind::BytesReceived, 5));
|
|
observations.record(event(RpcObservationKind::CallStarted));
|
|
// Non-byte observations do not contribute even if they carry a size.
|
|
observations.record(event(RpcObservationKind::Error, 100));
|
|
Void::Testing::expect_eq(observations.total_bytes(), std::uint64_t{15});
|
|
Void::Testing::expect_eq(observations.total_recorded(), std::uint64_t{4});
|
|
}
|
|
|
|
Void::Testing::Test rpc_observations_concurrent_producers_stay_bounded()
|
|
{
|
|
constexpr std::size_t kCapacity = 64;
|
|
constexpr int kThreads = 4;
|
|
constexpr int kPerThread = 500;
|
|
RpcObservations observations{RpcObservationLimits{.max_events = kCapacity}};
|
|
|
|
std::vector<std::thread> producers;
|
|
for (int t = 0; t < kThreads; ++t) {
|
|
producers.emplace_back([&observations] {
|
|
for (int i = 0; i < kPerThread; ++i) {
|
|
observations.record(event(RpcObservationKind::CallFinished));
|
|
}
|
|
});
|
|
}
|
|
for (auto& producer : producers) {
|
|
producer.join();
|
|
}
|
|
|
|
const std::uint64_t total = static_cast<std::uint64_t>(kThreads) *
|
|
static_cast<std::uint64_t>(kPerThread);
|
|
Void::Testing::expect_eq(observations.total_recorded(), total);
|
|
Void::Testing::expect(observations.size() <= kCapacity);
|
|
Void::Testing::expect_eq(observations.size() + observations.dropped(),
|
|
static_cast<std::size_t>(total));
|
|
const std::size_t retained = observations.size();
|
|
Void::Testing::expect_eq(observations.drain().size(), retained);
|
|
}
|