Skip to content

Commit b13797e

Browse files
committed
test: [#1358] add tests for events in torrent-repository pkg
1 parent f71211f commit b13797e

3 files changed

Lines changed: 406 additions & 2 deletions

File tree

packages/primitives/src/peer.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@
2222
//! };
2323
//! ```
2424
25+
use std::fmt;
2526
use std::net::{IpAddr, SocketAddr};
2627
use std::ops::{Deref, DerefMut};
28+
use std::str::FromStr;
2729
use std::sync::Arc;
2830

2931
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
30-
use derive_more::Display;
3132
use serde::Serialize;
3233
use zerocopy::FromBytes as _;
3334

3435
use crate::DurationSinceUnixEpoch;
3536

3637
pub type PeerAnnouncement = Peer;
3738

38-
#[derive(Debug, Display, Serialize, Copy, Clone, PartialEq, Eq, Hash)]
39+
#[derive(Debug, Serialize, Copy, Clone, PartialEq, Eq, Hash)]
3940
#[serde(rename_all_fields = "lowercase")]
4041
pub enum PeerRole {
4142
Seeder,
@@ -53,6 +54,39 @@ impl PeerRole {
5354
}
5455
}
5556

57+
impl fmt::Display for PeerRole {
58+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59+
match self {
60+
PeerRole::Seeder => write!(f, "seeder"),
61+
PeerRole::Leecher => write!(f, "leecher"),
62+
}
63+
}
64+
}
65+
66+
impl FromStr for PeerRole {
67+
type Err = ParsePeerRoleError;
68+
69+
fn from_str(s: &str) -> Result<Self, Self::Err> {
70+
match s.to_lowercase().as_str() {
71+
"seeder" => Ok(PeerRole::Seeder),
72+
"leecher" => Ok(PeerRole::Leecher),
73+
_ => Err(ParsePeerRoleError::InvalidPeerRole {
74+
location: Location::caller(),
75+
raw_param: s.to_string(),
76+
}),
77+
}
78+
}
79+
}
80+
81+
#[derive(Error, Debug)]
82+
pub enum ParsePeerRoleError {
83+
#[error("invalid param {raw_param} in {location}")]
84+
InvalidPeerRole {
85+
location: &'static Location<'static>,
86+
raw_param: String,
87+
},
88+
}
89+
5690
/// Peer struct used by the core `Tracker`.
5791
///
5892
/// A sample peer:
@@ -264,6 +298,14 @@ impl Peer {
264298
..self
265299
}
266300
}
301+
302+
#[must_use]
303+
pub fn into_seeder(self) -> Self {
304+
Self {
305+
left: NumberOfBytes::new(0),
306+
..self
307+
}
308+
}
267309
}
268310

269311
use std::panic::Location;

packages/torrent-repository/src/event.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,29 @@ pub mod bus {
8383

8484
pub type EventBus = torrust_tracker_events::bus::EventBus<Event>;
8585
}
86+
87+
#[cfg(test)]
88+
pub mod test {
89+
90+
use torrust_tracker_primitives::peer::Peer;
91+
92+
use super::Event;
93+
use crate::tests::sample_info_hash;
94+
95+
#[test]
96+
fn events_should_be_comparable() {
97+
let info_hash = sample_info_hash();
98+
99+
let event1 = Event::TorrentAdded {
100+
info_hash,
101+
announcement: Peer::default(),
102+
};
103+
104+
let event2 = Event::TorrentRemoved { info_hash };
105+
106+
let event1_clone = event1.clone();
107+
108+
assert!(event1 == event1_clone);
109+
assert!(event1 != event2);
110+
}
111+
}

0 commit comments

Comments
 (0)