From 27d46cffb5743314924d775fb4b298dece9e7348 Mon Sep 17 00:00:00 2001 From: mxsm Date: Fri, 6 Dec 2024 18:09:37 +0800 Subject: [PATCH] =?UTF-8?q?[ISSUE=20#1612]=E2=9A=A1=EF=B8=8FOptimize=20Con?= =?UTF-8?q?sumeMessageDirectlyResult=20struct=F0=9F=94=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../body/consume_message_directly_result.rs | 286 ++++++++++++------ 1 file changed, 199 insertions(+), 87 deletions(-) diff --git a/rocketmq-remoting/src/protocol/body/consume_message_directly_result.rs b/rocketmq-remoting/src/protocol/body/consume_message_directly_result.rs index 94eafc2e..c7944cb4 100644 --- a/rocketmq-remoting/src/protocol/body/consume_message_directly_result.rs +++ b/rocketmq-remoting/src/protocol/body/consume_message_directly_result.rs @@ -1,87 +1,199 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -use serde::Deserialize; -use serde::Serialize; - -use crate::protocol::body::cm_result::CMResult; - -#[derive(Debug, Serialize, Deserialize)] -pub struct ConsumeMessageDirectlyResult { - order: bool, - auto_commit: bool, - consume_result: CMResult, - remark: String, - spent_time_mills: u64, -} - -impl ConsumeMessageDirectlyResult { - pub fn new( - order: bool, - auto_commit: bool, - consume_result: CMResult, - remark: String, - spent_time_mills: u64, - ) -> Self { - Self { - order, - auto_commit, - consume_result, - remark, - spent_time_mills, - } - } - - pub fn order(&self) -> bool { - self.order - } - - pub fn set_order(&mut self, order: bool) { - self.order = order; - } - - pub fn auto_commit(&self) -> bool { - self.auto_commit - } - - pub fn set_auto_commit(&mut self, auto_commit: bool) { - self.auto_commit = auto_commit; - } - - pub fn consume_result(&self) -> &CMResult { - &self.consume_result - } - - pub fn set_consume_result(&mut self, consume_result: CMResult) { - self.consume_result = consume_result; - } - - pub fn remark(&self) -> &str { - &self.remark - } - - pub fn set_remark(&mut self, remark: String) { - self.remark = remark; - } - - pub fn spent_time_mills(&self) -> u64 { - self.spent_time_mills - } - - pub fn set_spent_time_mills(&mut self, spent_time_mills: u64) { - self.spent_time_mills = spent_time_mills; - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +use std::fmt::Display; + +use cheetah_string::CheetahString; +use serde::Deserialize; +use serde::Serialize; + +use crate::protocol::body::cm_result::CMResult; + +#[derive(Debug, Serialize, Deserialize)] +pub struct ConsumeMessageDirectlyResult { + order: bool, + auto_commit: bool, + consume_result: Option, + remark: Option, + spent_time_mills: u64, +} + +impl Default for ConsumeMessageDirectlyResult { + #[inline] + fn default() -> Self { + Self { + order: false, + auto_commit: true, + consume_result: None, + remark: None, + spent_time_mills: 0, + } + } +} + +impl Display for ConsumeMessageDirectlyResult { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "ConsumeMessageDirectlyResult [order={}, auto_commit={}, consume_result={:?}, \ + remark={:?}, spent_time_mills={}]", + self.order, self.auto_commit, self.consume_result, self.remark, self.spent_time_mills + ) + } +} + +impl ConsumeMessageDirectlyResult { + pub fn new( + order: bool, + auto_commit: bool, + consume_result: CMResult, + remark: CheetahString, + spent_time_mills: u64, + ) -> Self { + Self { + order, + auto_commit, + consume_result: Some(consume_result), + remark: Some(remark), + spent_time_mills, + } + } + + #[inline] + pub fn order(&self) -> bool { + self.order + } + + #[inline] + pub fn set_order(&mut self, order: bool) { + self.order = order; + } + + #[inline] + pub fn auto_commit(&self) -> bool { + self.auto_commit + } + + #[inline] + pub fn set_auto_commit(&mut self, auto_commit: bool) { + self.auto_commit = auto_commit; + } + + #[inline] + pub fn consume_result(&self) -> &Option { + &self.consume_result + } + + #[inline] + pub fn set_consume_result(&mut self, consume_result: CMResult) { + self.consume_result = Some(consume_result); + } + + #[inline] + pub fn remark(&self) -> &Option { + &self.remark + } + + #[inline] + pub fn set_remark(&mut self, remark: CheetahString) { + self.remark = Some(remark); + } + + #[inline] + pub fn spent_time_mills(&self) -> u64 { + self.spent_time_mills + } + + #[inline] + pub fn set_spent_time_mills(&mut self, spent_time_mills: u64) { + self.spent_time_mills = spent_time_mills; + } +} + +#[cfg(test)] +mod tests { + use cheetah_string::CheetahString; + + use super::*; + use crate::protocol::body::cm_result::CMResult; + + #[test] + fn consume_message_directly_result_default_initializes_correctly() { + let result = ConsumeMessageDirectlyResult::default(); + assert!(!result.order()); + assert!(result.auto_commit()); + assert!(result.consume_result().is_none()); + assert!(result.remark().is_none()); + assert_eq!(result.spent_time_mills(), 0); + } + + #[test] + fn consume_message_directly_result_new_initializes_correctly() { + let consume_result = CMResult::default(); + let remark = CheetahString::from_static_str("test remark"); + let result = ConsumeMessageDirectlyResult::new( + true, + false, + consume_result.clone(), + remark.clone(), + 12345, + ); + assert!(result.order()); + assert!(!result.auto_commit()); + assert_eq!(result.consume_result().as_ref().unwrap(), &consume_result); + assert_eq!(result.remark().as_ref().unwrap(), &remark); + assert_eq!(result.spent_time_mills(), 12345); + } + + #[test] + fn consume_message_directly_result_setters_work_correctly() { + let mut result = ConsumeMessageDirectlyResult::default(); + result.set_order(true); + result.set_auto_commit(false); + let consume_result = CMResult::default(); + result.set_consume_result(consume_result.clone()); + let remark = CheetahString::from_static_str("updated remark"); + result.set_remark(remark.clone()); + result.set_spent_time_mills(67890); + + assert!(result.order()); + assert!(!result.auto_commit()); + assert_eq!(result.consume_result().as_ref().unwrap(), &consume_result); + assert_eq!(result.remark().as_ref().unwrap(), &remark); + assert_eq!(result.spent_time_mills(), 67890); + } + + #[test] + fn consume_message_directly_result_display_formats_correctly() { + let consume_result = CMResult::default(); + let remark = CheetahString::from_static_str("test remark"); + let result = ConsumeMessageDirectlyResult::new( + true, + false, + consume_result.clone(), + remark.clone(), + 12345, + ); + let display = format!("{}", result); + let expected = format!( + "ConsumeMessageDirectlyResult [order=true, auto_commit=false, consume_result={:?}, \ + remark={:?}, spent_time_mills=12345]", + Some(consume_result), + Some(remark) + ); + assert_eq!(display, expected); + } +}