rocketmq_remoting/protocol/admin/
rollback_stats.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use std::fmt::Display;
18
19use cheetah_string::CheetahString;
20use serde::Deserialize;
21use serde::Serialize;
22
23#[derive(Debug, Clone, Serialize, Deserialize, Default)]
24#[serde(rename_all = "camelCase")]
25pub struct RollbackStats {
26    pub broker_name: CheetahString,
27    pub queue_id: i64,
28    pub broker_offset: i64,
29    pub consumer_offset: i64,
30    pub timestamp_offset: i64,
31    pub rollback_offset: i64,
32}
33
34impl Display for RollbackStats {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        write!(
37            f,
38            "RollbackStats [brokerName={}, queueId={}, brokerOffset={}, consumerOffset={}, \
39             timestampOffset={}, rollbackOffset={}]",
40            self.broker_name,
41            self.queue_id,
42            self.broker_offset,
43            self.consumer_offset,
44            self.timestamp_offset,
45            self.rollback_offset
46        )
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use cheetah_string::CheetahString;
53
54    use super::*;
55
56    #[test]
57    fn rollback_stats_default_values() {
58        let stats: RollbackStats = Default::default();
59        assert_eq!(stats.broker_name, CheetahString::from(""));
60        assert_eq!(stats.queue_id, 0);
61        assert_eq!(stats.broker_offset, 0);
62        assert_eq!(stats.consumer_offset, 0);
63        assert_eq!(stats.timestamp_offset, 0);
64        assert_eq!(stats.rollback_offset, 0);
65    }
66
67    #[test]
68    fn rollback_stats_serialization() {
69        let stats = RollbackStats {
70            broker_name: CheetahString::from("broker1"),
71            queue_id: 1,
72            broker_offset: 100,
73            consumer_offset: 200,
74            timestamp_offset: 300,
75            rollback_offset: 400,
76        };
77        let serialized = serde_json::to_string(&stats).unwrap();
78        assert_eq!(
79            serialized,
80            r#"{"brokerName":"broker1","queueId":1,"brokerOffset":100,"consumerOffset":200,"timestampOffset":300,"rollbackOffset":400}"#
81        );
82    }
83
84    #[test]
85    fn rollback_stats_deserialization() {
86        let json = r#"{"brokerName":"broker1","queueId":1,"brokerOffset":100,"consumerOffset":200,"timestampOffset":300,"rollbackOffset":400}"#;
87        let deserialized: RollbackStats = serde_json::from_str(json).unwrap();
88        assert_eq!(deserialized.broker_name, CheetahString::from("broker1"));
89        assert_eq!(deserialized.queue_id, 1);
90        assert_eq!(deserialized.broker_offset, 100);
91        assert_eq!(deserialized.consumer_offset, 200);
92        assert_eq!(deserialized.timestamp_offset, 300);
93        assert_eq!(deserialized.rollback_offset, 400);
94    }
95
96    #[test]
97    fn rollback_stats_display_format() {
98        let stats = RollbackStats {
99            broker_name: CheetahString::from("broker1"),
100            queue_id: 1,
101            broker_offset: 100,
102            consumer_offset: 200,
103            timestamp_offset: 300,
104            rollback_offset: 400,
105        };
106        let display = format!("{}", stats);
107        assert_eq!(
108            display,
109            "RollbackStats [brokerName=broker1, queueId=1, brokerOffset=100, consumerOffset=200, \
110             timestampOffset=300, rollbackOffset=400]"
111        );
112    }
113}