Skip to content

Commit 0f1b2fb

Browse files
committed
refactor: [#1203] use InMemoryTorrentRepository directly in core tracker tests
1 parent 2ac68f6 commit 0f1b2fb

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

src/core/mod.rs

+36-12
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ mod tests {
717717
use crate::core::peer::Peer;
718718
use crate::core::services::{initialize_tracker, initialize_whitelist_manager};
719719
use crate::core::torrent::manager::TorrentsManager;
720+
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
720721
use crate::core::whitelist::manager::WhiteListManager;
721722
use crate::core::{whitelist, Tracker};
722723

@@ -741,6 +742,29 @@ mod tests {
741742
)
742743
}
743744

745+
fn public_tracker_and_in_memory_torrents_repository() -> (Arc<Tracker>, Arc<InMemoryTorrentRepository>) {
746+
let config = configuration::ephemeral_public();
747+
748+
let (
749+
_database,
750+
_in_memory_whitelist,
751+
whitelist_authorization,
752+
_authentication_service,
753+
in_memory_torrent_repository,
754+
db_torrent_repository,
755+
_torrents_manager,
756+
) = initialize_tracker_dependencies(&config);
757+
758+
let tracker = Arc::new(initialize_tracker(
759+
&config,
760+
&whitelist_authorization,
761+
&in_memory_torrent_repository,
762+
&db_torrent_repository,
763+
));
764+
765+
(tracker, in_memory_torrent_repository)
766+
}
767+
744768
fn whitelisted_tracker() -> (Tracker, Arc<whitelist::authorization::Authorization>, Arc<WhiteListManager>) {
745769
let config = configuration::ephemeral_listed();
746770

@@ -766,7 +790,7 @@ mod tests {
766790
(tracker, whitelist_authorization, whitelist_manager)
767791
}
768792

769-
pub fn tracker_persisting_torrents_in_database() -> (Tracker, Arc<TorrentsManager>) {
793+
pub fn tracker_persisting_torrents_in_database() -> (Tracker, Arc<TorrentsManager>, Arc<InMemoryTorrentRepository>) {
770794
let mut config = configuration::ephemeral_listed();
771795
config.core.tracker_policy.persistent_torrent_completed_stat = true;
772796

@@ -787,7 +811,7 @@ mod tests {
787811
&db_torrent_repository,
788812
);
789813

790-
(tracker, torrents_manager)
814+
(tracker, torrents_manager, in_memory_torrent_repository)
791815
}
792816

793817
fn sample_info_hash() -> InfoHash {
@@ -876,14 +900,14 @@ mod tests {
876900

877901
#[tokio::test]
878902
async fn it_should_return_the_peers_for_a_given_torrent() {
879-
let tracker = public_tracker();
903+
let (tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();
880904

881905
let info_hash = sample_info_hash();
882906
let peer = sample_peer();
883907

884908
let _ = tracker.upsert_peer_and_get_stats(&info_hash, &peer);
885909

886-
let peers = tracker.in_memory_torrent_repository.get_torrent_peers(&info_hash);
910+
let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash);
887911

888912
assert_eq!(peers, vec![Arc::new(peer)]);
889913
}
@@ -908,7 +932,7 @@ mod tests {
908932

909933
#[tokio::test]
910934
async fn it_should_return_74_peers_at_the_most_for_a_given_torrent() {
911-
let tracker = public_tracker();
935+
let (tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();
912936

913937
let info_hash = sample_info_hash();
914938

@@ -926,7 +950,7 @@ mod tests {
926950
let _ = tracker.upsert_peer_and_get_stats(&info_hash, &peer);
927951
}
928952

929-
let peers = tracker.in_memory_torrent_repository.get_torrent_peers(&info_hash);
953+
let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash);
930954

931955
assert_eq!(peers.len(), 74);
932956
}
@@ -981,11 +1005,11 @@ mod tests {
9811005

9821006
#[tokio::test]
9831007
async fn it_should_return_the_torrent_metrics() {
984-
let tracker = public_tracker();
1008+
let (tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();
9851009

9861010
let _ = tracker.upsert_peer_and_get_stats(&sample_info_hash(), &leecher());
9871011

988-
let torrent_metrics = tracker.in_memory_torrent_repository.get_torrents_metrics();
1012+
let torrent_metrics = in_memory_torrent_repository.get_torrents_metrics();
9891013

9901014
assert_eq!(
9911015
torrent_metrics,
@@ -1000,7 +1024,7 @@ mod tests {
10001024

10011025
#[tokio::test]
10021026
async fn it_should_get_many_the_torrent_metrics() {
1003-
let tracker = public_tracker();
1027+
let (tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();
10041028

10051029
let start_time = std::time::Instant::now();
10061030
for i in 0..1_000_000 {
@@ -1009,7 +1033,7 @@ mod tests {
10091033
let result_a = start_time.elapsed();
10101034

10111035
let start_time = std::time::Instant::now();
1012-
let torrent_metrics = tracker.in_memory_torrent_repository.get_torrents_metrics();
1036+
let torrent_metrics = in_memory_torrent_repository.get_torrents_metrics();
10131037
let result_b = start_time.elapsed();
10141038

10151039
assert_eq!(
@@ -1434,7 +1458,7 @@ mod tests {
14341458

14351459
#[tokio::test]
14361460
async fn it_should_persist_the_number_of_completed_peers_for_all_torrents_into_the_database() {
1437-
let (tracker, torrents_manager) = tracker_persisting_torrents_in_database();
1461+
let (tracker, torrents_manager, in_memory_torrent_repository) = tracker_persisting_torrents_in_database();
14381462

14391463
let info_hash = sample_info_hash();
14401464

@@ -1449,7 +1473,7 @@ mod tests {
14491473
assert_eq!(swarm_stats.downloaded, 1);
14501474

14511475
// Remove the newly updated torrent from memory
1452-
let _unused = tracker.in_memory_torrent_repository.remove(&info_hash);
1476+
let _unused = in_memory_torrent_repository.remove(&info_hash);
14531477

14541478
torrents_manager.load_torrents_from_database().unwrap();
14551479

0 commit comments

Comments
 (0)