Skip to main content

rocketmq_protocol/protocol/body/response/
lock_batch_response_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;
16
17use rocketmq_model::message::MessageQueue;
18use serde::Deserialize;
19use serde::Serialize;
20
21#[derive(Serialize, Deserialize, Debug, Default)]
22pub struct LockBatchResponseBody {
23    #[serde(rename = "lockOKMQSet")]
24    pub lock_ok_mq_set: HashSet<MessageQueue>,
25}
26
27#[cfg(test)]
28mod test {
29    use super::*;
30
31    #[test]
32    fn serde_contract_uses_the_java_field_name_and_round_trips_the_queue() {
33        let queue = MessageQueue::from_parts("some_test_topic", "TEST_BROKER", 1);
34        let body = LockBatchResponseBody {
35            lock_ok_mq_set: HashSet::from([queue]),
36        };
37
38        let value = serde_json::to_value(&body).expect("serialize lock batch response");
39        let entries = value["lockOKMQSet"].as_array().expect("lockOKMQSet should be an array");
40        assert_eq!(entries.len(), 1);
41        assert_eq!(entries[0]["topic"], "some_test_topic");
42        assert_eq!(entries[0]["brokerName"], "TEST_BROKER");
43        assert_eq!(entries[0]["queueId"], 1);
44
45        let decoded: LockBatchResponseBody = serde_json::from_value(value).expect("deserialize lock batch response");
46        assert_eq!(decoded.lock_ok_mq_set.len(), 1);
47        let mq = decoded.lock_ok_mq_set.iter().next().expect("decoded queue");
48        assert_eq!(mq.topic(), "some_test_topic");
49        assert_eq!(mq.broker_name(), "TEST_BROKER");
50        assert_eq!(mq.queue_id(), 1);
51    }
52}