Skip to main content

rocketmq_remoting/protocol/header/controller/
get_replica_info_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(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2)]
20#[serde(rename_all = "camelCase")]
21pub struct GetReplicaInfoResponseHeader {
22    pub master_broker_id: Option<i64>,
23    pub master_address: Option<String>,
24    pub master_epoch: Option<i32>,
25}
26
27#[cfg(test)]
28mod tests {
29    use std::collections::HashMap;
30
31    use cheetah_string::CheetahString;
32
33    use super::*;
34    use crate::protocol::command_custom_header::CommandCustomHeader;
35    use crate::protocol::command_custom_header::FromMap;
36
37    #[test]
38    fn get_replica_info_response_header_serializes_correctly() {
39        let header = GetReplicaInfoResponseHeader {
40            master_broker_id: Some(123),
41            master_address: Some("127.0.0.1:10911".to_string()),
42            master_epoch: Some(5),
43        };
44        let map = header.to_map().unwrap();
45        assert_eq!(
46            map.get(&CheetahString::from_static_str("masterBrokerId")).unwrap(),
47            "123"
48        );
49        assert_eq!(
50            map.get(&CheetahString::from_static_str("masterAddress")).unwrap(),
51            "127.0.0.1:10911"
52        );
53        assert_eq!(map.get(&CheetahString::from_static_str("masterEpoch")).unwrap(), "5");
54    }
55
56    #[test]
57    fn get_replica_info_response_header_deserializes_correctly() {
58        let mut map = HashMap::new();
59        map.insert(
60            CheetahString::from_static_str("masterBrokerId"),
61            CheetahString::from_static_str("123"),
62        );
63        map.insert(
64            CheetahString::from_static_str("masterAddress"),
65            CheetahString::from_static_str("127.0.0.1:10911"),
66        );
67        map.insert(
68            CheetahString::from_static_str("masterEpoch"),
69            CheetahString::from_static_str("5"),
70        );
71
72        let header = <GetReplicaInfoResponseHeader as FromMap>::from(&map).unwrap();
73        assert_eq!(header.master_broker_id, Some(123));
74        assert_eq!(header.master_address, Some("127.0.0.1:10911".to_string()));
75        assert_eq!(header.master_epoch, Some(5));
76    }
77
78    #[test]
79    fn get_replica_info_response_header_default() {
80        let header = GetReplicaInfoResponseHeader::default();
81        assert_eq!(header.master_broker_id, None);
82        assert_eq!(header.master_address, None);
83        assert_eq!(header.master_epoch, None);
84    }
85
86    #[test]
87    fn get_replica_info_response_header_clone() {
88        let header = GetReplicaInfoResponseHeader {
89            master_broker_id: Some(123),
90            master_address: Some("127.0.0.1:10911".to_string()),
91            master_epoch: Some(5),
92        };
93        let cloned = header.clone();
94        assert_eq!(header.master_broker_id, cloned.master_broker_id);
95        assert_eq!(header.master_address, cloned.master_address);
96        assert_eq!(header.master_epoch, cloned.master_epoch);
97    }
98}