Skip to content

Commit

Permalink
dev: complete torrent entry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Mar 22, 2024
1 parent 9c7728b commit 14ad62d
Show file tree
Hide file tree
Showing 16 changed files with 521 additions and 234 deletions.
1 change: 1 addition & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"ostr",
"Pando",
"peekable",
"peerlist",
"proot",
"proto",
"Quickstart",
Expand Down
2 changes: 1 addition & 1 deletion packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ use torrust_tracker_primitives::{DatabaseDriver, TrackerMode};
/// The maximum number of returned peers for a torrent.
pub const TORRENT_PEERS_LIMIT: usize = 74;

#[derive(Copy, Clone, Debug, PartialEq, Default, Constructor)]
#[derive(Copy, Clone, Debug, PartialEq, Constructor)]
pub struct TrackerPolicy {
pub remove_peerless_torrents: bool,
pub max_peer_timeout: u32,
Expand Down
10 changes: 10 additions & 0 deletions packages/primitives/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ impl From<[u8; 20]> for Id {
}
}

impl From<i32> for Id {
fn from(number: i32) -> Self {
let peer_id = number.to_le_bytes();
Id::from([
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, peer_id[0], peer_id[1], peer_id[2],
peer_id[3],
])
}
}

impl TryFrom<Vec<u8>> for Id {
type Error = IdConversionError;

Expand Down
20 changes: 9 additions & 11 deletions packages/torrent-repository/src/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub trait Entry {
fn get_stats(&self) -> SwarmMetadata;

/// Returns True if Still a Valid Entry according to the Tracker Policy
fn is_not_zombie(&self, policy: &TrackerPolicy) -> bool;
fn is_good(&self, policy: &TrackerPolicy) -> bool;

/// Returns True if the Peers is Empty
fn peers_is_empty(&self) -> bool;
Expand Down Expand Up @@ -51,7 +51,7 @@ pub trait Entry {
#[allow(clippy::module_name_repetitions)]
pub trait EntrySync {
fn get_stats(&self) -> SwarmMetadata;
fn is_not_zombie(&self, policy: &TrackerPolicy) -> bool;
fn is_good(&self, policy: &TrackerPolicy) -> bool;
fn peers_is_empty(&self) -> bool;
fn get_peers_len(&self) -> usize;
fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>>;
Expand All @@ -63,15 +63,13 @@ pub trait EntrySync {

#[allow(clippy::module_name_repetitions)]
pub trait EntryAsync {
fn get_stats(self) -> impl std::future::Future<Output = SwarmMetadata> + Send;

#[allow(clippy::wrong_self_convention)]
fn is_not_zombie(self, policy: &TrackerPolicy) -> impl std::future::Future<Output = bool> + Send;
fn peers_is_empty(self) -> impl std::future::Future<Output = bool> + Send;
fn get_peers_len(self) -> impl std::future::Future<Output = usize> + Send;
fn get_peers(self, limit: Option<usize>) -> impl std::future::Future<Output = Vec<Arc<peer::Peer>>> + Send;
fn get_stats(&self) -> impl std::future::Future<Output = SwarmMetadata> + Send;
fn is_good(&self, policy: &TrackerPolicy) -> impl std::future::Future<Output = bool> + Send;
fn peers_is_empty(&self) -> impl std::future::Future<Output = bool> + Send;
fn get_peers_len(&self) -> impl std::future::Future<Output = usize> + Send;
fn get_peers(&self, limit: Option<usize>) -> impl std::future::Future<Output = Vec<Arc<peer::Peer>>> + Send;
fn get_peers_for_peer(
self,
&self,
client: &peer::Peer,
limit: Option<usize>,
) -> impl std::future::Future<Output = Vec<Arc<peer::Peer>>> + Send;
Expand All @@ -94,5 +92,5 @@ pub struct Torrent {
// #[serde(skip)]
pub(crate) peers: std::collections::BTreeMap<peer::Id, Arc<peer::Peer>>,
/// The number of peers that have ever completed downloading the torrent associated to this entry
pub(crate) completed: u32,
pub(crate) downloaded: u32,
}
4 changes: 2 additions & 2 deletions packages/torrent-repository/src/entry/mutex_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ impl EntrySync for EntryMutexStd {
self.lock().expect("it should get a lock").get_stats()
}

fn is_not_zombie(&self, policy: &TrackerPolicy) -> bool {
self.lock().expect("it should get a lock").is_not_zombie(policy)
fn is_good(&self, policy: &TrackerPolicy) -> bool {
self.lock().expect("it should get a lock").is_good(policy)
}

fn peers_is_empty(&self) -> bool {
Expand Down
14 changes: 7 additions & 7 deletions packages/torrent-repository/src/entry/mutex_tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ use super::{Entry, EntryAsync};
use crate::EntryMutexTokio;

impl EntryAsync for EntryMutexTokio {
async fn get_stats(self) -> SwarmMetadata {
async fn get_stats(&self) -> SwarmMetadata {
self.lock().await.get_stats()
}

async fn is_not_zombie(self, policy: &TrackerPolicy) -> bool {
self.lock().await.is_not_zombie(policy)
async fn is_good(&self, policy: &TrackerPolicy) -> bool {
self.lock().await.is_good(policy)
}

async fn peers_is_empty(self) -> bool {
async fn peers_is_empty(&self) -> bool {
self.lock().await.peers_is_empty()
}

async fn get_peers_len(self) -> usize {
async fn get_peers_len(&self) -> usize {
self.lock().await.get_peers_len()
}

async fn get_peers(self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
async fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
self.lock().await.get_peers(limit)
}

async fn get_peers_for_peer(self, client: &peer::Peer, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
async fn get_peers_for_peer(&self, client: &peer::Peer, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
self.lock().await.get_peers_for_peer(client, limit)
}

Expand Down
18 changes: 9 additions & 9 deletions packages/torrent-repository/src/entry/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ impl Entry for EntrySingle {
let incomplete: u32 = self.peers.len() as u32 - complete;

SwarmMetadata {
downloaded: self.completed,
downloaded: self.downloaded,
complete,
incomplete,
}
}

fn is_not_zombie(&self, policy: &TrackerPolicy) -> bool {
if policy.persistent_torrent_completed_stat && self.completed > 0 {
fn is_good(&self, policy: &TrackerPolicy) -> bool {
if policy.persistent_torrent_completed_stat && self.downloaded > 0 {
return true;
}

Expand Down Expand Up @@ -70,26 +70,26 @@ impl Entry for EntrySingle {
}

fn insert_or_update_peer(&mut self, peer: &peer::Peer) -> bool {
let mut did_torrent_stats_change: bool = false;
let mut downloaded_stats_updated: bool = false;

match peer::ReadInfo::get_event(peer) {
AnnounceEvent::Stopped => {
drop(self.peers.remove(&peer::ReadInfo::get_id(peer)));
}
AnnounceEvent::Completed => {
let peer_old = self.peers.insert(peer::ReadInfo::get_id(peer), Arc::new(*peer));
let previous = self.peers.insert(peer::ReadInfo::get_id(peer), Arc::new(*peer));
// Don't count if peer was not previously known and not already completed.
if peer_old.is_some_and(|p| p.event != AnnounceEvent::Completed) {
self.completed += 1;
did_torrent_stats_change = true;
if previous.is_some_and(|p| p.event != AnnounceEvent::Completed) {
self.downloaded += 1;
downloaded_stats_updated = true;
}
}
_ => {
drop(self.peers.insert(peer::ReadInfo::get_id(peer), Arc::new(*peer)));
}
}

did_torrent_stats_change
downloaded_stats_updated
}

fn insert_or_update_peer_and_get_stats(&mut self, peer: &peer::Peer) -> (bool, SwarmMetadata) {
Expand Down
4 changes: 2 additions & 2 deletions packages/torrent-repository/src/repository/rw_lock_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where

let entry = EntrySingle {
peers: BTreeMap::default(),
completed: *completed,
downloaded: *completed,
};

torrents.insert(*info_hash, entry);
Expand All @@ -107,6 +107,6 @@ where
fn remove_peerless_torrents(&self, policy: &TrackerPolicy) {
let mut db = self.get_torrents_mut();

db.retain(|_, e| e.is_not_zombie(policy));
db.retain(|_, e| e.is_good(policy));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ where
let entry = EntryMutexStd::new(
EntrySingle {
peers: BTreeMap::default(),
completed: *completed,
downloaded: *completed,
}
.into(),
);
Expand All @@ -118,6 +118,6 @@ where
fn remove_peerless_torrents(&self, policy: &TrackerPolicy) {
let mut db = self.get_torrents_mut();

db.retain(|_, e| e.lock().expect("it should lock entry").is_not_zombie(policy));
db.retain(|_, e| e.lock().expect("it should lock entry").is_good(policy));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ where
let entry = EntryMutexTokio::new(
EntrySingle {
peers: BTreeMap::default(),
completed: *completed,
downloaded: *completed,
}
.into(),
);
Expand Down Expand Up @@ -126,6 +126,6 @@ where
async fn remove_peerless_torrents(&self, policy: &TrackerPolicy) {
let mut db = self.get_torrents_mut();

db.retain(|_, e| e.blocking_lock().is_not_zombie(policy));
db.retain(|_, e| e.blocking_lock().is_good(policy));
}
}
4 changes: 2 additions & 2 deletions packages/torrent-repository/src/repository/rw_lock_tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ where

let entry = EntrySingle {
peers: BTreeMap::default(),
completed: *completed,
downloaded: *completed,
};

torrents.insert(*info_hash, entry);
Expand All @@ -108,6 +108,6 @@ where
async fn remove_peerless_torrents(&self, policy: &TrackerPolicy) {
let mut db = self.get_torrents_mut().await;

db.retain(|_, e| e.is_not_zombie(policy));
db.retain(|_, e| e.is_good(policy));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ where
let entry = EntryMutexStd::new(
EntrySingle {
peers: BTreeMap::default(),
completed: *completed,
downloaded: *completed,
}
.into(),
);
Expand All @@ -119,6 +119,6 @@ where
async fn remove_peerless_torrents(&self, policy: &TrackerPolicy) {
let mut db = self.get_torrents_mut().await;

db.retain(|_, e| e.lock().expect("it should lock entry").is_not_zombie(policy));
db.retain(|_, e| e.lock().expect("it should lock entry").is_good(policy));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ where
async fn get_metrics(&self) -> TorrentsMetrics {
let mut metrics = TorrentsMetrics::default();

for entry in self.get_torrents().await.values().cloned() {
for entry in self.get_torrents().await.values() {
let stats = entry.get_stats().await;
metrics.seeders += u64::from(stats.complete);
metrics.completed += u64::from(stats.downloaded);
Expand All @@ -93,7 +93,7 @@ where
let entry = EntryMutexTokio::new(
EntrySingle {
peers: BTreeMap::default(),
completed: *completed,
downloaded: *completed,
}
.into(),
);
Expand All @@ -119,6 +119,6 @@ where
async fn remove_peerless_torrents(&self, policy: &TrackerPolicy) {
let mut db = self.get_torrents_mut().await;

db.retain(|_, e| e.blocking_lock().is_not_zombie(policy));
db.retain(|_, e| e.blocking_lock().is_good(policy));
}
}
12 changes: 9 additions & 3 deletions packages/torrent-repository/tests/common/torrent_peer_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ impl TorrentPeerBuilder {
self
}

#[must_use]
pub fn with_event_started(mut self) -> Self {
self.peer.event = AnnounceEvent::Started;
self
}

#[must_use]
pub fn with_peer_address(mut self, peer_addr: SocketAddr) -> Self {
self.peer.peer_addr = peer_addr;
Expand Down Expand Up @@ -61,7 +67,7 @@ impl TorrentPeerBuilder {
/// A torrent seeder is a peer with 0 bytes left to download which
/// has not announced it has stopped
#[must_use]
pub fn a_torrent_seeder() -> peer::Peer {
pub fn a_completed_peer() -> peer::Peer {
TorrentPeerBuilder::new()
.with_number_of_bytes_left(0)
.with_event_completed()
Expand All @@ -71,9 +77,9 @@ pub fn a_torrent_seeder() -> peer::Peer {
/// A torrent leecher is a peer that is not a seeder.
/// Leecher: left > 0 OR event = Stopped
#[must_use]
pub fn a_torrent_leecher() -> peer::Peer {
pub fn a_started_peer() -> peer::Peer {
TorrentPeerBuilder::new()
.with_number_of_bytes_left(1)
.with_event_completed()
.with_event_started()
.into()
}
Loading

0 comments on commit 14ad62d

Please sign in to comment.