Skip to content

Commit

Permalink
refactor: [#1207] use InMemoryTorrentRepository in tests to add torrents
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 27, 2025
1 parent f23a3fc commit f741d06
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 60 deletions.
42 changes: 21 additions & 21 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ mod tests {
use crate::core::whitelist::manager::WhiteListManager;
use crate::core::{whitelist, Tracker};

fn public_tracker() -> (Arc<Tracker>, Arc<ScrapeHandler>) {
fn public_tracker() -> (Arc<Tracker>, Arc<InMemoryTorrentRepository>, Arc<ScrapeHandler>) {
let config = configuration::ephemeral_public();

let (
Expand All @@ -714,7 +714,7 @@ mod tests {

let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository));

(tracker, scrape_handler)
(tracker, in_memory_torrent_repository, scrape_handler)
}

fn public_tracker_and_in_memory_torrents_repository() -> (Arc<Tracker>, Arc<InMemoryTorrentRepository>) {
Expand Down Expand Up @@ -871,12 +871,12 @@ mod tests {

#[tokio::test]
async fn it_should_return_the_peers_for_a_given_torrent() {
let (tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();
let (_tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();

let info_hash = sample_info_hash();
let peer = sample_peer();

let _ = tracker.upsert_peer_and_get_stats(&info_hash, &peer);
let () = in_memory_torrent_repository.upsert_peer(&info_hash, &peer);

let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash);

Expand All @@ -903,7 +903,7 @@ mod tests {

#[tokio::test]
async fn it_should_return_74_peers_at_the_most_for_a_given_torrent() {
let (tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();
let (_tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();

let info_hash = sample_info_hash();

Expand All @@ -918,7 +918,7 @@ mod tests {
event: AnnounceEvent::Completed,
};

let _ = tracker.upsert_peer_and_get_stats(&info_hash, &peer);
let () = in_memory_torrent_repository.upsert_peer(&info_hash, &peer);
}

let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash);
Expand All @@ -928,12 +928,12 @@ mod tests {

#[tokio::test]
async fn it_should_return_the_peers_for_a_given_torrent_excluding_a_given_peer() {
let (tracker, _scrape_handler) = public_tracker();
let (tracker, in_memory_torrent_repository, _scrape_handler) = public_tracker();

let info_hash = sample_info_hash();
let peer = sample_peer();

let _ = tracker.upsert_peer_and_get_stats(&info_hash, &peer);
let () = in_memory_torrent_repository.upsert_peer(&info_hash, &peer);

let peers = tracker
.in_memory_torrent_repository
Expand All @@ -944,13 +944,13 @@ mod tests {

#[tokio::test]
async fn it_should_return_74_peers_at_the_most_for_a_given_torrent_when_it_filters_out_a_given_peer() {
let (tracker, _scrape_handler) = public_tracker();
let (tracker, in_memory_torrent_repository, _scrape_handler) = public_tracker();

let info_hash = sample_info_hash();

let excluded_peer = sample_peer();

let _ = tracker.upsert_peer_and_get_stats(&info_hash, &excluded_peer);
let () = in_memory_torrent_repository.upsert_peer(&info_hash, &excluded_peer);

// Add 74 peers
for idx in 2..=75 {
Expand All @@ -964,7 +964,7 @@ mod tests {
event: AnnounceEvent::Completed,
};

let _ = tracker.upsert_peer_and_get_stats(&info_hash, &peer);
let () = in_memory_torrent_repository.upsert_peer(&info_hash, &peer);
}

let peers = tracker
Expand All @@ -976,9 +976,9 @@ mod tests {

#[tokio::test]
async fn it_should_return_the_torrent_metrics() {
let (tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();
let (_tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();

let _ = tracker.upsert_peer_and_get_stats(&sample_info_hash(), &leecher());
let () = in_memory_torrent_repository.upsert_peer(&sample_info_hash(), &leecher());

let torrent_metrics = in_memory_torrent_repository.get_torrents_metrics();

Expand All @@ -995,11 +995,11 @@ mod tests {

#[tokio::test]
async fn it_should_get_many_the_torrent_metrics() {
let (tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();
let (_tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();

let start_time = std::time::Instant::now();
for i in 0..1_000_000 {
let _ = tracker.upsert_peer_and_get_stats(&gen_seeded_infohash(&i), &leecher());
let () = in_memory_torrent_repository.upsert_peer(&gen_seeded_infohash(&i), &leecher());
}
let result_a = start_time.elapsed();

Expand Down Expand Up @@ -1130,7 +1130,7 @@ mod tests {

#[tokio::test]
async fn it_should_return_the_announce_data_with_an_empty_peer_list_when_it_is_the_first_announced_peer() {
let (tracker, _scrape_handler) = public_tracker();
let (tracker, _in_memory_torrent_repository, _scrape_handler) = public_tracker();

let mut peer = sample_peer();

Expand All @@ -1141,7 +1141,7 @@ mod tests {

#[tokio::test]
async fn it_should_return_the_announce_data_with_the_previously_announced_peers() {
let (tracker, _scrape_handler) = public_tracker();
let (tracker, _in_memory_torrent_repository, _scrape_handler) = public_tracker();

let mut previously_announced_peer = sample_peer_1();
tracker.announce(
Expand All @@ -1166,7 +1166,7 @@ mod tests {

#[tokio::test]
async fn when_the_peer_is_a_seeder() {
let (tracker, _scrape_handler) = public_tracker();
let (tracker, _in_memory_torrent_repository, _scrape_handler) = public_tracker();

let mut peer = seeder();

Expand All @@ -1177,7 +1177,7 @@ mod tests {

#[tokio::test]
async fn when_the_peer_is_a_leecher() {
let (tracker, _scrape_handler) = public_tracker();
let (tracker, _in_memory_torrent_repository, _scrape_handler) = public_tracker();

let mut peer = leecher();

Expand All @@ -1188,7 +1188,7 @@ mod tests {

#[tokio::test]
async fn when_a_previously_announced_started_peer_has_completed_downloading() {
let (tracker, _scrape_handler) = public_tracker();
let (tracker, _in_memory_torrent_repository, _scrape_handler) = public_tracker();

// We have to announce with "started" event because peer does not count if peer was not previously known
let mut started_peer = started_peer();
Expand All @@ -1215,7 +1215,7 @@ mod tests {

#[tokio::test]
async fn it_should_return_the_swarm_metadata_for_the_requested_file_if_the_tracker_has_that_torrent() {
let (tracker, scrape_handler) = public_tracker();
let (tracker, _in_memory_torrent_repository, scrape_handler) = public_tracker();

let info_hash = "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::<InfoHash>().unwrap(); // # DevSkim: ignore DS173237

Expand Down
28 changes: 15 additions & 13 deletions src/core/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ mod tests {
async fn should_return_the_torrent_info_if_the_tracker_has_the_torrent() {
let config = tracker_configuration();

let (tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);
let (_tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();
let _ = tracker.upsert_peer_and_get_stats(&info_hash, &sample_peer());
let () = in_memory_torrent_repository.upsert_peer(&info_hash, &sample_peer());

let torrent_info = get_torrent_info(in_memory_torrent_repository.clone(), &info_hash)
.await
Expand Down Expand Up @@ -242,12 +242,12 @@ mod tests {
async fn should_return_a_summarized_info_for_all_torrents() {
let config = tracker_configuration();

let (tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);
let (_tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();

let _ = tracker.upsert_peer_and_get_stats(&info_hash, &sample_peer());
let () = in_memory_torrent_repository.upsert_peer(&info_hash, &sample_peer());

let torrents = get_torrents_page(in_memory_torrent_repository.clone(), Some(&Pagination::default())).await;

Expand All @@ -266,15 +266,16 @@ mod tests {
async fn should_allow_limiting_the_number_of_torrents_in_the_result() {
let config = tracker_configuration();

let (tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);
let (_tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();

let hash2 = "03840548643af2a7b63a9f5cbca348bc7150ca3a".to_owned();
let info_hash2 = InfoHash::from_str(&hash2).unwrap();

let _ = tracker.upsert_peer_and_get_stats(&info_hash1, &sample_peer());
let _ = tracker.upsert_peer_and_get_stats(&info_hash2, &sample_peer());
let () = in_memory_torrent_repository.upsert_peer(&info_hash1, &sample_peer());
let () = in_memory_torrent_repository.upsert_peer(&info_hash2, &sample_peer());

let offset = 0;
let limit = 1;
Expand All @@ -288,15 +289,16 @@ mod tests {
async fn should_allow_using_pagination_in_the_result() {
let config = tracker_configuration();

let (tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);
let (_tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();

let hash2 = "03840548643af2a7b63a9f5cbca348bc7150ca3a".to_owned();
let info_hash2 = InfoHash::from_str(&hash2).unwrap();

let _ = tracker.upsert_peer_and_get_stats(&info_hash1, &sample_peer());
let _ = tracker.upsert_peer_and_get_stats(&info_hash2, &sample_peer());
let () = in_memory_torrent_repository.upsert_peer(&info_hash1, &sample_peer());
let () = in_memory_torrent_repository.upsert_peer(&info_hash2, &sample_peer());

let offset = 1;
let limit = 4000;
Expand All @@ -319,15 +321,15 @@ mod tests {
async fn should_return_torrents_ordered_by_info_hash() {
let config = tracker_configuration();

let (tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);
let (_tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
let _ = tracker.upsert_peer_and_get_stats(&info_hash1, &sample_peer());
let () = in_memory_torrent_repository.upsert_peer(&info_hash1, &sample_peer());

let hash2 = "03840548643af2a7b63a9f5cbca348bc7150ca3a".to_owned();
let info_hash2 = InfoHash::from_str(&hash2).unwrap();
let _ = tracker.upsert_peer_and_get_stats(&info_hash2, &sample_peer());
let () = in_memory_torrent_repository.upsert_peer(&info_hash2, &sample_peer());

let torrents = get_torrents_page(in_memory_torrent_repository.clone(), Some(&Pagination::default())).await;

Expand Down
Loading

0 comments on commit f741d06

Please sign in to comment.