Skip to main content

rocketmq_protocol/protocol/body/
sync_state_set_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 serde::Deserialize;
18use serde::Serialize;
19
20#[derive(Debug, Serialize, Deserialize, Default)]
21#[serde(rename_all = "camelCase")]
22pub struct SyncStateSet {
23    sync_state_set: Option<HashSet<i64>>,
24    sync_state_set_epoch: i32,
25}
26
27impl SyncStateSet {
28    pub fn new() -> SyncStateSet {
29        SyncStateSet {
30            sync_state_set: None,
31            sync_state_set_epoch: 0,
32        }
33    }
34
35    pub fn take_sync_state_set(&mut self) -> Option<HashSet<i64>> {
36        self.sync_state_set.take()
37    }
38
39    pub fn with_values(sync_state_set: HashSet<i64>, sync_state_set_epoch: i32) -> SyncStateSet {
40        SyncStateSet {
41            sync_state_set: Some(sync_state_set),
42            sync_state_set_epoch,
43        }
44    }
45
46    pub fn set_sync_state_set(&mut self, sync_state_set: HashSet<i64>) {
47        self.sync_state_set = Some(sync_state_set);
48    }
49
50    pub fn set_sync_state_set_epoch(&mut self, sync_state_set_epoch: i32) {
51        self.sync_state_set_epoch = sync_state_set_epoch;
52    }
53
54    pub fn get_sync_state_set(&self) -> Option<&HashSet<i64>> {
55        self.sync_state_set.as_ref()
56    }
57
58    pub fn get_sync_state_set_epoch(&self) -> i32 {
59        self.sync_state_set_epoch
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn sync_state_set_serializes_correctly() {
69        let mut set = HashSet::new();
70        set.insert(0);
71        set.insert(1);
72
73        let sync_state = SyncStateSet::with_values(set, 5);
74        let json = serde_json::to_string(&sync_state).unwrap();
75
76        assert!(json.contains("syncStateSet"));
77        assert!(json.contains("syncStateSetEpoch"));
78        assert!(json.contains("5"));
79    }
80
81    #[test]
82    fn sync_state_set_deserializes_correctly() {
83        let json = r#"{
84            "syncStateSet": [0, 1, 2],
85            "syncStateSetEpoch": 10
86        }"#;
87
88        let sync_state: SyncStateSet = serde_json::from_str(json).unwrap();
89        assert!(sync_state.get_sync_state_set().is_some());
90        assert_eq!(sync_state.get_sync_state_set().unwrap().len(), 3);
91        assert!(sync_state.get_sync_state_set().unwrap().contains(&0));
92        assert!(sync_state.get_sync_state_set().unwrap().contains(&1));
93        assert!(sync_state.get_sync_state_set().unwrap().contains(&2));
94        assert_eq!(sync_state.get_sync_state_set_epoch(), 10);
95    }
96
97    #[test]
98    fn sync_state_set_take_sync_state_set() {
99        let mut set = HashSet::new();
100        set.insert(1);
101        set.insert(2);
102
103        let mut sync_state = SyncStateSet::with_values(set, 5);
104
105        let taken = sync_state.take_sync_state_set();
106        assert!(taken.is_some());
107        assert_eq!(taken.unwrap().len(), 2);
108
109        assert!(sync_state.get_sync_state_set().is_none());
110    }
111
112    #[test]
113    fn sync_state_set_setters_and_getters() {
114        let mut sync_state = SyncStateSet::new();
115
116        let mut set = HashSet::new();
117        set.insert(100);
118        set.insert(200);
119
120        sync_state.set_sync_state_set(set);
121        sync_state.set_sync_state_set_epoch(10);
122
123        assert!(sync_state.get_sync_state_set().is_some());
124        assert_eq!(sync_state.get_sync_state_set().unwrap().len(), 2);
125        assert!(sync_state.get_sync_state_set().unwrap().contains(&100));
126        assert!(sync_state.get_sync_state_set().unwrap().contains(&200));
127        assert_eq!(sync_state.get_sync_state_set_epoch(), 10);
128    }
129}