1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! Structures and traits for custom backend development and testing process.
pub use hitbox_backend::{Backend, BackendError, Delete, DeleteStatus, Get, Lock, LockStatus, Set};

#[doc(hidden)]
/// Mocked backend implementation module.
pub mod backend {
    use super::*;
    use actix::prelude::*;

    #[derive(Debug, Clone, PartialEq)]
    pub enum MockMessage {
        Get(Get),
        Set(Set),
        Delete(Delete),
        Lock(Lock),
    }

    pub struct MockBackend {
        pub messages: Vec<MockMessage>,
    }

    impl MockBackend {
        pub fn new() -> Self {
            MockBackend::default()
        }
    }

    impl Default for MockBackend {
        fn default() -> Self {
            MockBackend {
                messages: Vec::with_capacity(10),
            }
        }
    }

    impl Actor for MockBackend {
        type Context = Context<Self>;
    }

    impl Backend for MockBackend {
        type Actor = Self;
        type Context = Context<Self>;
    }

    impl Handler<Get> for MockBackend {
        type Result = <Get as Message>::Result;

        fn handle(&mut self, msg: Get, _: &mut Self::Context) -> Self::Result {
            self.messages.push(MockMessage::Get(msg));
            Ok(None)
        }
    }

    impl Handler<Set> for MockBackend {
        type Result = <Set as Message>::Result;

        fn handle(&mut self, msg: Set, _: &mut Self::Context) -> Self::Result {
            self.messages.push(MockMessage::Set(msg));
            Ok("".to_owned())
        }
    }

    impl Handler<Lock> for MockBackend {
        type Result = <Lock as Message>::Result;

        fn handle(&mut self, msg: Lock, _: &mut Self::Context) -> Self::Result {
            self.messages.push(MockMessage::Lock(msg));
            Ok(LockStatus::Locked)
        }
    }

    impl Handler<Delete> for MockBackend {
        type Result = <Delete as Message>::Result;

        fn handle(&mut self, msg: Delete, _: &mut Self::Context) -> Self::Result {
            self.messages.push(MockMessage::Delete(msg));
            Ok(DeleteStatus::Missing)
        }
    }

    #[derive(Message)]
    #[rtype(result = "GetMessagesResult")]
    pub struct GetMessages;

    #[derive(MessageResponse)]
    pub struct GetMessagesResult(pub Vec<MockMessage>);

    impl Handler<GetMessages> for MockBackend {
        type Result = GetMessagesResult;

        fn handle(&mut self, _msg: GetMessages, _: &mut Self::Context) -> Self::Result {
            GetMessagesResult(self.messages.clone())
        }
    }
}