@@ -459,7 +459,6 @@ use torrust_tracker_configuration::{AnnouncePolicy, Core, TORRENT_PEERS_LIMIT};
459
459
use torrust_tracker_primitives:: core:: { AnnounceData , ScrapeData } ;
460
460
use torrust_tracker_primitives:: peer;
461
461
use torrust_tracker_primitives:: swarm_metadata:: SwarmMetadata ;
462
- use torrust_tracker_primitives:: torrent_metrics:: TorrentsMetrics ;
463
462
464
463
/// The domain layer tracker service.
465
464
///
@@ -475,7 +474,7 @@ pub struct Tracker {
475
474
config : Core ,
476
475
477
476
/// The service to check is a torrent is whitelisted.
478
- pub whitelist_authorization : Arc < whitelist:: authorization:: Authorization > ,
477
+ whitelist_authorization : Arc < whitelist:: authorization:: Authorization > ,
479
478
480
479
/// The in-memory torrents repository.
481
480
in_memory_torrent_repository : Arc < InMemoryTorrentRepository > ,
@@ -619,7 +618,9 @@ impl Tracker {
619
618
620
619
let stats = self . upsert_peer_and_get_stats ( info_hash, peer) ;
621
620
622
- let peers = self . get_peers_for ( info_hash, peer, peers_wanted. limit ( ) ) ;
621
+ let peers = self
622
+ . in_memory_torrent_repository
623
+ . get_peers_for ( info_hash, peer, peers_wanted. limit ( ) ) ;
623
624
624
625
AnnounceData {
625
626
peers,
@@ -638,7 +639,7 @@ impl Tracker {
638
639
639
640
for info_hash in info_hashes {
640
641
let swarm_metadata = match self . whitelist_authorization . authorize ( info_hash) . await {
641
- Ok ( ( ) ) => self . get_swarm_metadata ( info_hash) ,
642
+ Ok ( ( ) ) => self . in_memory_torrent_repository . get_swarm_metadata ( info_hash) ,
642
643
Err ( _) => SwarmMetadata :: zeroed ( ) ,
643
644
} ;
644
645
scrape_data. add_file ( info_hash, swarm_metadata) ;
@@ -684,40 +685,6 @@ impl Tracker {
684
685
drop ( self . db_torrent_repository . save ( & info_hash, completed) ) ;
685
686
}
686
687
}
687
-
688
- /// It returns the data for a `scrape` response.
689
- fn get_swarm_metadata ( & self , info_hash : & InfoHash ) -> SwarmMetadata {
690
- self . in_memory_torrent_repository . get_swarm_metadata ( info_hash)
691
- }
692
-
693
- /// # Context: Tracker
694
- ///
695
- /// Get torrent peers for a given torrent and client.
696
- ///
697
- /// It filters out the client making the request.
698
- fn get_peers_for ( & self , info_hash : & InfoHash , peer : & peer:: Peer , limit : usize ) -> Vec < Arc < peer:: Peer > > {
699
- self . in_memory_torrent_repository . get_peers_for ( info_hash, peer, limit)
700
- }
701
-
702
- /// # Context: Tracker
703
- ///
704
- /// Get torrent peers for a given torrent.
705
- #[ must_use]
706
- pub fn get_torrent_peers ( & self , info_hash : & InfoHash ) -> Vec < Arc < peer:: Peer > > {
707
- self . in_memory_torrent_repository . get_torrent_peers ( info_hash)
708
- }
709
-
710
- /// It calculates and returns the general `Tracker`
711
- /// [`TorrentsMetrics`]
712
- ///
713
- /// # Context: Tracker
714
- ///
715
- /// # Panics
716
- /// Panics if unable to get the torrent metrics.
717
- #[ must_use]
718
- pub fn get_torrents_metrics ( & self ) -> TorrentsMetrics {
719
- self . in_memory_torrent_repository . get_torrents_metrics ( )
720
- }
721
688
}
722
689
723
690
#[ must_use]
@@ -742,15 +709,17 @@ mod tests {
742
709
use bittorrent_primitives:: info_hash:: fixture:: gen_seeded_infohash;
743
710
use bittorrent_primitives:: info_hash:: InfoHash ;
744
711
use torrust_tracker_configuration:: TORRENT_PEERS_LIMIT ;
712
+ use torrust_tracker_primitives:: torrent_metrics:: TorrentsMetrics ;
745
713
use torrust_tracker_primitives:: DurationSinceUnixEpoch ;
746
714
use torrust_tracker_test_helpers:: configuration;
747
715
748
716
use crate :: app_test:: initialize_tracker_dependencies;
749
717
use crate :: core:: peer:: Peer ;
750
718
use crate :: core:: services:: { initialize_tracker, initialize_whitelist_manager} ;
751
719
use crate :: core:: torrent:: manager:: TorrentsManager ;
720
+ use crate :: core:: torrent:: repository:: in_memory:: InMemoryTorrentRepository ;
752
721
use crate :: core:: whitelist:: manager:: WhiteListManager ;
753
- use crate :: core:: { whitelist, TorrentsMetrics , Tracker } ;
722
+ use crate :: core:: { whitelist, Tracker } ;
754
723
755
724
fn public_tracker ( ) -> Tracker {
756
725
let config = configuration:: ephemeral_public ( ) ;
@@ -773,6 +742,29 @@ mod tests {
773
742
)
774
743
}
775
744
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
+
776
768
fn whitelisted_tracker ( ) -> ( Tracker , Arc < whitelist:: authorization:: Authorization > , Arc < WhiteListManager > ) {
777
769
let config = configuration:: ephemeral_listed ( ) ;
778
770
@@ -798,7 +790,7 @@ mod tests {
798
790
( tracker, whitelist_authorization, whitelist_manager)
799
791
}
800
792
801
- pub fn tracker_persisting_torrents_in_database ( ) -> ( Tracker , Arc < TorrentsManager > ) {
793
+ pub fn tracker_persisting_torrents_in_database ( ) -> ( Tracker , Arc < TorrentsManager > , Arc < InMemoryTorrentRepository > ) {
802
794
let mut config = configuration:: ephemeral_listed ( ) ;
803
795
config. core . tracker_policy . persistent_torrent_completed_stat = true ;
804
796
@@ -819,7 +811,7 @@ mod tests {
819
811
& db_torrent_repository,
820
812
) ;
821
813
822
- ( tracker, torrents_manager)
814
+ ( tracker, torrents_manager, in_memory_torrent_repository )
823
815
}
824
816
825
817
fn sample_info_hash ( ) -> InfoHash {
@@ -906,33 +898,16 @@ mod tests {
906
898
}
907
899
}
908
900
909
- #[ tokio:: test]
910
- async fn should_collect_torrent_metrics ( ) {
911
- let tracker = public_tracker ( ) ;
912
-
913
- let torrents_metrics = tracker. get_torrents_metrics ( ) ;
914
-
915
- assert_eq ! (
916
- torrents_metrics,
917
- TorrentsMetrics {
918
- complete: 0 ,
919
- downloaded: 0 ,
920
- incomplete: 0 ,
921
- torrents: 0
922
- }
923
- ) ;
924
- }
925
-
926
901
#[ tokio:: test]
927
902
async fn it_should_return_the_peers_for_a_given_torrent ( ) {
928
- let tracker = public_tracker ( ) ;
903
+ let ( tracker, in_memory_torrent_repository ) = public_tracker_and_in_memory_torrents_repository ( ) ;
929
904
930
905
let info_hash = sample_info_hash ( ) ;
931
906
let peer = sample_peer ( ) ;
932
907
933
908
let _ = tracker. upsert_peer_and_get_stats ( & info_hash, & peer) ;
934
909
935
- let peers = tracker . get_torrent_peers ( & info_hash) ;
910
+ let peers = in_memory_torrent_repository . get_torrent_peers ( & info_hash) ;
936
911
937
912
assert_eq ! ( peers, vec![ Arc :: new( peer) ] ) ;
938
913
}
@@ -957,7 +932,7 @@ mod tests {
957
932
958
933
#[ tokio:: test]
959
934
async fn it_should_return_74_peers_at_the_most_for_a_given_torrent ( ) {
960
- let tracker = public_tracker ( ) ;
935
+ let ( tracker, in_memory_torrent_repository ) = public_tracker_and_in_memory_torrents_repository ( ) ;
961
936
962
937
let info_hash = sample_info_hash ( ) ;
963
938
@@ -975,7 +950,7 @@ mod tests {
975
950
let _ = tracker. upsert_peer_and_get_stats ( & info_hash, & peer) ;
976
951
}
977
952
978
- let peers = tracker . get_torrent_peers ( & info_hash) ;
953
+ let peers = in_memory_torrent_repository . get_torrent_peers ( & info_hash) ;
979
954
980
955
assert_eq ! ( peers. len( ) , 74 ) ;
981
956
}
@@ -989,7 +964,9 @@ mod tests {
989
964
990
965
let _ = tracker. upsert_peer_and_get_stats ( & info_hash, & peer) ;
991
966
992
- let peers = tracker. get_peers_for ( & info_hash, & peer, TORRENT_PEERS_LIMIT ) ;
967
+ let peers = tracker
968
+ . in_memory_torrent_repository
969
+ . get_peers_for ( & info_hash, & peer, TORRENT_PEERS_LIMIT ) ;
993
970
994
971
assert_eq ! ( peers, vec![ ] ) ;
995
972
}
@@ -1019,18 +996,20 @@ mod tests {
1019
996
let _ = tracker. upsert_peer_and_get_stats ( & info_hash, & peer) ;
1020
997
}
1021
998
1022
- let peers = tracker. get_peers_for ( & info_hash, & excluded_peer, TORRENT_PEERS_LIMIT ) ;
999
+ let peers = tracker
1000
+ . in_memory_torrent_repository
1001
+ . get_peers_for ( & info_hash, & excluded_peer, TORRENT_PEERS_LIMIT ) ;
1023
1002
1024
1003
assert_eq ! ( peers. len( ) , 74 ) ;
1025
1004
}
1026
1005
1027
1006
#[ tokio:: test]
1028
1007
async fn it_should_return_the_torrent_metrics ( ) {
1029
- let tracker = public_tracker ( ) ;
1008
+ let ( tracker, in_memory_torrent_repository ) = public_tracker_and_in_memory_torrents_repository ( ) ;
1030
1009
1031
1010
let _ = tracker. upsert_peer_and_get_stats ( & sample_info_hash ( ) , & leecher ( ) ) ;
1032
1011
1033
- let torrent_metrics = tracker . get_torrents_metrics ( ) ;
1012
+ let torrent_metrics = in_memory_torrent_repository . get_torrents_metrics ( ) ;
1034
1013
1035
1014
assert_eq ! (
1036
1015
torrent_metrics,
@@ -1045,7 +1024,7 @@ mod tests {
1045
1024
1046
1025
#[ tokio:: test]
1047
1026
async fn it_should_get_many_the_torrent_metrics ( ) {
1048
- let tracker = public_tracker ( ) ;
1027
+ let ( tracker, in_memory_torrent_repository ) = public_tracker_and_in_memory_torrents_repository ( ) ;
1049
1028
1050
1029
let start_time = std:: time:: Instant :: now ( ) ;
1051
1030
for i in 0 ..1_000_000 {
@@ -1054,7 +1033,7 @@ mod tests {
1054
1033
let result_a = start_time. elapsed ( ) ;
1055
1034
1056
1035
let start_time = std:: time:: Instant :: now ( ) ;
1057
- let torrent_metrics = tracker . get_torrents_metrics ( ) ;
1036
+ let torrent_metrics = in_memory_torrent_repository . get_torrents_metrics ( ) ;
1058
1037
let result_b = start_time. elapsed ( ) ;
1059
1038
1060
1039
assert_eq ! (
@@ -1346,24 +1325,24 @@ mod tests {
1346
1325
1347
1326
#[ tokio:: test]
1348
1327
async fn it_should_authorize_the_announce_and_scrape_actions_on_whitelisted_torrents ( ) {
1349
- let ( tracker , _whitelist_authorization , whitelist_manager) = whitelisted_tracker ( ) ;
1328
+ let ( _tracker , whitelist_authorization , whitelist_manager) = whitelisted_tracker ( ) ;
1350
1329
1351
1330
let info_hash = sample_info_hash ( ) ;
1352
1331
1353
1332
let result = whitelist_manager. add_torrent_to_whitelist ( & info_hash) . await ;
1354
1333
assert ! ( result. is_ok( ) ) ;
1355
1334
1356
- let result = tracker . whitelist_authorization . authorize ( & info_hash) . await ;
1335
+ let result = whitelist_authorization. authorize ( & info_hash) . await ;
1357
1336
assert ! ( result. is_ok( ) ) ;
1358
1337
}
1359
1338
1360
1339
#[ tokio:: test]
1361
1340
async fn it_should_not_authorize_the_announce_and_scrape_actions_on_not_whitelisted_torrents ( ) {
1362
- let ( tracker , _whitelist_authorization , _whitelist_manager) = whitelisted_tracker ( ) ;
1341
+ let ( _tracker , whitelist_authorization , _whitelist_manager) = whitelisted_tracker ( ) ;
1363
1342
1364
1343
let info_hash = sample_info_hash ( ) ;
1365
1344
1366
- let result = tracker . whitelist_authorization . authorize ( & info_hash) . await ;
1345
+ let result = whitelist_authorization. authorize ( & info_hash) . await ;
1367
1346
assert ! ( result. is_err( ) ) ;
1368
1347
}
1369
1348
}
@@ -1479,7 +1458,7 @@ mod tests {
1479
1458
1480
1459
#[ tokio:: test]
1481
1460
async fn it_should_persist_the_number_of_completed_peers_for_all_torrents_into_the_database ( ) {
1482
- let ( tracker, torrents_manager) = tracker_persisting_torrents_in_database ( ) ;
1461
+ let ( tracker, torrents_manager, in_memory_torrent_repository ) = tracker_persisting_torrents_in_database ( ) ;
1483
1462
1484
1463
let info_hash = sample_info_hash ( ) ;
1485
1464
@@ -1494,7 +1473,7 @@ mod tests {
1494
1473
assert_eq ! ( swarm_stats. downloaded, 1 ) ;
1495
1474
1496
1475
// Remove the newly updated torrent from memory
1497
- let _unused = tracker . in_memory_torrent_repository . remove ( & info_hash) ;
1476
+ let _unused = in_memory_torrent_repository. remove ( & info_hash) ;
1498
1477
1499
1478
torrents_manager. load_torrents_from_database ( ) . unwrap ( ) ;
1500
1479
0 commit comments