Skip to content

Commit

Permalink
fix: add tests and fix some
Browse files Browse the repository at this point in the history
1. add test_ringbuf_spsc_with_wait_result_and_notify
2. calc data_len of datablock may panic
  • Loading branch information
fengys1996 committed Dec 24, 2024
1 parent f13552c commit b4d56e7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/ringbuf/data_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,19 @@ impl<T> DataBlock<T> {
object: Arc<T>,
) -> Result<Self> {
let header_len_u32 = convert_num!(HEADER_LEN, u32)?;
let data_len = len - header_len_u32;

ensure!(
data_len > 0,
len >= header_len_u32,
error::InvalidParameterSnafu {
detail: "Total_length must be greater than HEADER_LEN.",
detail: "Total length must be greater than HEADER_LEN.",
}
);

// Unwrap safety: checked above.
let data_len = len.checked_sub(header_len_u32).unwrap();

let header = Header::fow_raw(start_ptr);
header.set_capacity(len - header_len_u32);
header.set_capacity(data_len);
header.set_written(0);
header.set_busy(true);
header.set_req_id(req_id);
Expand Down
5 changes: 3 additions & 2 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ pub async fn wait_consumer_online(
Err("wait consumer online timeout".to_string())
}

pub fn gen_str(max_len: usize) -> String {
pub fn gen_str(min_len: usize, max_len: usize) -> String {
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789)(*&^%$#@!~";
let len = rand::random::<usize>() % max_len + 1;

let len = rand::random::<usize>() % (max_len - min_len) + min_len;

let mut s = String::new();

Expand Down
18 changes: 14 additions & 4 deletions tests/ringbuf_spsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,34 @@ use shm_ringbuf::producer::RingbufProducer;

#[tokio::test]
async fn test_ringbuf_spsc_base() {
do_test_ringbuf_spsc(false, Duration::from_millis(10), None).await;
do_test_ringbuf_spsc(false, Duration::from_millis(10), None, 0, 1000).await;
}

#[tokio::test]
async fn test_ringbuf_spsc_with_notify() {
// Set too long interval for testing notify.
do_test_ringbuf_spsc(false, Duration::from_secs(100), Some(1000)).await;
do_test_ringbuf_spsc(false, Duration::from_secs(100), Some(500), 499, 1000)
.await;
}

#[tokio::test]
async fn test_ringbuf_spsc_with_wait_result() {
do_test_ringbuf_spsc(true, Duration::from_millis(10), None).await;
do_test_ringbuf_spsc(true, Duration::from_millis(10), None, 0, 1000).await;
}

#[tokio::test]
async fn test_ringbuf_spsc_with_wait_result_and_notify() {
// Set too long interval for testing notify.
do_test_ringbuf_spsc(true, Duration::from_secs(100), Some(500), 499, 1000)
.await;
}

async fn do_test_ringbuf_spsc(
wait_result: bool,
process_interval: Duration,
notify_limit: Option<u32>,
min_msg_len: usize,
max_msg_len: usize,
) {
tracing_subscriber::fmt::init();

Expand Down Expand Up @@ -74,7 +84,7 @@ async fn do_test_ringbuf_spsc(
};

for i in 0..msg_num {
let write_str = gen_str(1000);
let write_str = gen_str(min_msg_len, max_msg_len);

expected_send.send(write_str.clone()).await.unwrap();

Expand Down

0 comments on commit b4d56e7

Please sign in to comment.