common_meta/rpc/
lock.rs

1// Copyright 2023 Greptime Team
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 api::v1::meta::{
16    LockRequest as PbLockRequest, LockResponse as PbLockResponse, UnlockRequest as PbUnlockRequest,
17};
18
19#[derive(Debug)]
20pub struct LockRequest {
21    pub name: Vec<u8>,
22    pub expire_secs: i64,
23}
24
25impl From<LockRequest> for PbLockRequest {
26    fn from(req: LockRequest) -> Self {
27        Self {
28            header: None,
29            name: req.name,
30            expire_secs: req.expire_secs,
31        }
32    }
33}
34
35#[derive(Debug)]
36pub struct LockResponse {
37    pub key: Vec<u8>,
38}
39
40impl From<PbLockResponse> for LockResponse {
41    fn from(resp: PbLockResponse) -> Self {
42        Self { key: resp.key }
43    }
44}
45
46#[derive(Debug)]
47pub struct UnlockRequest {
48    pub key: Vec<u8>,
49}
50
51impl From<UnlockRequest> for PbUnlockRequest {
52    fn from(req: UnlockRequest) -> Self {
53        Self {
54            header: None,
55            key: req.key,
56        }
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use api::v1::meta::{
63        LockRequest as PbLockRequest, LockResponse as PbLockResponse,
64        UnlockRequest as PbUnlockRequest,
65    };
66
67    use super::LockRequest;
68    use crate::rpc::lock::{LockResponse, UnlockRequest};
69
70    #[test]
71    fn test_convert_lock_req() {
72        let lock_req = LockRequest {
73            name: "lock_1".as_bytes().to_vec(),
74            expire_secs: 1,
75        };
76        let pb_lock_req: PbLockRequest = lock_req.into();
77
78        let expected = PbLockRequest {
79            header: None,
80            name: "lock_1".as_bytes().to_vec(),
81            expire_secs: 1,
82        };
83
84        assert_eq!(expected, pb_lock_req);
85    }
86
87    #[test]
88    fn test_convert_unlock_req() {
89        let unlock_req = UnlockRequest {
90            key: "lock_1_12378123".as_bytes().to_vec(),
91        };
92        let pb_unlock_req: PbUnlockRequest = unlock_req.into();
93
94        let expected = PbUnlockRequest {
95            header: None,
96            key: "lock_1_12378123".as_bytes().to_vec(),
97        };
98
99        assert_eq!(expected, pb_unlock_req);
100    }
101
102    #[test]
103    fn test_convert_lock_response() {
104        let pb_lock_resp = PbLockResponse {
105            header: None,
106            key: "lock_1_12378123".as_bytes().to_vec(),
107        };
108
109        let lock_resp: LockResponse = pb_lock_resp.into();
110
111        let expected_key = "lock_1_12378123".as_bytes().to_vec();
112
113        assert_eq!(expected_key, lock_resp.key);
114    }
115}