Skip to main content

rocketmq_remoting/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 std::fmt::Display;
16
17use cheetah_string::CheetahString;
18use rocketmq_macros::RequestHeaderCodecV2;
19use serde::Deserialize;
20use serde::Serialize;
21
22#[derive(Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2, Clone)]
23pub struct PopMessageResponseHeader {
24    #[serde(rename = "popTime")]
25    #[required]
26    pub pop_time: u64,
27
28    #[serde(rename = "invisibleTime")]
29    #[required]
30    pub invisible_time: u64,
31
32    #[serde(rename = "reviveQid")]
33    #[required]
34    pub revive_qid: u32,
35
36    #[serde(rename = "restNum")]
37    #[required]
38    pub rest_num: u64,
39
40    #[serde(rename = "startOffsetInfo", skip_serializing_if = "Option::is_none")]
41    pub start_offset_info: Option<CheetahString>,
42
43    #[serde(rename = "msgOffsetInfo", skip_serializing_if = "Option::is_none")]
44    pub msg_offset_info: Option<CheetahString>,
45
46    #[serde(rename = "orderCountInfo", skip_serializing_if = "Option::is_none")]
47    pub order_count_info: Option<CheetahString>,
48}
49
50impl Display for PopMessageResponseHeader {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        write!(
53            f,
54            "PopMessageResponseHeader [pop_time={}, invisible_time={}, revive_qid={}, rest_num={}, \
55             start_offset_info={:?}, msg_offset_info={:?}, order_count_info={:?}]",
56            self.pop_time,
57            self.invisible_time,
58            self.revive_qid,
59            self.rest_num,
60            self.start_offset_info,
61            self.msg_offset_info,
62            self.order_count_info
63        )
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn display_formatting() {
73        let header = PopMessageResponseHeader {
74            pop_time: 123456789,
75            invisible_time: 987654321,
76            revive_qid: 42,
77            rest_num: 10,
78            start_offset_info: Some("start_offset".into()),
79            msg_offset_info: Some("msg_offset".into()),
80            order_count_info: Some("order_count".into()),
81        };
82        let expected = "PopMessageResponseHeader [pop_time=123456789, invisible_time=987654321, revive_qid=42, \
83                        rest_num=10, start_offset_info=Some(\"start_offset\"), msg_offset_info=Some(\"msg_offset\"), \
84                        order_count_info=Some(\"order_count\")]";
85        assert_eq!(format!("{}", header), expected);
86    }
87
88    #[test]
89    fn display_formatting_with_none_values() {
90        let header = PopMessageResponseHeader {
91            pop_time: 123456789,
92            invisible_time: 987654321,
93            revive_qid: 42,
94            rest_num: 10,
95            start_offset_info: None,
96            msg_offset_info: None,
97            order_count_info: None,
98        };
99        let expected = "PopMessageResponseHeader [pop_time=123456789, invisible_time=987654321, revive_qid=42, \
100                        rest_num=10, start_offset_info=None, msg_offset_info=None, order_count_info=None]";
101        assert_eq!(format!("{}", header), expected);
102    }
103
104    #[test]
105    fn serialize_to_json() {
106        let header = PopMessageResponseHeader {
107            pop_time: 123456789,
108            invisible_time: 987654321,
109            revive_qid: 42,
110            rest_num: 10,
111            start_offset_info: Some("start_offset".into()),
112            msg_offset_info: Some("msg_offset".into()),
113            order_count_info: Some("order_count".into()),
114        };
115        let json = serde_json::to_string(&header).unwrap();
116        let expected = r#"{"popTime":123456789,"invisibleTime":987654321,"reviveQid":42,"restNum":10,"startOffsetInfo":"start_offset","msgOffsetInfo":"msg_offset","orderCountInfo":"order_count"}"#;
117        assert_eq!(json, expected);
118    }
119
120    #[test]
121    fn deserialize_from_json() {
122        let json = r#"{"popTime":123456789,"invisibleTime":987654321,"reviveQid":42,"restNum":10,"startOffsetInfo":"start_offset","msgOffsetInfo":"msg_offset","orderCountInfo":"order_count"}"#;
123        let header: PopMessageResponseHeader = serde_json::from_str(json).unwrap();
124        assert_eq!(header.pop_time, 123456789);
125        assert_eq!(header.invisible_time, 987654321);
126        assert_eq!(header.revive_qid, 42);
127        assert_eq!(header.rest_num, 10);
128        assert_eq!(header.start_offset_info, Some("start_offset".into()));
129        assert_eq!(header.msg_offset_info, Some("msg_offset".into()));
130        assert_eq!(header.order_count_info, Some("order_count".into()));
131    }
132
133    #[test]
134    fn deserialize_from_json_with_none_values() {
135        let json = r#"{"popTime":123456789,"invisibleTime":987654321,"reviveQid":42,"restNum":10}"#;
136        let header: PopMessageResponseHeader = serde_json::from_str(json).unwrap();
137        assert_eq!(header.pop_time, 123456789);
138        assert_eq!(header.invisible_time, 987654321);
139        assert_eq!(header.revive_qid, 42);
140        assert_eq!(header.rest_num, 10);
141        assert_eq!(header.start_offset_info, None);
142        assert_eq!(header.msg_offset_info, None);
143        assert_eq!(header.order_count_info, None);
144    }
145}