Skip to content

Commit

Permalink
[ISSUE #1127]🧪Add test case for BlockingQueue (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
main-master-hub authored Nov 9, 2024
1 parent 0f6030f commit 57e658d
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions rocketmq/src/blocking_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,48 @@ impl<T> BlockingQueue<T> {
time::timeout(timeout, self.take()).await.ok()
}
}

#[cfg(test)]
mod tests {
use tokio::time::Duration;

use super::*;

#[tokio::test]
async fn put_item_in_queue() {
let queue = BlockingQueue::new(2);
queue.put(1).await;
let item = queue.take().await;
assert_eq!(item, 1);
}

#[tokio::test]
async fn offer_item_within_timeout() {
let queue = BlockingQueue::new(1);
let result = queue.offer(1, Duration::from_millis(100)).await;
assert!(result);
}

#[tokio::test]
async fn offer_item_exceeds_timeout() {
let queue = BlockingQueue::new(1);
queue.put(1).await;
let result = queue.offer(2, Duration::from_millis(100)).await;
assert!(!result);
}

#[tokio::test]
async fn poll_item_within_timeout() {
let queue = BlockingQueue::new(1);
queue.put(1).await;
let item = queue.poll(Duration::from_millis(100)).await;
assert_eq!(item, Some(1));
}

#[tokio::test]
async fn poll_item_exceeds_timeout() {
let queue = BlockingQueue::<()>::new(1);
let item = queue.poll(Duration::from_millis(100)).await;
assert_eq!(item, None);
}
}

0 comments on commit 57e658d

Please sign in to comment.