From fb2cde2732f8b54b013c5f1e389ce0db4e322b48 Mon Sep 17 00:00:00 2001 From: dream-cloud-fly <3950754891@qq.com> Date: Fri, 14 Jun 2024 00:20:07 +0800 Subject: [PATCH] =?UTF-8?q?[ISSUE=20#520]=20=F0=9F=90=9BFix=20count=5Finne?= =?UTF-8?q?r=5Fmsg=5Fnum=20method=20of=20=20message=20decoder=20=20mode=20?= =?UTF-8?q?(#521)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pdated count_inner_msg_num to check for bytes remaining instead of emptiness; adjusted tests to reflect new logic and expected message counts --- .../src/common/message/message_decoder.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rocketmq-common/src/common/message/message_decoder.rs b/rocketmq-common/src/common/message/message_decoder.rs index 63a0c98c..08ac4c7f 100644 --- a/rocketmq-common/src/common/message/message_decoder.rs +++ b/rocketmq-common/src/common/message/message_decoder.rs @@ -285,8 +285,8 @@ pub fn count_inner_msg_num(bytes: Option) -> u32 { None => 0, Some(mut bytes) => { let mut count = 0; - while !bytes.is_empty() { - let size = bytes.get_i32(); + while bytes.has_remaining() { + let size = bytes.slice(0..4).get_i32(); if size as usize > bytes.len() { break; } @@ -308,9 +308,9 @@ mod tests { #[test] fn count_inner_msg_num_counts_correctly_for_multiple_messages() { let mut bytes = BytesMut::new(); - bytes.put_i32(4); + bytes.put_i32(8); bytes.put_slice(&[0, 0, 0, 0]); - bytes.put_i32(4); + bytes.put_i32(8); bytes.put_slice(&[0, 0, 0, 0]); assert_eq!(count_inner_msg_num(Some(bytes.freeze())), 2); } @@ -318,7 +318,7 @@ mod tests { #[test] fn count_inner_msg_num_counts_correctly_for_single_message() { let mut bytes = BytesMut::new(); - bytes.put_i32(4); + bytes.put_i32(8); bytes.put_slice(&[0, 0, 0, 0]); assert_eq!(count_inner_msg_num(Some(bytes.freeze())), 1); } @@ -333,6 +333,6 @@ mod tests { fn count_inner_msg_num_ignores_incomplete_messages() { let mut bytes = BytesMut::new(); bytes.put_i32(4); - assert_eq!(count_inner_msg_num(Some(bytes.freeze())), 0); + assert_eq!(count_inner_msg_num(Some(bytes.freeze())), 1); } }