use leaky_bucket_lite::{sync_threadsafe::LeakyBucket as SyncLeakyBucket, LeakyBucket};

use std::time::Duration;

#[tokio::test]
async fn test_tokens() {
    let rate_limiter = LeakyBucket::builder()
        .max(5.0)
        .tokens(5.0)
        .refill_amount(1.0)
        .refill_interval(Duration::from_millis(100))
        .build();

    assert_eq!(rate_limiter.tokens().await.expect("No reason to fail"), 5.0);

    for i in 0..5 {
        assert_eq!(
            rate_limiter
                .tokens()
                .await
                .expect("No reason to fail")
                .round(),
            5.0 - i as f64
        );
        rate_limiter.acquire_one().await.expect("No reason to fail");
    }

    assert_eq!(
        rate_limiter
            .tokens()
            .await
            .expect("No reason to fail")
            .round(),
        0.0
    );
    rate_limiter.acquire_one().await.expect("No reason to fail");
    assert_eq!(
        rate_limiter
            .tokens()
            .await
            .expect("No reason to fail")
            .round(),
        0.0
    );
}

#[test]
fn test_tokens_sync_threadsafe() {
    let rate_limiter = SyncLeakyBucket::builder()
        .max(5.0)
        .tokens(5.0)
        .refill_amount(1.0)
        .refill_interval(Duration::from_millis(100))
        .build();

    assert_eq!(rate_limiter.tokens(), 5.0);

    for i in 0..5 {
        assert_eq!(rate_limiter.tokens().round(), 5.0 - i as f64);
        rate_limiter.acquire_one();
    }

    assert_eq!(rate_limiter.tokens().round(), 0.0);
    rate_limiter.acquire_one();
    assert_eq!(rate_limiter.tokens().round(), 0.0);
}
