Skip to main content

rocketmq_remoting/protocol/header/
get_max_offset_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(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodecV2)]
20pub struct GetMaxOffsetResponseHeader {
21    pub offset: i64,
22}
23
24#[cfg(test)]
25mod tests {
26    use std::collections::HashMap;
27
28    use cheetah_string::CheetahString;
29
30    use super::*;
31    use crate::protocol::command_custom_header::FromMap;
32
33    #[test]
34    fn get_max_offset_response_header_default() {
35        let header = GetMaxOffsetResponseHeader::default();
36        assert_eq!(header.offset, 0);
37    }
38
39    #[test]
40    fn get_max_offset_response_header_serialization() {
41        let header = GetMaxOffsetResponseHeader { offset: 12345 };
42        let json = serde_json::to_string(&header).unwrap();
43        assert_eq!(json, r#"{"offset":12345}"#);
44    }
45
46    #[test]
47    fn get_max_offset_response_header_deserialization() {
48        let json = r#"{"offset":12345}"#;
49        let header: GetMaxOffsetResponseHeader = serde_json::from_str(json).unwrap();
50        assert_eq!(header.offset, 12345);
51    }
52
53    #[test]
54    fn get_max_offset_response_header_from_map() {
55        let mut map = HashMap::new();
56        map.insert(CheetahString::from("offset"), CheetahString::from("12345"));
57        let header = <GetMaxOffsetResponseHeader as FromMap>::from(&map).unwrap();
58        assert_eq!(header.offset, 12345);
59    }
60}