Skip to main content

rocketmq_remoting/protocol/body/request/
lock_batch_request_body.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 std::collections::HashSet;
16use std::fmt::Display;
17
18use cheetah_string::CheetahString;
19use rocketmq_common::common::message::message_queue::MessageQueue;
20use serde::Deserialize;
21use serde::Serialize;
22
23#[derive(Serialize, Deserialize, Debug, Default, Clone)]
24#[serde(rename_all = "camelCase")]
25pub struct LockBatchRequestBody {
26    pub consumer_group: Option<CheetahString>,
27    pub client_id: Option<CheetahString>,
28    pub only_this_broker: bool,
29    pub mq_set: HashSet<MessageQueue>,
30}
31
32impl Display for LockBatchRequestBody {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        write!(
35            f,
36            "LockBatchRequestBody [consumer_group={}, client_id={}, only_this_broker={}, mq_set={:?}]",
37            self.consumer_group.as_ref().unwrap_or(&"".into()),
38            self.client_id.as_ref().unwrap_or(&"".into()),
39            self.only_this_broker,
40            self.mq_set
41        )
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_lock_batch_request_body_default() {
51        let body = LockBatchRequestBody::default();
52        assert!(body.consumer_group.is_none());
53        assert!(body.client_id.is_none());
54        assert!(!body.only_this_broker);
55        assert!(body.mq_set.is_empty());
56    }
57
58    #[test]
59    fn test_lock_batch_request_body_serialization() {
60        let mut mq_set = HashSet::new();
61        mq_set.insert(MessageQueue::from_parts("topic", "broker", 1));
62        let body = LockBatchRequestBody {
63            consumer_group: Some(CheetahString::from("group")),
64            client_id: Some(CheetahString::from("client")),
65            only_this_broker: true,
66            mq_set,
67        };
68        let json = serde_json::to_string(&body).unwrap();
69        assert!(json.contains("consumerGroup"));
70        assert!(json.contains("clientId"));
71        assert!(json.contains("onlyThisBroker"));
72        assert!(json.contains("mqSet"));
73    }
74
75    #[test]
76    fn test_lock_batch_request_body_deserialization() {
77        let json = r#"{"consumerGroup":"group","clientId":"client","onlyThisBroker":true,"mqSet":[{"topic":"topic","brokerName":"broker","queueId":1}]}"#;
78        let body: LockBatchRequestBody = serde_json::from_str(json).unwrap();
79        assert_eq!(body.consumer_group.unwrap(), "group");
80        assert_eq!(body.client_id.unwrap(), "client");
81        assert!(body.only_this_broker);
82        assert_eq!(body.mq_set.len(), 1);
83    }
84
85    #[test]
86    fn test_lock_batch_request_body_display() {
87        let body = LockBatchRequestBody {
88            consumer_group: Some(CheetahString::from("group")),
89            client_id: Some(CheetahString::from("client")),
90            only_this_broker: true,
91            mq_set: HashSet::new(),
92        };
93        let display = format!("{}", body);
94        let expected =
95            "LockBatchRequestBody [consumer_group=group, client_id=client, only_this_broker=true, mq_set={}]";
96        assert_eq!(display, expected);
97    }
98}