Skip to content

Commit

Permalink
test cases improved
Browse files Browse the repository at this point in the history
  • Loading branch information
mindeng committed Aug 19, 2024
1 parent e249412 commit f216bf0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
22 changes: 14 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ mod tests {

let start = Instant::now();
rl.acquire().await;
assert!(start.elapsed() < Duration::from_millis(10));
rl.acquire().await;
assert!(start.elapsed() > Duration::from_millis(330));
assert!(start.elapsed() < Duration::from_millis(340));
rl.acquire().await;
assert!(start.elapsed() > Duration::from_millis(660));
assert!(start.elapsed() < Duration::from_millis(680));
rl.acquire().await;
assert!(start.elapsed() < Duration::from_millis(1010));

let res = rl.acquire_with_timeout(Duration::from_millis(5000)).await;
assert!(res);
Expand All @@ -79,11 +81,11 @@ mod tests {
"got: {:?}",
start.elapsed()
);
assert!(start.elapsed() < Duration::from_millis(1340));
assert!(start.elapsed() < Duration::from_millis(1030));

let res = rl.acquire_with_timeout(Duration::from_millis(10)).await;
assert!(!res);
assert!(start.elapsed() < Duration::from_millis(1360));
assert!(start.elapsed() < Duration::from_millis(1050));
}

#[tokio::test]
Expand All @@ -94,11 +96,13 @@ mod tests {

let start = Instant::now();
rl.acquire().await;
assert!(start.elapsed() < Duration::from_millis(10));
rl.acquire().await;
assert!(start.elapsed() > Duration::from_millis(330));
assert!(start.elapsed() < Duration::from_millis(340));
rl.acquire().await;
assert!(start.elapsed() > Duration::from_millis(660));
assert!(start.elapsed() < Duration::from_millis(680));
rl.acquire().await;
assert!(start.elapsed() < Duration::from_millis(1010));

let rl2 = rl.clone();
let jh = spawn(async move {
Expand Down Expand Up @@ -129,11 +133,13 @@ mod tests {

let start = Instant::now();
rl.acquire().await;
assert!(start.elapsed() < Duration::from_millis(10));
rl.acquire().await;
assert!(start.elapsed() > Duration::from_millis(330));
assert!(start.elapsed() < Duration::from_millis(340));
rl.acquire().await;
assert!(start.elapsed() > Duration::from_millis(660));
assert!(start.elapsed() < Duration::from_millis(680));
rl.acquire().await;
assert!(start.elapsed() < Duration::from_millis(1010));

let rl2 = rl.clone();
let jh = spawn(async move {
Expand Down
4 changes: 1 addition & 3 deletions src/token_bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ impl TokenBucketRateLimiter {
pub fn new(rate: usize) -> TokenBucketRateLimiter {
assert!(rate > 0);

let period_ns = NANOS_PER_SEC
.checked_div(rate as u64)
.expect("rate is too big");
let period_ns = NANOS_PER_SEC.checked_div(rate as u64).unwrap();

let inner = TokenBucketInner {
tokens: 1,
Expand Down

0 comments on commit f216bf0

Please sign in to comment.