forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.rs
More file actions
111 lines (87 loc) · 2.58 KB
/
event.rs
File metadata and controls
111 lines (87 loc) · 2.58 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use bittorrent_primitives::info_hash::InfoHash;
use torrust_tracker_primitives::peer::{Peer, PeerAnnouncement};
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Event {
TorrentAdded {
info_hash: InfoHash,
announcement: PeerAnnouncement,
},
TorrentRemoved {
info_hash: InfoHash,
},
PeerAdded {
info_hash: InfoHash,
peer: Peer,
},
PeerRemoved {
info_hash: InfoHash,
peer: Peer,
},
PeerUpdated {
info_hash: InfoHash,
old_peer: Peer,
new_peer: Peer,
},
PeerDownloadCompleted {
info_hash: InfoHash,
peer: Peer,
},
}
pub mod sender {
use std::sync::Arc;
use super::Event;
pub type Sender = Option<Arc<dyn torrust_tracker_events::sender::Sender<Event = Event>>>;
pub type Broadcaster = torrust_tracker_events::broadcaster::Broadcaster<Event>;
#[cfg(test)]
pub mod tests {
use futures::future::{self, BoxFuture};
use mockall::mock;
use mockall::predicate::eq;
use torrust_tracker_events::sender::{SendError, Sender};
use crate::event::Event;
mock! {
pub EventSender {}
impl Sender for EventSender {
type Event = Event;
fn send(&self, event: Event) -> BoxFuture<'static,Option<Result<usize,SendError<Event> > > > ;
}
}
pub fn expect_event(mock: &mut MockEventSender, event: Event) {
mock.expect_send()
.with(eq(event))
.times(1)
.returning(|_| Box::pin(future::ready(Some(Ok(1)))));
}
pub fn expect_event_sequence(mock: &mut MockEventSender, event: Vec<Event>) {
for e in event {
expect_event(mock, e);
}
}
}
}
pub mod receiver {
use super::Event;
pub type Receiver = Box<dyn torrust_tracker_events::receiver::Receiver<Event = Event>>;
}
pub mod bus {
use crate::event::Event;
pub type EventBus = torrust_tracker_events::bus::EventBus<Event>;
}
#[cfg(test)]
pub mod test {
use torrust_tracker_primitives::peer::Peer;
use super::Event;
use crate::tests::sample_info_hash;
#[test]
fn events_should_be_comparable() {
let info_hash = sample_info_hash();
let event1 = Event::TorrentAdded {
info_hash,
announcement: Peer::default(),
};
let event2 = Event::TorrentRemoved { info_hash };
let event1_clone = event1.clone();
assert!(event1 == event1_clone);
assert!(event1 != event2);
}
}