Skip to main content

rocketmq_remoting/protocol/header/
elect_master_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 cheetah_string::CheetahString;
16use rocketmq_macros::RequestHeaderCodecV2;
17use serde::Deserialize;
18use serde::Serialize;
19
20#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2)]
21#[serde(rename_all = "camelCase")]
22pub struct ElectMasterResponseHeader {
23    pub master_broker_id: Option<i64>,
24    pub master_address: Option<CheetahString>,
25    pub master_epoch: Option<i32>,
26    pub sync_state_set_epoch: Option<i32>,
27}
28
29#[cfg(test)]
30mod tests {
31    use std::collections::HashMap;
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 elect_master_response_header_serializes_correctly() {
39        let header = ElectMasterResponseHeader {
40            master_broker_id: Some(1),
41            master_address: Some(CheetahString::from_static_str("test_address")),
42            master_epoch: Some(2),
43            sync_state_set_epoch: Some(3),
44        };
45        let map = header.to_map().unwrap();
46        assert_eq!(
47            map.get(&CheetahString::from_static_str(
48                ElectMasterResponseHeader::MASTER_BROKER_ID
49            ))
50            .unwrap(),
51            "1"
52        );
53        assert_eq!(
54            map.get(&CheetahString::from_static_str(
55                ElectMasterResponseHeader::MASTER_ADDRESS
56            ))
57            .unwrap(),
58            "test_address"
59        );
60        assert_eq!(
61            map.get(&CheetahString::from_static_str(ElectMasterResponseHeader::MASTER_EPOCH))
62                .unwrap(),
63            "2"
64        );
65        assert_eq!(
66            map.get(&CheetahString::from_static_str(
67                ElectMasterResponseHeader::SYNC_STATE_SET_EPOCH
68            ))
69            .unwrap(),
70            "3"
71        );
72    }
73
74    #[test]
75
76    fn elect_master_response_header_deserializes_correctly() {
77        let mut map = HashMap::new();
78        map.insert(
79            CheetahString::from_static_str(ElectMasterResponseHeader::MASTER_BROKER_ID),
80            CheetahString::from("1"),
81        );
82        map.insert(
83            CheetahString::from_static_str(ElectMasterResponseHeader::MASTER_ADDRESS),
84            CheetahString::from("test_address"),
85        );
86        map.insert(
87            CheetahString::from_static_str(ElectMasterResponseHeader::MASTER_EPOCH),
88            CheetahString::from("2"),
89        );
90        map.insert(
91            CheetahString::from_static_str(ElectMasterResponseHeader::SYNC_STATE_SET_EPOCH),
92            CheetahString::from("3"),
93        );
94        let header = <ElectMasterResponseHeader as FromMap>::from(&map).unwrap();
95        assert_eq!(header.master_broker_id, Some(1));
96        assert_eq!(
97            header.master_address,
98            Some(CheetahString::from_static_str("test_address"))
99        );
100        assert_eq!(header.master_epoch, Some(2));
101        assert_eq!(header.sync_state_set_epoch, Some(3));
102    }
103
104    #[test]
105    fn elect_master_response_header_handles_missing_optional_fields() {
106        let map = HashMap::new();
107        let header = <ElectMasterResponseHeader as FromMap>::from(&map).unwrap();
108        assert!(header.master_broker_id.is_none());
109        assert!(header.master_address.is_none());
110        assert!(header.master_epoch.is_none());
111        assert!(header.sync_state_set_epoch.is_none());
112    }
113}