rocketmq_remoting/protocol/header/
pop_message_response_header.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use std::fmt::Display;
18
19use cheetah_string::CheetahString;
20use rocketmq_macros::RequestHeaderCodecV2;
21use serde::Deserialize;
22use serde::Serialize;
23
24#[derive(Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2, Clone)]
25pub struct PopMessageResponseHeader {
26    #[serde(rename = "popTime")]
27    #[required]
28    pub pop_time: u64,
29
30    #[serde(rename = "invisibleTime")]
31    #[required]
32    pub invisible_time: u64,
33
34    #[serde(rename = "reviveQid")]
35    #[required]
36    pub revive_qid: u32,
37
38    #[serde(rename = "restNum")]
39    #[required]
40    pub rest_num: u64,
41
42    #[serde(rename = "startOffsetInfo", skip_serializing_if = "Option::is_none")]
43    pub start_offset_info: Option<CheetahString>,
44
45    #[serde(rename = "msgOffsetInfo", skip_serializing_if = "Option::is_none")]
46    pub msg_offset_info: Option<CheetahString>,
47
48    #[serde(rename = "orderCountInfo", skip_serializing_if = "Option::is_none")]
49    pub order_count_info: Option<CheetahString>,
50}
51
52impl Display for PopMessageResponseHeader {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        write!(
55            f,
56            "PopMessageResponseHeader [pop_time={}, invisible_time={}, revive_qid={}, \
57             rest_num={}, start_offset_info={:?}, msg_offset_info={:?}, order_count_info={:?}]",
58            self.pop_time,
59            self.invisible_time,
60            self.revive_qid,
61            self.rest_num,
62            self.start_offset_info,
63            self.msg_offset_info,
64            self.order_count_info
65        )
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn display_formatting() {
75        let header = PopMessageResponseHeader {
76            pop_time: 123456789,
77            invisible_time: 987654321,
78            revive_qid: 42,
79            rest_num: 10,
80            start_offset_info: Some("start_offset".into()),
81            msg_offset_info: Some("msg_offset".into()),
82            order_count_info: Some("order_count".into()),
83        };
84        let expected = "PopMessageResponseHeader [pop_time=123456789, invisible_time=987654321, \
85                        revive_qid=42, rest_num=10, start_offset_info=Some(\"start_offset\"), \
86                        msg_offset_info=Some(\"msg_offset\"), \
87                        order_count_info=Some(\"order_count\")]";
88        assert_eq!(format!("{}", header), expected);
89    }
90
91    #[test]
92    fn display_formatting_with_none_values() {
93        let header = PopMessageResponseHeader {
94            pop_time: 123456789,
95            invisible_time: 987654321,
96            revive_qid: 42,
97            rest_num: 10,
98            start_offset_info: None,
99            msg_offset_info: None,
100            order_count_info: None,
101        };
102        let expected = "PopMessageResponseHeader [pop_time=123456789, invisible_time=987654321, \
103                        revive_qid=42, rest_num=10, start_offset_info=None, msg_offset_info=None, \
104                        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}