2019-10-26 21:57:36 -07:00
|
|
|
#![cfg(test)]
|
|
|
|
|
2019-11-04 23:29:46 -08:00
|
|
|
use fake_instant::FakeClock as Instant;
|
2019-10-26 21:57:36 -07:00
|
|
|
|
2019-10-27 00:42:28 -07:00
|
|
|
/// This is a hack to test token bucket, substituting FakeClock for Instant.
|
2019-10-26 21:57:36 -07:00
|
|
|
mod token_bucket {
|
|
|
|
include!("token_bucket.rs");
|
|
|
|
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-11-02 20:55:07 -07:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
pub static ref D00: Duration = Duration::new(0, 0);
|
|
|
|
}
|
2019-10-26 21:57:36 -07:00
|
|
|
|
|
|
|
#[test]
|
2019-10-27 00:42:28 -07:00
|
|
|
fn test_basic() {
|
|
|
|
Instant::set_time(50_000);
|
2019-11-02 20:55:07 -07:00
|
|
|
let bucket = VectorTokenBucket::new(Duration::from_millis(1000), 100, *D00, 0.95);
|
2019-10-27 00:42:28 -07:00
|
|
|
assert!(bucket.get_tokens(50), "Should have not violated limit.");
|
|
|
|
assert_eq!(None, bucket.get_delay(), "Can get stuff.");
|
|
|
|
assert!(!bucket.get_tokens(51), "Should have violated limit.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_internal_constructor() {
|
2019-11-02 20:55:07 -07:00
|
|
|
let bucket = VectorTokenBucket::new(Duration::from_millis(1000), 100, *D00, 1.0);
|
2019-10-27 00:42:28 -07:00
|
|
|
assert_eq!(100, bucket.burst_limit);
|
|
|
|
|
2019-11-02 20:55:07 -07:00
|
|
|
let bucket = VectorTokenBucket::new(Duration::from_millis(1000), 100, *D00, 1e-6);
|
2019-10-27 00:42:28 -07:00
|
|
|
assert_eq!(1, bucket.burst_limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_saturated_100_burst() {
|
2019-11-02 20:55:07 -07:00
|
|
|
let bucket = VectorTokenBucket::new(Duration::from_millis(1000), 100, *D00, 1.00);
|
2019-10-27 00:42:28 -07:00
|
|
|
|
|
|
|
Instant::set_time(50_000);
|
|
|
|
assert!(bucket.get_tokens(100), "All tokens should be immediately available.");
|
2019-10-30 13:24:07 -07:00
|
|
|
assert_ne!(None, bucket.get_delay(), "Bucket should have delay.");
|
2019-10-27 00:42:28 -07:00
|
|
|
|
|
|
|
Instant::advance_time(1001); // Extra buffer for Duration(0).
|
|
|
|
assert!(bucket.get_tokens(100), "All tokens should be available after a bucket duration.");
|
2019-10-30 13:24:07 -07:00
|
|
|
assert_ne!(None, bucket.get_delay(), "Bucket should have delay.");
|
2019-10-27 00:42:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_saturated_95_burst() {
|
2019-11-02 20:55:07 -07:00
|
|
|
let bucket = VectorTokenBucket::new(Duration::from_millis(1000), 100, *D00, 0.50);
|
2019-10-27 00:42:28 -07:00
|
|
|
|
|
|
|
Instant::set_time(50_000);
|
|
|
|
assert!(bucket.get_tokens(95), "95 tokens should be immediately available.");
|
2019-10-30 13:24:07 -07:00
|
|
|
assert_ne!(None, bucket.get_delay(), "Bucket should have delay.");
|
2019-10-27 00:42:28 -07:00
|
|
|
|
|
|
|
Instant::advance_time(475); // Total 951.
|
2019-10-30 13:24:07 -07:00
|
|
|
assert_ne!(None, bucket.get_delay(), "Bucket should have delay.");
|
2019-10-27 00:42:28 -07:00
|
|
|
|
|
|
|
Instant::advance_time(476); // Extra buffer for Duration(0).
|
|
|
|
assert!(bucket.get_tokens(5), "Last 5 tokens should be available.");
|
2019-10-30 13:24:07 -07:00
|
|
|
assert_ne!(None, bucket.get_delay(), "Bucket should have delay.");
|
2019-10-27 00:42:28 -07:00
|
|
|
|
|
|
|
Instant::advance_time(51);
|
|
|
|
assert!(bucket.get_tokens(95), "95 tokens should be available.");
|
2019-10-30 13:24:07 -07:00
|
|
|
assert_ne!(None, bucket.get_delay(), "Bucket should have delay.");
|
2019-10-27 00:42:28 -07:00
|
|
|
|
|
|
|
Instant::advance_time(951);
|
2019-10-30 13:24:07 -07:00
|
|
|
assert!(bucket.get_tokens(5), "Last 5 tokens should be available.");
|
|
|
|
assert_ne!(None, bucket.get_delay(), "Bucket should have delay.");
|
2019-10-27 00:42:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_saturated_50_burst() {
|
2019-11-02 20:55:07 -07:00
|
|
|
let bucket = VectorTokenBucket::new(Duration::from_millis(1000), 100, *D00, 0.5);
|
2019-10-27 00:42:28 -07:00
|
|
|
|
|
|
|
Instant::set_time(50_000);
|
|
|
|
assert!(bucket.get_tokens(50), "Half the tokens should be immediately available.");
|
2019-10-30 13:24:07 -07:00
|
|
|
assert_ne!(None, bucket.get_delay(), "Bucket should have delay.");
|
2019-10-27 00:42:28 -07:00
|
|
|
|
|
|
|
Instant::advance_time(501); // Extra buffer for Duration(0).
|
|
|
|
assert!(bucket.get_tokens(50), "Half the tokens should be available after a half bucket duration.");
|
2019-10-30 13:24:07 -07:00
|
|
|
assert_ne!(None, bucket.get_delay(), "Bucket should have delay.");
|
2019-10-27 00:42:28 -07:00
|
|
|
|
|
|
|
Instant::advance_time(501);
|
|
|
|
assert!(bucket.get_tokens(50), "Half the tokens should be available after a full bucket duration.");
|
2019-10-30 13:24:07 -07:00
|
|
|
assert_ne!(None, bucket.get_delay(), "Bucket should have delay.");
|
2019-10-27 00:42:28 -07:00
|
|
|
|
|
|
|
Instant::advance_time(501);
|
|
|
|
assert!(bucket.get_tokens(50));
|
2019-10-30 13:24:07 -07:00
|
|
|
assert_ne!(None, bucket.get_delay(), "Bucket should have delay.");
|
2019-10-27 00:42:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_many() {
|
|
|
|
Instant::set_time(50_000);
|
2019-11-02 20:55:07 -07:00
|
|
|
let bucket = VectorTokenBucket::new(Duration::from_millis(1000), 100, *D00, 0.95);
|
2019-10-27 00:42:28 -07:00
|
|
|
assert!(bucket.get_tokens(50), "Should have not violated limit.");
|
|
|
|
assert_eq!(None, bucket.get_delay(), "Should not be blocked.");
|
|
|
|
for _ in 0..20_000 {
|
|
|
|
Instant::advance_time(501);
|
|
|
|
assert!(bucket.get_tokens(50), "Should have not violated limit.");
|
2019-10-30 13:24:07 -07:00
|
|
|
assert_ne!(None, bucket.get_delay(), "Bucket should have delay.");
|
2019-10-27 00:42:28 -07:00
|
|
|
Instant::advance_time(501);
|
|
|
|
assert!(bucket.get_tokens(50), "Should have not violated limit.");
|
2019-10-30 13:24:07 -07:00
|
|
|
assert_ne!(None, bucket.get_delay(), "Bucket should have delay.");
|
2019-10-27 00:42:28 -07:00
|
|
|
}
|
2019-10-30 13:24:07 -07:00
|
|
|
assert!(bucket.timestamps.lock().len() < 110, "Check memory leak.");
|
2019-10-26 21:57:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|