Skip to main content

rocketmq_remoting/protocol/header/
pop_lite_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(Clone, Debug, Default, Serialize, Deserialize, RequestHeaderCodecV2)]
23#[serde(rename_all = "camelCase")]
24pub struct PopLiteMessageResponseHeader {
25    #[required]
26    pub pop_time: i64,
27    #[required]
28    pub invisible_time: i64,
29    #[required]
30    pub revive_qid: i32,
31    pub start_offset_info: Option<CheetahString>,
32    pub msg_offset_info: Option<CheetahString>,
33    pub order_count_info: Option<CheetahString>,
34}
35
36impl Display for PopLiteMessageResponseHeader {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        write!(
39            f,
40            "PopLiteMessageResponseHeader [pop_time={}, invisible_time={}, revive_qid={}, start_offset_info={:?}, \
41             msg_offset_info={:?}, order_count_info={:?}]",
42            self.pop_time,
43            self.invisible_time,
44            self.revive_qid,
45            self.start_offset_info,
46            self.msg_offset_info,
47            self.order_count_info
48        )
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use crate::protocol::command_custom_header::CommandCustomHeader;
56
57    #[test]
58    fn pop_lite_message_response_header_serializes_to_map() {
59        let header = PopLiteMessageResponseHeader {
60            pop_time: 123,
61            invisible_time: 456,
62            revive_qid: 7,
63            start_offset_info: Some("start".into()),
64            msg_offset_info: Some("msg".into()),
65            order_count_info: Some("order".into()),
66        };
67
68        let map = header.to_map().unwrap();
69        assert_eq!(map.get(&CheetahString::from_static_str("popTime")).unwrap(), "123");
70        assert_eq!(
71            map.get(&CheetahString::from_static_str("invisibleTime")).unwrap(),
72            "456"
73        );
74        assert_eq!(map.get(&CheetahString::from_static_str("reviveQid")).unwrap(), "7");
75    }
76
77    #[test]
78    fn pop_lite_message_response_header_display_contains_fields() {
79        let header = PopLiteMessageResponseHeader {
80            pop_time: 1,
81            invisible_time: 2,
82            revive_qid: 3,
83            start_offset_info: None,
84            msg_offset_info: None,
85            order_count_info: None,
86        };
87
88        let display = format!("{header}");
89        assert!(display.contains("pop_time=1"));
90        assert!(display.contains("invisible_time=2"));
91        assert!(display.contains("revive_qid=3"));
92    }
93}