Skip to content

Commit c3f0bc7

Browse files
committed
refactor: [#1211] move tracker tests to InMemoryTorrentRepository
1 parent 17bea24 commit c3f0bc7

File tree

2 files changed

+195
-166
lines changed

2 files changed

+195
-166
lines changed

src/core/mod.rs

-165
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,8 @@ mod tests {
462462
use std::sync::Arc;
463463

464464
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
465-
use bittorrent_primitives::info_hash::fixture::gen_seeded_infohash;
466465
use bittorrent_primitives::info_hash::InfoHash;
467-
use torrust_tracker_configuration::TORRENT_PEERS_LIMIT;
468466
use torrust_tracker_primitives::peer::Peer;
469-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
470467
use torrust_tracker_primitives::DurationSinceUnixEpoch;
471468
use torrust_tracker_test_helpers::configuration;
472469

@@ -503,22 +500,6 @@ mod tests {
503500
(announce_handler, in_memory_torrent_repository, scrape_handler)
504501
}
505502

506-
fn initialize_in_memory_torrents_repository() -> Arc<InMemoryTorrentRepository> {
507-
let config = configuration::ephemeral_public();
508-
509-
let (
510-
_database,
511-
_in_memory_whitelist,
512-
_whitelist_authorization,
513-
_authentication_service,
514-
in_memory_torrent_repository,
515-
_db_torrent_repository,
516-
_torrents_manager,
517-
) = initialize_tracker_dependencies(&config);
518-
519-
in_memory_torrent_repository
520-
}
521-
522503
#[allow(clippy::type_complexity)]
523504
fn whitelisted_tracker() -> (
524505
Arc<AnnounceHandler>,
@@ -659,152 +640,6 @@ mod tests {
659640
}
660641
}
661642

662-
#[tokio::test]
663-
async fn it_should_return_the_peers_for_a_given_torrent() {
664-
let in_memory_torrent_repository = initialize_in_memory_torrents_repository();
665-
666-
let info_hash = sample_info_hash();
667-
let peer = sample_peer();
668-
669-
let () = in_memory_torrent_repository.upsert_peer(&info_hash, &peer);
670-
671-
let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash);
672-
673-
assert_eq!(peers, vec![Arc::new(peer)]);
674-
}
675-
676-
/// It generates a peer id from a number where the number is the last
677-
/// part of the peer ID. For example, for `12` it returns
678-
/// `-qB00000000000000012`.
679-
fn numeric_peer_id(two_digits_value: i32) -> PeerId {
680-
// Format idx as a string with leading zeros, ensuring it has exactly 2 digits
681-
let idx_str = format!("{two_digits_value:02}");
682-
683-
// Create the base part of the peer ID.
684-
let base = b"-qB00000000000000000";
685-
686-
// Concatenate the base with idx bytes, ensuring the total length is 20 bytes.
687-
let mut peer_id_bytes = [0u8; 20];
688-
peer_id_bytes[..base.len()].copy_from_slice(base);
689-
peer_id_bytes[base.len() - idx_str.len()..].copy_from_slice(idx_str.as_bytes());
690-
691-
PeerId(peer_id_bytes)
692-
}
693-
694-
#[tokio::test]
695-
async fn it_should_return_74_peers_at_the_most_for_a_given_torrent() {
696-
let in_memory_torrent_repository = initialize_in_memory_torrents_repository();
697-
698-
let info_hash = sample_info_hash();
699-
700-
for idx in 1..=75 {
701-
let peer = Peer {
702-
peer_id: numeric_peer_id(idx),
703-
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, idx.try_into().unwrap())), 8080),
704-
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
705-
uploaded: NumberOfBytes::new(0),
706-
downloaded: NumberOfBytes::new(0),
707-
left: NumberOfBytes::new(0), // No bytes left to download
708-
event: AnnounceEvent::Completed,
709-
};
710-
711-
let () = in_memory_torrent_repository.upsert_peer(&info_hash, &peer);
712-
}
713-
714-
let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash);
715-
716-
assert_eq!(peers.len(), 74);
717-
}
718-
719-
#[tokio::test]
720-
async fn it_should_return_the_peers_for_a_given_torrent_excluding_a_given_peer() {
721-
let (_announce_handler, in_memory_torrent_repository, _scrape_handler) = public_tracker();
722-
723-
let info_hash = sample_info_hash();
724-
let peer = sample_peer();
725-
726-
let () = in_memory_torrent_repository.upsert_peer(&info_hash, &peer);
727-
728-
let peers = in_memory_torrent_repository.get_peers_for(&info_hash, &peer, TORRENT_PEERS_LIMIT);
729-
730-
assert_eq!(peers, vec![]);
731-
}
732-
733-
#[tokio::test]
734-
async fn it_should_return_74_peers_at_the_most_for_a_given_torrent_when_it_filters_out_a_given_peer() {
735-
let (_announce_handler, in_memory_torrent_repository, _scrape_handler) = public_tracker();
736-
737-
let info_hash = sample_info_hash();
738-
739-
let excluded_peer = sample_peer();
740-
741-
let () = in_memory_torrent_repository.upsert_peer(&info_hash, &excluded_peer);
742-
743-
// Add 74 peers
744-
for idx in 2..=75 {
745-
let peer = Peer {
746-
peer_id: numeric_peer_id(idx),
747-
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, idx.try_into().unwrap())), 8080),
748-
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
749-
uploaded: NumberOfBytes::new(0),
750-
downloaded: NumberOfBytes::new(0),
751-
left: NumberOfBytes::new(0), // No bytes left to download
752-
event: AnnounceEvent::Completed,
753-
};
754-
755-
let () = in_memory_torrent_repository.upsert_peer(&info_hash, &peer);
756-
}
757-
758-
let peers = in_memory_torrent_repository.get_peers_for(&info_hash, &excluded_peer, TORRENT_PEERS_LIMIT);
759-
760-
assert_eq!(peers.len(), 74);
761-
}
762-
763-
#[tokio::test]
764-
async fn it_should_return_the_torrent_metrics() {
765-
let in_memory_torrent_repository = initialize_in_memory_torrents_repository();
766-
767-
let () = in_memory_torrent_repository.upsert_peer(&sample_info_hash(), &leecher());
768-
769-
let torrent_metrics = in_memory_torrent_repository.get_torrents_metrics();
770-
771-
assert_eq!(
772-
torrent_metrics,
773-
TorrentsMetrics {
774-
complete: 0,
775-
downloaded: 0,
776-
incomplete: 1,
777-
torrents: 1,
778-
}
779-
);
780-
}
781-
782-
#[tokio::test]
783-
async fn it_should_get_many_the_torrent_metrics() {
784-
let in_memory_torrent_repository = initialize_in_memory_torrents_repository();
785-
786-
let start_time = std::time::Instant::now();
787-
for i in 0..1_000_000 {
788-
let () = in_memory_torrent_repository.upsert_peer(&gen_seeded_infohash(&i), &leecher());
789-
}
790-
let result_a = start_time.elapsed();
791-
792-
let start_time = std::time::Instant::now();
793-
let torrent_metrics = in_memory_torrent_repository.get_torrents_metrics();
794-
let result_b = start_time.elapsed();
795-
796-
assert_eq!(
797-
(torrent_metrics),
798-
(TorrentsMetrics {
799-
complete: 0,
800-
downloaded: 0,
801-
incomplete: 1_000_000,
802-
torrents: 1_000_000,
803-
}),
804-
"{result_a:?} {result_b:?}"
805-
);
806-
}
807-
808643
mod for_all_config_modes {
809644

810645
mod handling_an_announce_request {

0 commit comments

Comments
 (0)