Skip to main content

rocketmq_remoting/protocol/header/
pull_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::RequestHeaderCodecV2;
16use serde::Deserialize;
17use serde::Serialize;
18
19#[derive(Serialize, Deserialize, Debug, Default, Clone, RequestHeaderCodecV2)]
20#[serde(rename_all = "camelCase")]
21pub struct PullMessageResponseHeader {
22    #[required]
23    pub suggest_which_broker_id: u64,
24
25    #[required]
26    pub next_begin_offset: i64,
27
28    #[required]
29    pub min_offset: i64,
30
31    #[required]
32    pub max_offset: i64,
33
34    pub offset_delta: Option<i64>,
35    pub topic_sys_flag: Option<i32>,
36    pub group_sys_flag: Option<i32>,
37    pub forbidden_type: Option<i32>,
38}
39
40#[cfg(test)]
41mod tests {
42    use std::collections::HashMap;
43
44    use cheetah_string::CheetahString;
45
46    use super::*;
47    use crate::protocol::command_custom_header::CommandCustomHeader;
48    use crate::protocol::command_custom_header::FromMap;
49
50    #[test]
51    fn pull_message_response_header_serializes_correctly() {
52        let header = PullMessageResponseHeader {
53            suggest_which_broker_id: 123,
54            next_begin_offset: 456,
55            min_offset: 789,
56            max_offset: 101112,
57            offset_delta: Some(131415),
58            topic_sys_flag: Some(161718),
59            group_sys_flag: Some(192021),
60            forbidden_type: Some(222324),
61        };
62        let map = header.to_map().unwrap();
63        assert_eq!(
64            map.get(&CheetahString::from_static_str("suggestWhichBrokerId"))
65                .unwrap(),
66            "123"
67        );
68        assert_eq!(
69            map.get(&CheetahString::from_static_str("nextBeginOffset")).unwrap(),
70            "456"
71        );
72        assert_eq!(map.get(&CheetahString::from_static_str("minOffset")).unwrap(), "789");
73        assert_eq!(map.get(&CheetahString::from_static_str("maxOffset")).unwrap(), "101112");
74        assert_eq!(
75            map.get(&CheetahString::from_static_str("offsetDelta")).unwrap(),
76            "131415"
77        );
78        assert_eq!(
79            map.get(&CheetahString::from_static_str("topicSysFlag")).unwrap(),
80            "161718"
81        );
82        assert_eq!(
83            map.get(&CheetahString::from_static_str("groupSysFlag")).unwrap(),
84            "192021"
85        );
86        assert_eq!(
87            map.get(&CheetahString::from_static_str("forbiddenType")).unwrap(),
88            "222324"
89        );
90    }
91
92    #[test]
93    fn pull_message_response_header_deserializes_correctly() {
94        let mut map = HashMap::new();
95        map.insert(
96            CheetahString::from_static_str("suggestWhichBrokerId"),
97            CheetahString::from_static_str("123"),
98        );
99        map.insert(
100            CheetahString::from_static_str("nextBeginOffset"),
101            CheetahString::from_static_str("456"),
102        );
103        map.insert(
104            CheetahString::from_static_str("minOffset"),
105            CheetahString::from_static_str("789"),
106        );
107        map.insert(
108            CheetahString::from_static_str("maxOffset"),
109            CheetahString::from_static_str("101112"),
110        );
111        map.insert(
112            CheetahString::from_static_str("offsetDelta"),
113            CheetahString::from_static_str("131415"),
114        );
115        map.insert(
116            CheetahString::from_static_str("topicSysFlag"),
117            CheetahString::from_static_str("161718"),
118        );
119        map.insert(
120            CheetahString::from_static_str("groupSysFlag"),
121            CheetahString::from_static_str("192021"),
122        );
123        map.insert(
124            CheetahString::from_static_str("forbiddenType"),
125            CheetahString::from_static_str("222324"),
126        );
127
128        let header: PullMessageResponseHeader = <PullMessageResponseHeader as FromMap>::from(&map).unwrap();
129        assert_eq!(header.suggest_which_broker_id, 123);
130        assert_eq!(header.next_begin_offset, 456);
131        assert_eq!(header.min_offset, 789);
132        assert_eq!(header.max_offset, 101112);
133        assert_eq!(header.offset_delta.unwrap(), 131415);
134        assert_eq!(header.topic_sys_flag.unwrap(), 161718);
135        assert_eq!(header.group_sys_flag.unwrap(), 192021);
136        assert_eq!(header.forbidden_type.unwrap(), 222324);
137    }
138
139    #[test]
140    fn pull_message_response_header_handles_missing_optional_fields() {
141        let mut map = HashMap::new();
142        map.insert(
143            CheetahString::from_static_str("suggestWhichBrokerId"),
144            CheetahString::from_static_str("123"),
145        );
146        map.insert(
147            CheetahString::from_static_str("nextBeginOffset"),
148            CheetahString::from_static_str("456"),
149        );
150        map.insert(
151            CheetahString::from_static_str("minOffset"),
152            CheetahString::from_static_str("789"),
153        );
154        map.insert(
155            CheetahString::from_static_str("maxOffset"),
156            CheetahString::from_static_str("101112"),
157        );
158
159        let header = <PullMessageResponseHeader as FromMap>::from(&map).unwrap();
160        assert_eq!(header.suggest_which_broker_id, 123);
161        assert_eq!(header.next_begin_offset, 456);
162        assert_eq!(header.min_offset, 789);
163        assert_eq!(header.max_offset, 101112);
164        assert!(header.offset_delta.is_none());
165        assert!(header.topic_sys_flag.is_none());
166        assert!(header.group_sys_flag.is_none());
167        assert!(header.forbidden_type.is_none());
168    }
169
170    #[test]
171    fn pull_message_response_header_handles_invalid_data() {
172        let mut map = HashMap::new();
173        map.insert(
174            CheetahString::from_static_str("suggestWhichBrokerId"),
175            CheetahString::from_static_str("invalid"),
176        );
177        map.insert(
178            CheetahString::from_static_str("nextBeginOffset"),
179            CheetahString::from_static_str("invalid"),
180        );
181        map.insert(
182            CheetahString::from_static_str("minOffset"),
183            CheetahString::from_static_str("invalid"),
184        );
185        map.insert(
186            CheetahString::from_static_str("maxOffset"),
187            CheetahString::from_static_str("invalid"),
188        );
189
190        let result = <PullMessageResponseHeader as FromMap>::from(&map);
191        assert!(result.is_err());
192    }
193}