Skip to main content

rocketmq_remoting/protocol/admin/
topic_offset.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 serde::Deserialize;
16use serde::Serialize;
17
18#[derive(Debug, Clone, Serialize, Deserialize, Default)]
19#[serde(rename_all = "camelCase")]
20pub struct TopicOffset {
21    min_offset: i64,
22    max_offset: i64,
23    last_update_timestamp: i64,
24}
25
26impl TopicOffset {
27    pub fn new() -> Self {
28        Self {
29            min_offset: 0,
30            max_offset: 0,
31            last_update_timestamp: 0,
32        }
33    }
34
35    pub fn get_min_offset(&self) -> i64 {
36        self.min_offset
37    }
38
39    pub fn set_min_offset(&mut self, min_offset: i64) {
40        self.min_offset = min_offset;
41    }
42
43    pub fn get_max_offset(&self) -> i64 {
44        self.max_offset
45    }
46
47    pub fn set_max_offset(&mut self, max_offset: i64) {
48        self.max_offset = max_offset;
49    }
50
51    pub fn get_last_update_timestamp(&self) -> i64 {
52        self.last_update_timestamp
53    }
54
55    pub fn set_last_update_timestamp(&mut self, last_update_timestamp: i64) {
56        self.last_update_timestamp = last_update_timestamp;
57    }
58}
59
60impl std::fmt::Display for TopicOffset {
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        write!(
63            f,
64            "TopicOffset{{min_offset={}, max_offset={}, last_update_timestamp={}}}",
65            self.min_offset, self.max_offset, self.last_update_timestamp
66        )
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn test_topic_offset_default_and_new() {
76        let offset = TopicOffset::default();
77        assert_eq!(offset.get_min_offset(), 0);
78        assert_eq!(offset.get_max_offset(), 0);
79        assert_eq!(offset.get_last_update_timestamp(), 0);
80
81        let offset = TopicOffset::new();
82        assert_eq!(offset.get_min_offset(), 0);
83        assert_eq!(offset.get_max_offset(), 0);
84        assert_eq!(offset.get_last_update_timestamp(), 0);
85    }
86
87    #[test]
88    fn test_topic_offset_setters_and_getters() {
89        let mut offset = TopicOffset::new();
90        offset.set_min_offset(10);
91        offset.set_max_offset(20);
92        offset.set_last_update_timestamp(1000);
93
94        assert_eq!(offset.get_min_offset(), 10);
95        assert_eq!(offset.get_max_offset(), 20);
96        assert_eq!(offset.get_last_update_timestamp(), 1000);
97    }
98
99    #[test]
100    fn test_topic_offset_serialization_and_deserialization() {
101        let mut offset = TopicOffset::new();
102        offset.set_min_offset(-10);
103        offset.set_max_offset(-20);
104        offset.set_last_update_timestamp(-1000);
105
106        let serialized = serde_json::to_string(&offset).unwrap();
107        let expected = r#"{"minOffset":-10,"maxOffset":-20,"lastUpdateTimestamp":-1000}"#;
108        assert_eq!(serialized, expected);
109
110        let deserialized: TopicOffset = serde_json::from_str(&serialized).unwrap();
111        assert_eq!(deserialized.get_min_offset(), -10);
112        assert_eq!(deserialized.get_max_offset(), -20);
113        assert_eq!(deserialized.get_last_update_timestamp(), -1000);
114    }
115
116    #[test]
117    fn test_topic_offset_display() {
118        let mut offset = TopicOffset::new();
119        offset.set_min_offset(10);
120        offset.set_max_offset(20);
121        offset.set_last_update_timestamp(1000);
122
123        let display = format!("{}", offset);
124        assert_eq!(
125            display,
126            "TopicOffset{min_offset=10, max_offset=20, last_update_timestamp=1000}"
127        );
128    }
129}