Skip to content

Commit a194f1e

Browse files
committed
Merge #1526: Remove duplicate code for Peer builder
85d9d35 refactor: [#1493] remove duplicate code for Peer buidler (Jose Celano) Pull request description: Remove duplicate code for Peer builder. ACKs for top commit: josecelano: ACK 85d9d35 Tree-SHA512: c2de6d0d75a662c59c7dfd70ccf2694e0586ba401d4b40c18a3eeb225f3b0819baad575d7adf43c1083cd45a1189e44c68c46b2972b02829dd80ddc69a0031a9
2 parents 6735018 + 85d9d35 commit a194f1e

File tree

6 files changed

+61
-153
lines changed

6 files changed

+61
-153
lines changed

packages/axum-http-tracker-server/tests/server/v1/contract.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ mod for_all_config_modes {
10121012
&info_hash,
10131013
&PeerBuilder::default()
10141014
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
1015-
.with_bytes_pending_to_download(1)
1015+
.with_bytes_left_to_download(1)
10161016
.build(),
10171017
)
10181018
.await;
@@ -1053,7 +1053,7 @@ mod for_all_config_modes {
10531053
&info_hash,
10541054
&PeerBuilder::default()
10551055
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
1056-
.with_no_bytes_pending_to_download()
1056+
.with_no_bytes_left_to_download()
10571057
.build(),
10581058
)
10591059
.await;
@@ -1286,7 +1286,7 @@ mod configured_as_whitelisted {
12861286
&info_hash,
12871287
&PeerBuilder::default()
12881288
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
1289-
.with_bytes_pending_to_download(1)
1289+
.with_bytes_left_to_download(1)
12901290
.build(),
12911291
)
12921292
.await;
@@ -1323,7 +1323,7 @@ mod configured_as_whitelisted {
13231323
&info_hash,
13241324
&PeerBuilder::default()
13251325
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
1326-
.with_bytes_pending_to_download(1)
1326+
.with_bytes_left_to_download(1)
13271327
.build(),
13281328
)
13291329
.await;
@@ -1500,7 +1500,7 @@ mod configured_as_private {
15001500
&info_hash,
15011501
&PeerBuilder::default()
15021502
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
1503-
.with_bytes_pending_to_download(1)
1503+
.with_bytes_left_to_download(1)
15041504
.build(),
15051505
)
15061506
.await;
@@ -1532,7 +1532,7 @@ mod configured_as_private {
15321532
&info_hash,
15331533
&PeerBuilder::default()
15341534
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
1535-
.with_bytes_pending_to_download(1)
1535+
.with_bytes_left_to_download(1)
15361536
.build(),
15371537
)
15381538
.await;
@@ -1584,7 +1584,7 @@ mod configured_as_private {
15841584
&info_hash,
15851585
&PeerBuilder::default()
15861586
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
1587-
.with_bytes_pending_to_download(1)
1587+
.with_bytes_left_to_download(1)
15881588
.build(),
15891589
)
15901590
.await;

packages/primitives/src/peer.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -558,21 +558,30 @@ pub mod fixture {
558558
self
559559
}
560560

561-
#[allow(dead_code)]
562561
#[must_use]
563-
pub fn with_bytes_pending_to_download(mut self, left: i64) -> Self {
562+
pub fn with_peer_address(mut self, peer_addr: SocketAddr) -> Self {
563+
self.peer.peer_addr = peer_addr;
564+
self
565+
}
566+
567+
#[must_use]
568+
pub fn updated_on(mut self, updated: DurationSinceUnixEpoch) -> Self {
569+
self.peer.updated = updated;
570+
self
571+
}
572+
573+
#[must_use]
574+
pub fn with_bytes_left_to_download(mut self, left: i64) -> Self {
564575
self.peer.left = NumberOfBytes::new(left);
565576
self
566577
}
567578

568-
#[allow(dead_code)]
569579
#[must_use]
570-
pub fn with_no_bytes_pending_to_download(mut self) -> Self {
580+
pub fn with_no_bytes_left_to_download(mut self) -> Self {
571581
self.peer.left = NumberOfBytes::new(0);
572582
self
573583
}
574584

575-
#[allow(dead_code)]
576585
#[must_use]
577586
pub fn last_updated_on(mut self, updated: DurationSinceUnixEpoch) -> Self {
578587
self.peer.updated = updated;
@@ -585,13 +594,23 @@ pub mod fixture {
585594
self
586595
}
587596

588-
#[allow(dead_code)]
597+
#[must_use]
598+
pub fn with_event_started(mut self) -> Self {
599+
self.peer.event = AnnounceEvent::Started;
600+
self
601+
}
602+
603+
#[must_use]
604+
pub fn with_event_completed(mut self) -> Self {
605+
self.peer.event = AnnounceEvent::Completed;
606+
self
607+
}
608+
589609
#[must_use]
590610
pub fn build(self) -> Peer {
591611
self.into()
592612
}
593613

594-
#[allow(dead_code)]
595614
#[must_use]
596615
pub fn into(self) -> Peer {
597616
self.peer
Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,15 @@
1-
use std::net::SocketAddr;
2-
3-
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
4-
use torrust_tracker_clock::clock::Time;
5-
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};
6-
7-
use crate::CurrentClock;
8-
9-
#[derive(Debug, Default)]
10-
struct TorrentPeerBuilder {
11-
peer: peer::Peer,
12-
}
13-
14-
#[allow(dead_code)]
15-
impl TorrentPeerBuilder {
16-
#[must_use]
17-
fn new() -> Self {
18-
Self {
19-
peer: peer::Peer {
20-
updated: CurrentClock::now(),
21-
..Default::default()
22-
},
23-
}
24-
}
25-
26-
#[must_use]
27-
fn with_event_completed(mut self) -> Self {
28-
self.peer.event = AnnounceEvent::Completed;
29-
self
30-
}
31-
32-
#[must_use]
33-
fn with_event_started(mut self) -> Self {
34-
self.peer.event = AnnounceEvent::Started;
35-
self
36-
}
37-
38-
#[must_use]
39-
fn with_peer_address(mut self, peer_addr: SocketAddr) -> Self {
40-
self.peer.peer_addr = peer_addr;
41-
self
42-
}
43-
44-
#[must_use]
45-
fn with_peer_id(mut self, peer_id: PeerId) -> Self {
46-
self.peer.peer_id = peer_id;
47-
self
48-
}
49-
50-
#[must_use]
51-
fn with_number_of_bytes_left(mut self, left: i64) -> Self {
52-
self.peer.left = NumberOfBytes::new(left);
53-
self
54-
}
55-
56-
#[must_use]
57-
fn updated_at(mut self, updated: DurationSinceUnixEpoch) -> Self {
58-
self.peer.updated = updated;
59-
self
60-
}
61-
62-
#[must_use]
63-
fn into(self) -> peer::Peer {
64-
self.peer
65-
}
66-
}
1+
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
2+
use torrust_tracker_primitives::peer::{self};
673

684
/// A torrent seeder is a peer with 0 bytes left to download which
695
/// has not announced it has stopped
706
#[must_use]
717
pub fn a_completed_peer(id: i32) -> peer::Peer {
728
let peer_id = peer::Id::new(id);
73-
TorrentPeerBuilder::new()
74-
.with_number_of_bytes_left(0)
9+
PeerBuilder::default()
10+
.with_bytes_left_to_download(0)
7511
.with_event_completed()
76-
.with_peer_id(*peer_id)
12+
.with_peer_id(&peer_id)
7713
.into()
7814
}
7915

@@ -82,9 +18,9 @@ pub fn a_completed_peer(id: i32) -> peer::Peer {
8218
#[must_use]
8319
pub fn a_started_peer(id: i32) -> peer::Peer {
8420
let peer_id = peer::Id::new(id);
85-
TorrentPeerBuilder::new()
86-
.with_number_of_bytes_left(1)
21+
PeerBuilder::default()
22+
.with_bytes_left_to_download(1)
8723
.with_event_started()
88-
.with_peer_id(*peer_id)
24+
.with_peer_id(&peer_id)
8925
.into()
9026
}

packages/udp-tracker-server/src/handlers/announce.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ mod tests {
207207
use bittorrent_udp_tracker_core::connection_cookie::{gen_remote_fingerprint, make};
208208
use mockall::predicate::eq;
209209
use torrust_tracker_events::bus::SenderStatus;
210+
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
210211
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
211212

212213
use crate::event::{ConnectionContext, Event, UdpRequestKind};
@@ -216,7 +217,6 @@ mod tests {
216217
initialize_core_tracker_services_for_default_tracker_configuration,
217218
initialize_core_tracker_services_for_public_tracker, sample_cookie_valid_range, sample_ipv4_socket_address,
218219
sample_issue_time, CoreTrackerServices, CoreUdpTrackerServices, MockUdpServerStatsEventSender,
219-
TorrentPeerBuilder,
220220
};
221221

222222
#[tokio::test]
@@ -258,8 +258,8 @@ mod tests {
258258
.get_torrent_peers(&info_hash.0.into())
259259
.await;
260260

261-
let expected_peer = TorrentPeerBuilder::new()
262-
.with_peer_id(peer_id)
261+
let expected_peer = PeerBuilder::default()
262+
.with_peer_id(&peer_id)
263263
.with_peer_address(SocketAddr::new(IpAddr::V4(client_ip), client_port))
264264
.updated_on(peers[0].updated)
265265
.into();
@@ -364,8 +364,8 @@ mod tests {
364364
let client_port = 8080;
365365
let peer_id = AquaticPeerId([255u8; 20]);
366366

367-
let peer_using_ipv6 = TorrentPeerBuilder::new()
368-
.with_peer_id(peer_id)
367+
let peer_using_ipv6 = PeerBuilder::default()
368+
.with_peer_id(&peer_id)
369369
.with_peer_address(SocketAddr::new(IpAddr::V6(client_ip_v6), client_port))
370370
.into();
371371

@@ -466,13 +466,13 @@ mod tests {
466466

467467
use aquatic_udp_protocol::{InfoHash as AquaticInfoHash, PeerId as AquaticPeerId};
468468
use bittorrent_udp_tracker_core::connection_cookie::{gen_remote_fingerprint, make};
469+
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
469470
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
470471

471472
use crate::handlers::announce::tests::announce_request::AnnounceRequestBuilder;
472473
use crate::handlers::handle_announce;
473474
use crate::handlers::tests::{
474475
initialize_core_tracker_services_for_public_tracker, sample_cookie_valid_range, sample_issue_time,
475-
TorrentPeerBuilder,
476476
};
477477

478478
#[tokio::test]
@@ -516,8 +516,8 @@ mod tests {
516516

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

519-
let expected_peer = TorrentPeerBuilder::new()
520-
.with_peer_id(peer_id)
519+
let expected_peer = PeerBuilder::default()
520+
.with_peer_id(&peer_id)
521521
.with_peer_address(SocketAddr::new(external_ip_in_tracker_configuration, client_port))
522522
.updated_on(peers[0].updated)
523523
.into();
@@ -547,6 +547,7 @@ mod tests {
547547
use mockall::predicate::eq;
548548
use torrust_tracker_configuration::Core;
549549
use torrust_tracker_events::bus::SenderStatus;
550+
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
550551
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
551552

552553
use crate::event::{ConnectionContext, Event, UdpRequestKind};
@@ -555,7 +556,7 @@ mod tests {
555556
use crate::handlers::tests::{
556557
initialize_core_tracker_services_for_default_tracker_configuration,
557558
initialize_core_tracker_services_for_public_tracker, sample_cookie_valid_range, sample_ipv6_remote_addr,
558-
sample_issue_time, MockUdpServerStatsEventSender, TorrentPeerBuilder,
559+
sample_issue_time, MockUdpServerStatsEventSender,
559560
};
560561

561562
#[tokio::test]
@@ -598,8 +599,8 @@ mod tests {
598599
.get_torrent_peers(&info_hash.0.into())
599600
.await;
600601

601-
let expected_peer = TorrentPeerBuilder::new()
602-
.with_peer_id(peer_id)
602+
let expected_peer = PeerBuilder::default()
603+
.with_peer_id(&peer_id)
603604
.with_peer_address(SocketAddr::new(IpAddr::V6(client_ip_v6), client_port))
604605
.updated_on(peers[0].updated)
605606
.into();
@@ -707,8 +708,8 @@ mod tests {
707708
let client_port = 8080;
708709
let peer_id = AquaticPeerId([255u8; 20]);
709710

710-
let peer_using_ipv4 = TorrentPeerBuilder::new()
711-
.with_peer_id(peer_id)
711+
let peer_using_ipv4 = PeerBuilder::default()
712+
.with_peer_id(&peer_id)
712713
.with_peer_address(SocketAddr::new(IpAddr::V4(client_ip_v4), client_port))
713714
.into();
714715

0 commit comments

Comments
 (0)