Skip to content

Commit b51018f

Browse files
committed
refactor: [torrust#1211] extract duplicate code
1 parent 6529021 commit b51018f

File tree

4 files changed

+73
-132
lines changed

4 files changed

+73
-132
lines changed

src/core/announce_handler.rs

+6-57
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,6 @@ mod tests {
190190
IpAddr::V4(Ipv4Addr::from_str("126.0.0.1").unwrap())
191191
}
192192

193-
/// Sample peer whose state is not relevant for the tests
194-
fn sample_peer() -> Peer {
195-
complete_peer()
196-
}
197-
198193
/// Sample peer when for tests that need more than one peer
199194
fn sample_peer_1() -> Peer {
200195
Peer {
@@ -221,61 +216,17 @@ mod tests {
221216
}
222217
}
223218

224-
fn seeder() -> Peer {
225-
complete_peer()
226-
}
227-
228-
fn leecher() -> Peer {
229-
incomplete_peer()
230-
}
231-
232-
fn started_peer() -> Peer {
233-
incomplete_peer()
234-
}
235-
236-
fn completed_peer() -> Peer {
237-
complete_peer()
238-
}
239-
240-
/// A peer that counts as `complete` is swarm metadata
241-
/// IMPORTANT!: it only counts if the it has been announce at least once before
242-
/// announcing the `AnnounceEvent::Completed` event.
243-
fn complete_peer() -> Peer {
244-
Peer {
245-
peer_id: PeerId(*b"-qB00000000000000000"),
246-
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
247-
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
248-
uploaded: NumberOfBytes::new(0),
249-
downloaded: NumberOfBytes::new(0),
250-
left: NumberOfBytes::new(0), // No bytes left to download
251-
event: AnnounceEvent::Completed,
252-
}
253-
}
254-
255-
/// A peer that counts as `incomplete` is swarm metadata
256-
fn incomplete_peer() -> Peer {
257-
Peer {
258-
peer_id: PeerId(*b"-qB00000000000000000"),
259-
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
260-
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
261-
uploaded: NumberOfBytes::new(0),
262-
downloaded: NumberOfBytes::new(0),
263-
left: NumberOfBytes::new(1000), // Still bytes to download
264-
event: AnnounceEvent::Started,
265-
}
266-
}
267-
268219
mod for_all_tracker_config_modes {
269220

270221
mod handling_an_announce_request {
271222

272223
use std::sync::Arc;
273224

274225
use crate::core::announce_handler::tests::the_announce_handler::{
275-
peer_ip, public_tracker, sample_peer, sample_peer_1, sample_peer_2,
226+
peer_ip, public_tracker, sample_peer_1, sample_peer_2,
276227
};
277228
use crate::core::announce_handler::PeersWanted;
278-
use crate::core::core_tests::sample_info_hash;
229+
use crate::core::core_tests::{sample_info_hash, sample_peer};
279230

280231
mod should_assign_the_ip_to_the_peer {
281232

@@ -406,11 +357,9 @@ mod tests {
406357

407358
mod it_should_update_the_swarm_stats_for_the_torrent {
408359

409-
use crate::core::announce_handler::tests::the_announce_handler::{
410-
completed_peer, leecher, peer_ip, public_tracker, seeder, started_peer,
411-
};
360+
use crate::core::announce_handler::tests::the_announce_handler::{peer_ip, public_tracker};
412361
use crate::core::announce_handler::PeersWanted;
413-
use crate::core::core_tests::sample_info_hash;
362+
use crate::core::core_tests::{completed_peer, leecher, sample_info_hash, seeder, started_peer};
414363

415364
#[tokio::test]
416365
async fn when_the_peer_is_a_seeder() {
@@ -462,9 +411,9 @@ mod tests {
462411
use torrust_tracker_test_helpers::configuration;
463412
use torrust_tracker_torrent_repository::entry::EntrySync;
464413

465-
use crate::core::announce_handler::tests::the_announce_handler::{peer_ip, sample_peer};
414+
use crate::core::announce_handler::tests::the_announce_handler::peer_ip;
466415
use crate::core::announce_handler::{AnnounceHandler, PeersWanted};
467-
use crate::core::core_tests::sample_info_hash;
416+
use crate::core::core_tests::{sample_info_hash, sample_peer};
468417
use crate::core::services::initialize_database;
469418
use crate::core::torrent::manager::TorrentsManager;
470419
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;

src/core/core_tests.rs

+61
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
//! Some generic test helpers functions.
2+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
13
use std::sync::Arc;
24

5+
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
36
use bittorrent_primitives::info_hash::InfoHash;
47
use torrust_tracker_configuration::Configuration;
8+
use torrust_tracker_primitives::peer::Peer;
9+
use torrust_tracker_primitives::DurationSinceUnixEpoch;
510

611
use super::announce_handler::AnnounceHandler;
712
use super::scrape_handler::ScrapeHandler;
@@ -21,6 +26,62 @@ pub fn sample_info_hash() -> InfoHash {
2126
.expect("String should be a valid info hash")
2227
}
2328

29+
/// Sample peer whose state is not relevant for the tests.
30+
#[must_use]
31+
pub fn sample_peer() -> Peer {
32+
complete_peer()
33+
}
34+
35+
#[must_use]
36+
pub fn seeder() -> Peer {
37+
complete_peer()
38+
}
39+
40+
#[must_use]
41+
pub fn leecher() -> Peer {
42+
incomplete_peer()
43+
}
44+
45+
#[must_use]
46+
pub fn started_peer() -> Peer {
47+
incomplete_peer()
48+
}
49+
50+
#[must_use]
51+
pub fn completed_peer() -> Peer {
52+
complete_peer()
53+
}
54+
55+
/// A peer that counts as `complete` is swarm metadata
56+
/// IMPORTANT!: it only counts if the it has been announce at least once before
57+
/// announcing the `AnnounceEvent::Completed` event.
58+
#[must_use]
59+
pub fn complete_peer() -> Peer {
60+
Peer {
61+
peer_id: PeerId(*b"-qB00000000000000000"),
62+
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
63+
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
64+
uploaded: NumberOfBytes::new(0),
65+
downloaded: NumberOfBytes::new(0),
66+
left: NumberOfBytes::new(0), // No bytes left to download
67+
event: AnnounceEvent::Completed,
68+
}
69+
}
70+
71+
/// A peer that counts as `incomplete` is swarm metadata
72+
#[must_use]
73+
pub fn incomplete_peer() -> Peer {
74+
Peer {
75+
peer_id: PeerId(*b"-qB00000000000000000"),
76+
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
77+
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
78+
uploaded: NumberOfBytes::new(0),
79+
downloaded: NumberOfBytes::new(0),
80+
left: NumberOfBytes::new(1000), // Still bytes to download
81+
event: AnnounceEvent::Started,
82+
}
83+
}
84+
2485
#[must_use]
2586
pub fn initialize_handlers(config: &Configuration) -> (Arc<AnnounceHandler>, Arc<ScrapeHandler>) {
2687
let database = initialize_database(config);

src/core/mod.rs

+5-37
Original file line numberDiff line numberDiff line change
@@ -455,14 +455,10 @@ pub mod peer_tests;
455455
#[cfg(test)]
456456
mod tests {
457457
mod the_tracker {
458-
459-
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
458+
use std::net::{IpAddr, Ipv4Addr};
460459
use std::str::FromStr;
461460
use std::sync::Arc;
462461

463-
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
464-
use torrust_tracker_primitives::peer::Peer;
465-
use torrust_tracker_primitives::DurationSinceUnixEpoch;
466462
use torrust_tracker_test_helpers::configuration;
467463

468464
use crate::core::announce_handler::AnnounceHandler;
@@ -484,34 +480,6 @@ mod tests {
484480
IpAddr::V4(Ipv4Addr::from_str("126.0.0.1").unwrap())
485481
}
486482

487-
/// A peer that counts as `complete` is swarm metadata
488-
/// IMPORTANT!: it only counts if the it has been announce at least once before
489-
/// announcing the `AnnounceEvent::Completed` event.
490-
fn complete_peer() -> Peer {
491-
Peer {
492-
peer_id: PeerId(*b"-qB00000000000000000"),
493-
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
494-
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
495-
uploaded: NumberOfBytes::new(0),
496-
downloaded: NumberOfBytes::new(0),
497-
left: NumberOfBytes::new(0), // No bytes left to download
498-
event: AnnounceEvent::Completed,
499-
}
500-
}
501-
502-
/// A peer that counts as `incomplete` is swarm metadata
503-
fn incomplete_peer() -> Peer {
504-
Peer {
505-
peer_id: PeerId(*b"-qB00000000000000000"),
506-
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
507-
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
508-
uploaded: NumberOfBytes::new(0),
509-
downloaded: NumberOfBytes::new(0),
510-
left: NumberOfBytes::new(1000), // Still bytes to download
511-
event: AnnounceEvent::Started,
512-
}
513-
}
514-
515483
mod for_all_config_modes {
516484

517485
mod handling_a_scrape_request {
@@ -523,7 +491,8 @@ mod tests {
523491
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
524492

525493
use crate::core::announce_handler::PeersWanted;
526-
use crate::core::tests::the_tracker::{complete_peer, incomplete_peer, initialize_handlers_for_public_tracker};
494+
use crate::core::core_tests::{complete_peer, incomplete_peer};
495+
use crate::core::tests::the_tracker::initialize_handlers_for_public_tracker;
527496

528497
#[tokio::test]
529498
async fn it_should_return_the_swarm_metadata_for_the_requested_file_if_the_tracker_has_that_torrent() {
@@ -577,9 +546,8 @@ mod tests {
577546
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
578547

579548
use crate::core::announce_handler::PeersWanted;
580-
use crate::core::tests::the_tracker::{
581-
complete_peer, incomplete_peer, initialize_handlers_for_listed_tracker, peer_ip,
582-
};
549+
use crate::core::core_tests::{complete_peer, incomplete_peer};
550+
use crate::core::tests::the_tracker::{initialize_handlers_for_listed_tracker, peer_ip};
583551

584552
#[tokio::test]
585553
async fn it_should_return_the_zeroed_swarm_metadata_for_the_requested_file_if_it_is_not_whitelisted() {

src/core/torrent/repository/in_memory.rs

+1-38
Original file line numberDiff line numberDiff line change
@@ -114,46 +114,9 @@ mod tests {
114114
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
115115
use torrust_tracker_primitives::DurationSinceUnixEpoch;
116116

117-
use crate::core::core_tests::sample_info_hash;
117+
use crate::core::core_tests::{leecher, sample_info_hash, sample_peer};
118118
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
119119

120-
/// Sample peer whose state is not relevant for the tests
121-
fn sample_peer() -> Peer {
122-
complete_peer()
123-
}
124-
125-
fn leecher() -> Peer {
126-
incomplete_peer()
127-
}
128-
129-
/// A peer that counts as `complete` is swarm metadata
130-
/// IMPORTANT!: it only counts if the it has been announce at least once before
131-
/// announcing the `AnnounceEvent::Completed` event.
132-
fn complete_peer() -> Peer {
133-
Peer {
134-
peer_id: PeerId(*b"-qB00000000000000000"),
135-
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
136-
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
137-
uploaded: NumberOfBytes::new(0),
138-
downloaded: NumberOfBytes::new(0),
139-
left: NumberOfBytes::new(0), // No bytes left to download
140-
event: AnnounceEvent::Completed,
141-
}
142-
}
143-
144-
/// A peer that counts as `incomplete` is swarm metadata
145-
fn incomplete_peer() -> Peer {
146-
Peer {
147-
peer_id: PeerId(*b"-qB00000000000000000"),
148-
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
149-
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
150-
uploaded: NumberOfBytes::new(0),
151-
downloaded: NumberOfBytes::new(0),
152-
left: NumberOfBytes::new(1000), // Still bytes to download
153-
event: AnnounceEvent::Started,
154-
}
155-
}
156-
157120
/// It generates a peer id from a number where the number is the last
158121
/// part of the peer ID. For example, for `12` it returns
159122
/// `-qB00000000000000012`.

0 commit comments

Comments
 (0)