Skip to main content

rocketmq_protocol/protocol/header/
pop_message_response_header.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use rocketmq_macros::RequestHeaderCodec;
16use std::fmt::Display;
17
18use cheetah_string::CheetahString;
19use serde::Deserialize;
20use serde::Serialize;
21
22#[derive(Debug, Serialize, Deserialize, Default, RequestHeaderCodec, Clone)]
23#[header(
24    type_id = "rocketmq_protocol::protocol::header::pop_message_response_header::PopMessageResponseHeader",
25    java_class = "org.apache.rocketmq.remoting.protocol.header.PopMessageResponseHeader"
26)]
27pub struct PopMessageResponseHeader {
28    #[serde(rename = "popTime")]
29    #[header(required, range = "i64")]
30    pub pop_time: u64,
31
32    #[serde(rename = "invisibleTime")]
33    #[header(required, range = "i64")]
34    pub invisible_time: u64,
35
36    #[serde(rename = "reviveQid")]
37    #[header(required, range = "i32")]
38    pub revive_qid: u32,
39
40    #[serde(rename = "restNum")]
41    #[header(required, range = "i64")]
42    pub rest_num: u64,
43
44    #[serde(rename = "startOffsetInfo", skip_serializing_if = "Option::is_none")]
45    pub start_offset_info: Option<CheetahString>,
46
47    #[serde(rename = "msgOffsetInfo", skip_serializing_if = "Option::is_none")]
48    pub msg_offset_info: Option<CheetahString>,
49
50    #[serde(rename = "orderCountInfo", skip_serializing_if = "Option::is_none")]
51    pub order_count_info: Option<CheetahString>,
52}
53
54impl Display for PopMessageResponseHeader {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        write!(
57            f,
58            "PopMessageResponseHeader [pop_time={}, invisible_time={}, revive_qid={}, rest_num={}, \
59             start_offset_info={:?}, msg_offset_info={:?}, order_count_info={:?}]",
60            self.pop_time,
61            self.invisible_time,
62            self.revive_qid,
63            self.rest_num,
64            self.start_offset_info,
65            self.msg_offset_info,
66            self.order_count_info
67        )
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn display_formatting() {
77        let header = PopMessageResponseHeader {
78            pop_time: 123456789,
79            invisible_time: 987654321,
80            revive_qid: 42,
81            rest_num: 10,
82            start_offset_info: Some("start_offset".into()),
83            msg_offset_info: Some("msg_offset".into()),
84            order_count_info: Some("order_count".into()),
85        };
86        let expected = "PopMessageResponseHeader [pop_time=123456789, invisible_time=987654321, revive_qid=42, \
87                        rest_num=10, start_offset_info=Some(\"start_offset\"), msg_offset_info=Some(\"msg_offset\"), \
88                        order_count_info=Some(\"order_count\")]";
89        assert_eq!(format!("{}", header), expected);
90    }
91
92    #[test]
93    fn display_formatting_with_none_values() {
94        let header = PopMessageResponseHeader {
95            pop_time: 123456789,
96            invisible_time: 987654321,
97            revive_qid: 42,
98            rest_num: 10,
99            start_offset_info: None,
100            msg_offset_info: None,
101            order_count_info: None,
102        };
103        let expected = "PopMessageResponseHeader [pop_time=123456789, invisible_time=987654321, revive_qid=42, \
104                        rest_num=10, start_offset_info=None, msg_offset_info=None, order_count_info=None]";
105        assert_eq!(format!("{}", header), expected);
106    }
107
108    #[test]
109    fn serialize_to_json() {
110        let header = PopMessageResponseHeader {
111            pop_time: 123456789,
112            invisible_time: 987654321,
113            revive_qid: 42,
114            rest_num: 10,
115            start_offset_info: Some("start_offset".into()),
116            msg_offset_info: Some("msg_offset".into()),
117            order_count_info: Some("order_count".into()),
118        };
119        let json = serde_json::to_string(&header).unwrap();
120        let expected = r#"{"popTime":123456789,"invisibleTime":987654321,"reviveQid":42,"restNum":10,"startOffsetInfo":"start_offset","msgOffsetInfo":"msg_offset","orderCountInfo":"order_count"}"#;
121        assert_eq!(json, expected);
122    }
123
124    #[test]
125    fn deserialize_from_json() {
126        let json = r#"{"popTime":123456789,"invisibleTime":987654321,"reviveQid":42,"restNum":10,"startOffsetInfo":"start_offset","msgOffsetInfo":"msg_offset","orderCountInfo":"order_count"}"#;
127        let header: PopMessageResponseHeader = serde_json::from_str(json).unwrap();
128        assert_eq!(header.pop_time, 123456789);
129        assert_eq!(header.invisible_time, 987654321);
130        assert_eq!(header.revive_qid, 42);
131        assert_eq!(header.rest_num, 10);
132        assert_eq!(header.start_offset_info, Some("start_offset".into()));
133        assert_eq!(header.msg_offset_info, Some("msg_offset".into()));
134        assert_eq!(header.order_count_info, Some("order_count".into()));
135    }
136
137    #[test]
138    fn deserialize_from_json_with_none_values() {
139        let json = r#"{"popTime":123456789,"invisibleTime":987654321,"reviveQid":42,"restNum":10}"#;
140        let header: PopMessageResponseHeader = serde_json::from_str(json).unwrap();
141        assert_eq!(header.pop_time, 123456789);
142        assert_eq!(header.invisible_time, 987654321);
143        assert_eq!(header.revive_qid, 42);
144        assert_eq!(header.rest_num, 10);
145        assert_eq!(header.start_offset_info, None);
146        assert_eq!(header.msg_offset_info, None);
147        assert_eq!(header.order_count_info, None);
148    }
149}