Skip to main content

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