Skip to main content

rocketmq_protocol/protocol/header/namesrv/
config_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::RequestHeaderCodec;
16use serde::Deserialize;
17use serde::Serialize;
18
19#[derive(Debug, Clone, Default, Deserialize, Serialize, RequestHeaderCodec)]
20#[header(type_id = "rocketmq_protocol::protocol::header::namesrv::config_header::GetNamesrvConfigRequestHeader")]
21#[serde(rename_all = "camelCase")]
22pub struct GetNamesrvConfigRequestHeader {
23    pub probe_only: Option<bool>,
24}
25
26impl GetNamesrvConfigRequestHeader {
27    pub fn for_probe() -> Self {
28        Self { probe_only: Some(true) }
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use cheetah_string::CheetahString;
35
36    use super::GetNamesrvConfigRequestHeader;
37    use crate::code::request_code::RequestCode;
38    use crate::protocol::remoting_command::RemotingCommand;
39
40    #[test]
41    fn request_command_encodes_probe_only_header() {
42        let mut command = RemotingCommand::create_request_command(
43            RequestCode::GetNamesrvConfig,
44            GetNamesrvConfigRequestHeader::for_probe(),
45        );
46        command.make_custom_header_to_net();
47
48        let ext_fields = command.ext_fields().expect("header fields should exist");
49        assert_eq!(ext_fields.get("probeOnly").map(CheetahString::as_str), Some("true"));
50    }
51
52    #[test]
53    fn request_command_decodes_probe_only_header() {
54        let mut command = RemotingCommand::create_request_command(
55            RequestCode::GetNamesrvConfig,
56            GetNamesrvConfigRequestHeader::for_probe(),
57        );
58        command.make_custom_header_to_net();
59
60        let decoded = command
61            .decode_command_custom_header_fast::<GetNamesrvConfigRequestHeader>()
62            .expect("header should decode");
63        assert_eq!(decoded.probe_only, Some(true));
64    }
65}