Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #1127]🧪Add test case for BlockingQueue #1128

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading