Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/axum-http-tracker-server/tests/server/v1/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ mod for_all_config_modes {
&info_hash,
&PeerBuilder::default()
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
.with_bytes_pending_to_download(1)
.with_bytes_left_to_download(1)
.build(),
)
.await;
Expand Down Expand Up @@ -1053,7 +1053,7 @@ mod for_all_config_modes {
&info_hash,
&PeerBuilder::default()
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
.with_no_bytes_pending_to_download()
.with_no_bytes_left_to_download()
.build(),
)
.await;
Expand Down Expand Up @@ -1286,7 +1286,7 @@ mod configured_as_whitelisted {
&info_hash,
&PeerBuilder::default()
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
.with_bytes_pending_to_download(1)
.with_bytes_left_to_download(1)
.build(),
)
.await;
Expand Down Expand Up @@ -1323,7 +1323,7 @@ mod configured_as_whitelisted {
&info_hash,
&PeerBuilder::default()
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
.with_bytes_pending_to_download(1)
.with_bytes_left_to_download(1)
.build(),
)
.await;
Expand Down Expand Up @@ -1500,7 +1500,7 @@ mod configured_as_private {
&info_hash,
&PeerBuilder::default()
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
.with_bytes_pending_to_download(1)
.with_bytes_left_to_download(1)
.build(),
)
.await;
Expand Down Expand Up @@ -1532,7 +1532,7 @@ mod configured_as_private {
&info_hash,
&PeerBuilder::default()
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
.with_bytes_pending_to_download(1)
.with_bytes_left_to_download(1)
.build(),
)
.await;
Expand Down Expand Up @@ -1584,7 +1584,7 @@ mod configured_as_private {
&info_hash,
&PeerBuilder::default()
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
.with_bytes_pending_to_download(1)
.with_bytes_left_to_download(1)
.build(),
)
.await;
Expand Down
33 changes: 26 additions & 7 deletions packages/primitives/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,21 +558,30 @@ pub mod fixture {
self
}

#[allow(dead_code)]
#[must_use]
pub fn with_bytes_pending_to_download(mut self, left: i64) -> Self {
pub fn with_peer_address(mut self, peer_addr: SocketAddr) -> Self {
self.peer.peer_addr = peer_addr;
self
}

#[must_use]
pub fn updated_on(mut self, updated: DurationSinceUnixEpoch) -> Self {
self.peer.updated = updated;
self
}

#[must_use]
pub fn with_bytes_left_to_download(mut self, left: i64) -> Self {
self.peer.left = NumberOfBytes::new(left);
self
}

#[allow(dead_code)]
#[must_use]
pub fn with_no_bytes_pending_to_download(mut self) -> Self {
pub fn with_no_bytes_left_to_download(mut self) -> Self {
self.peer.left = NumberOfBytes::new(0);
self
}

#[allow(dead_code)]
#[must_use]
pub fn last_updated_on(mut self, updated: DurationSinceUnixEpoch) -> Self {
self.peer.updated = updated;
Expand All @@ -585,13 +594,23 @@ pub mod fixture {
self
}

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

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

#[must_use]
pub fn build(self) -> Peer {
self.into()
}

#[allow(dead_code)]
#[must_use]
pub fn into(self) -> Peer {
self.peer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,79 +1,15 @@
use std::net::SocketAddr;

use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
use torrust_tracker_clock::clock::Time;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};

use crate::CurrentClock;

#[derive(Debug, Default)]
struct TorrentPeerBuilder {
peer: peer::Peer,
}

#[allow(dead_code)]
impl TorrentPeerBuilder {
#[must_use]
fn new() -> Self {
Self {
peer: peer::Peer {
updated: CurrentClock::now(),
..Default::default()
},
}
}

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

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

#[must_use]
fn with_peer_address(mut self, peer_addr: SocketAddr) -> Self {
self.peer.peer_addr = peer_addr;
self
}

#[must_use]
fn with_peer_id(mut self, peer_id: PeerId) -> Self {
self.peer.peer_id = peer_id;
self
}

#[must_use]
fn with_number_of_bytes_left(mut self, left: i64) -> Self {
self.peer.left = NumberOfBytes::new(left);
self
}

#[must_use]
fn updated_at(mut self, updated: DurationSinceUnixEpoch) -> Self {
self.peer.updated = updated;
self
}

#[must_use]
fn into(self) -> peer::Peer {
self.peer
}
}
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
use torrust_tracker_primitives::peer::{self};

/// A torrent seeder is a peer with 0 bytes left to download which
/// has not announced it has stopped
#[must_use]
pub fn a_completed_peer(id: i32) -> peer::Peer {
let peer_id = peer::Id::new(id);
TorrentPeerBuilder::new()
.with_number_of_bytes_left(0)
PeerBuilder::default()
.with_bytes_left_to_download(0)
.with_event_completed()
.with_peer_id(*peer_id)
.with_peer_id(&peer_id)
.into()
}

Expand All @@ -82,9 +18,9 @@ pub fn a_completed_peer(id: i32) -> peer::Peer {
#[must_use]
pub fn a_started_peer(id: i32) -> peer::Peer {
let peer_id = peer::Id::new(id);
TorrentPeerBuilder::new()
.with_number_of_bytes_left(1)
PeerBuilder::default()
.with_bytes_left_to_download(1)
.with_event_started()
.with_peer_id(*peer_id)
.with_peer_id(&peer_id)
.into()
}
27 changes: 14 additions & 13 deletions packages/udp-tracker-server/src/handlers/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ mod tests {
use bittorrent_udp_tracker_core::connection_cookie::{gen_remote_fingerprint, make};
use mockall::predicate::eq;
use torrust_tracker_events::bus::SenderStatus;
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};

use crate::event::{ConnectionContext, Event, UdpRequestKind};
Expand All @@ -216,7 +217,6 @@ mod tests {
initialize_core_tracker_services_for_default_tracker_configuration,
initialize_core_tracker_services_for_public_tracker, sample_cookie_valid_range, sample_ipv4_socket_address,
sample_issue_time, CoreTrackerServices, CoreUdpTrackerServices, MockUdpServerStatsEventSender,
TorrentPeerBuilder,
};

#[tokio::test]
Expand Down Expand Up @@ -258,8 +258,8 @@ mod tests {
.get_torrent_peers(&info_hash.0.into())
.await;

let expected_peer = TorrentPeerBuilder::new()
.with_peer_id(peer_id)
let expected_peer = PeerBuilder::default()
.with_peer_id(&peer_id)
.with_peer_address(SocketAddr::new(IpAddr::V4(client_ip), client_port))
.updated_on(peers[0].updated)
.into();
Expand Down Expand Up @@ -364,8 +364,8 @@ mod tests {
let client_port = 8080;
let peer_id = AquaticPeerId([255u8; 20]);

let peer_using_ipv6 = TorrentPeerBuilder::new()
.with_peer_id(peer_id)
let peer_using_ipv6 = PeerBuilder::default()
.with_peer_id(&peer_id)
.with_peer_address(SocketAddr::new(IpAddr::V6(client_ip_v6), client_port))
.into();

Expand Down Expand Up @@ -466,13 +466,13 @@ mod tests {

use aquatic_udp_protocol::{InfoHash as AquaticInfoHash, PeerId as AquaticPeerId};
use bittorrent_udp_tracker_core::connection_cookie::{gen_remote_fingerprint, make};
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};

use crate::handlers::announce::tests::announce_request::AnnounceRequestBuilder;
use crate::handlers::handle_announce;
use crate::handlers::tests::{
initialize_core_tracker_services_for_public_tracker, sample_cookie_valid_range, sample_issue_time,
TorrentPeerBuilder,
};

#[tokio::test]
Expand Down Expand Up @@ -516,8 +516,8 @@ mod tests {

let external_ip_in_tracker_configuration = core_tracker_services.core_config.net.external_ip.unwrap();

let expected_peer = TorrentPeerBuilder::new()
.with_peer_id(peer_id)
let expected_peer = PeerBuilder::default()
.with_peer_id(&peer_id)
.with_peer_address(SocketAddr::new(external_ip_in_tracker_configuration, client_port))
.updated_on(peers[0].updated)
.into();
Expand Down Expand Up @@ -547,6 +547,7 @@ mod tests {
use mockall::predicate::eq;
use torrust_tracker_configuration::Core;
use torrust_tracker_events::bus::SenderStatus;
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};

use crate::event::{ConnectionContext, Event, UdpRequestKind};
Expand All @@ -555,7 +556,7 @@ mod tests {
use crate::handlers::tests::{
initialize_core_tracker_services_for_default_tracker_configuration,
initialize_core_tracker_services_for_public_tracker, sample_cookie_valid_range, sample_ipv6_remote_addr,
sample_issue_time, MockUdpServerStatsEventSender, TorrentPeerBuilder,
sample_issue_time, MockUdpServerStatsEventSender,
};

#[tokio::test]
Expand Down Expand Up @@ -598,8 +599,8 @@ mod tests {
.get_torrent_peers(&info_hash.0.into())
.await;

let expected_peer = TorrentPeerBuilder::new()
.with_peer_id(peer_id)
let expected_peer = PeerBuilder::default()
.with_peer_id(&peer_id)
.with_peer_address(SocketAddr::new(IpAddr::V6(client_ip_v6), client_port))
.updated_on(peers[0].updated)
.into();
Expand Down Expand Up @@ -707,8 +708,8 @@ mod tests {
let client_port = 8080;
let peer_id = AquaticPeerId([255u8; 20]);

let peer_using_ipv4 = TorrentPeerBuilder::new()
.with_peer_id(peer_id)
let peer_using_ipv4 = PeerBuilder::default()
.with_peer_id(&peer_id)
.with_peer_address(SocketAddr::new(IpAddr::V4(client_ip_v4), client_port))
.into();

Expand Down
Loading
Loading