use parking_lot::Mutex;
use std::collections::VecDeque;
use crate::core::constants::RESULT_RING_CAPACITY;
use crate::core::result::ExperimentResult;
pub struct ResultLog {
inner: Mutex<Inner>,
capacity: usize,
}
struct Inner {
buf: VecDeque<ExperimentResult>,
dropped: u64,
}
impl ResultLog {
pub fn new(capacity: usize) -> Self {
Self {
inner: Mutex::new(Inner {
buf: VecDeque::with_capacity(capacity.min(1_024)),
dropped: 0,
}),
capacity: capacity.max(1),
}
}
pub fn push(&self, result: ExperimentResult) {
let mut inner = self.inner.lock();
if inner.buf.len() == self.capacity {
inner.buf.pop_front();
inner.dropped += 1;
}
inner.buf.push_back(result);
}
pub fn to_vec(&self) -> Vec<ExperimentResult> {
self.inner.lock().buf.iter().cloned().collect()
}
pub fn len(&self) -> usize {
self.inner.lock().buf.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn capacity(&self) -> usize {
self.capacity
}
pub fn dropped_from_front(&self) -> u64 {
self.inner.lock().dropped
}
pub fn clear(&self) {
let mut inner = self.inner.lock();
inner.buf.clear();
inner.dropped = 0;
}
}
impl Default for ResultLog {
fn default() -> Self {
Self::new(RESULT_RING_CAPACITY)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::result::ExperimentResult;
use chrono::Utc;
use std::time::Duration;
fn result(status: u16) -> ExperimentResult {
ExperimentResult {
timestamp: Utc::now(),
latency: Duration::from_millis(1),
service_time: Duration::from_millis(1),
queue_wait: Duration::ZERO,
status,
success: status < 400,
bytes: 0,
user_id: "u".into(),
query: "custom".into(),
error: None,
response_body: None,
}
}
#[test]
fn empty_log_reports_empty() {
let log = ResultLog::new(8);
assert!(log.is_empty());
assert_eq!(log.len(), 0);
assert_eq!(log.dropped_from_front(), 0);
}
#[test]
fn retains_up_to_capacity_without_dropping() {
let log = ResultLog::new(4);
for i in 0..4 {
log.push(result(200 + i as u16));
}
assert_eq!(log.len(), 4);
assert_eq!(log.dropped_from_front(), 0);
}
#[test]
fn evicts_oldest_and_counts_drops_at_capacity() {
let log = ResultLog::new(3);
for i in 0..10 {
log.push(result(200 + i as u16));
}
assert_eq!(log.len(), 3, "buffer must never exceed capacity");
assert_eq!(log.dropped_from_front(), 7);
}
#[test]
fn retains_most_recent_results_after_eviction() {
let log = ResultLog::new(3);
for i in 0..10 {
log.push(result(200 + i as u16));
}
let retained = log.to_vec();
let statuses: Vec<u16> = retained.iter().map(|r| r.status).collect();
assert_eq!(
statuses,
vec![207, 208, 209],
"must keep newest, drop oldest"
);
}
#[test]
fn memory_is_bounded_under_sustained_load() {
let log = ResultLog::new(16);
for i in 0..100_000 {
log.push(result(200 + (i % 100) as u16));
}
assert_eq!(log.len(), 16);
assert_eq!(log.capacity(), 16);
assert_eq!(log.dropped_from_front(), 99_984);
}
#[test]
fn clear_resets_buffer_and_drop_count() {
let log = ResultLog::new(2);
for i in 0..5 {
log.push(result(200 + i as u16));
}
log.clear();
assert!(log.is_empty());
assert_eq!(log.dropped_from_front(), 0);
}
#[test]
fn capacity_is_at_least_one() {
let log = ResultLog::new(0);
log.push(result(200));
assert_eq!(
log.len(),
1,
"zero capacity must not panic or drop everything"
);
}
#[test]
fn zero_capacity_retains_one_and_counts_every_eviction() {
let log = ResultLog::new(0);
for _ in 0..5 {
log.push(result(200));
}
assert_eq!(log.len(), 1);
assert_eq!(log.capacity(), 1);
assert_eq!(
log.dropped_from_front(),
4,
"a zero-capacity request must still account for what it discarded, \
or the report writer is told a partial view is a complete one"
);
}
#[test]
fn capacity_beyond_the_initial_allocation_grows_without_dropping() {
let log = ResultLog::new(2_000);
for i in 0..2_000 {
log.push(result(200 + (i % 100) as u16));
}
assert_eq!(
log.len(),
2_000,
"a capacity above 1,024 must still be honoured"
);
assert_eq!(log.dropped_from_front(), 0);
}
#[test]
fn a_fresh_log_copies_to_an_empty_vec() {
assert!(ResultLog::new(4).to_vec().is_empty());
}
}