29 lines
805 B
Rust
29 lines
805 B
Rust
|
|
//! Incoming rend accepts: 30 / 60s, burst 10.
|
||
|
|
|
||
|
|
use std::time::{Duration, Instant};
|
||
|
|
|
||
|
|
use onionwire::ratelimit::TokenBucket;
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn burst_allows_then_denies() {
|
||
|
|
let t0 = Instant::now();
|
||
|
|
let mut b = TokenBucket::new(30, Duration::from_secs(60), 10);
|
||
|
|
for _ in 0..10 {
|
||
|
|
assert!(b.try_acquire_at(t0), "burst of 10 must pass");
|
||
|
|
}
|
||
|
|
assert!(!b.try_acquire_at(t0), "11th in the burst must drop");
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn refill_one_token_after_two_seconds() {
|
||
|
|
let t0 = Instant::now();
|
||
|
|
let mut b = TokenBucket::new(30, Duration::from_secs(60), 10);
|
||
|
|
for _ in 0..10 {
|
||
|
|
assert!(b.try_acquire_at(t0));
|
||
|
|
}
|
||
|
|
// 30 tokens / 60s = 0.5/s → 2s yields one token.
|
||
|
|
let t1 = t0 + Duration::from_secs(2);
|
||
|
|
assert!(b.try_acquire_at(t1));
|
||
|
|
assert!(!b.try_acquire_at(t1));
|
||
|
|
}
|