@@ -38,6 +38,9 @@ pub struct Tracker {
3838
3939#[ derive( Debug , PartialEq , Default ) ]
4040pub struct TorrentsMetrics {
41+ // code-review: consider using `SwamStats` for
42+ // `seeders`, `completed`, and `leechers` attributes.
43+ // pub swam_stats: SwamStats;
4144 pub seeders : u64 ,
4245 pub completed : u64 ,
4346 pub leechers : u64 ,
@@ -223,7 +226,7 @@ impl Tracker {
223226 /// # Errors
224227 ///
225228 /// Will return a `database::Error` if unable to `load_keys` from the database.
226- pub async fn load_keys ( & self ) -> Result < ( ) , databases:: error:: Error > {
229+ pub async fn load_keys_from_database ( & self ) -> Result < ( ) , databases:: error:: Error > {
227230 let keys_from_database = self . database . load_keys ( ) . await ?;
228231 let mut keys = self . keys . write ( ) . await ;
229232
@@ -301,7 +304,7 @@ impl Tracker {
301304 /// # Errors
302305 ///
303306 /// Will return a `database::Error` if unable to load the list whitelisted `info_hash`s from the database.
304- pub async fn load_whitelist ( & self ) -> Result < ( ) , databases:: error:: Error > {
307+ pub async fn load_whitelist_from_database ( & self ) -> Result < ( ) , databases:: error:: Error > {
305308 let whitelisted_torrents_from_database = self . database . load_whitelist ( ) . await ?;
306309 let mut whitelist = self . whitelist . write ( ) . await ;
307310
@@ -402,7 +405,7 @@ impl Tracker {
402405 /// # Errors
403406 ///
404407 /// Will return a `database::Error` if unable to load the list of `persistent_torrents` from the database.
405- pub async fn load_persistent_torrents ( & self ) -> Result < ( ) , databases:: error:: Error > {
408+ pub async fn load_torrents_from_database ( & self ) -> Result < ( ) , databases:: error:: Error > {
406409 let persistent_torrents = self . database . load_persistent_torrents ( ) . await ?;
407410
408411 let mut torrents = self . torrents . write ( ) . await ;
@@ -700,6 +703,55 @@ mod tests {
700703 ) ;
701704 }
702705
706+ #[ tokio:: test]
707+ async fn it_should_return_all_the_peers_for_a_given_torrent ( ) {
708+ let tracker = public_tracker ( ) ;
709+
710+ let info_hash = sample_info_hash ( ) ;
711+ let peer = sample_peer ( ) ;
712+
713+ tracker. update_torrent_with_peer_and_get_stats ( & info_hash, & peer) . await ;
714+
715+ let peers = tracker. get_all_torrent_peers ( & info_hash) . await ;
716+
717+ assert_eq ! ( peers, vec![ peer] ) ;
718+ }
719+
720+ #[ tokio:: test]
721+ async fn it_should_return_all_the_peers_for_a_given_torrent_excluding_a_given_peer ( ) {
722+ let tracker = public_tracker ( ) ;
723+
724+ let info_hash = sample_info_hash ( ) ;
725+ let peer = sample_peer ( ) ;
726+
727+ tracker. update_torrent_with_peer_and_get_stats ( & info_hash, & peer) . await ;
728+
729+ let peers = tracker. get_peers_for_peer ( & info_hash, & peer) . await ;
730+
731+ assert_eq ! ( peers, vec![ ] ) ;
732+ }
733+
734+ #[ tokio:: test]
735+ async fn it_should_return_the_torrent_metrics ( ) {
736+ let tracker = public_tracker ( ) ;
737+
738+ tracker
739+ . update_torrent_with_peer_and_get_stats ( & sample_info_hash ( ) , & leecher ( ) )
740+ . await ;
741+
742+ let torrent_metrics = tracker. get_torrents_metrics ( ) . await ;
743+
744+ assert_eq ! (
745+ torrent_metrics,
746+ TorrentsMetrics {
747+ seeders: 0 ,
748+ completed: 0 ,
749+ leechers: 1 ,
750+ torrents: 1 ,
751+ }
752+ ) ;
753+ }
754+
703755 mod for_all_config_modes {
704756
705757 mod handling_an_announce_request {
@@ -984,6 +1036,55 @@ mod tests {
9841036 }
9851037 }
9861038
1039+ mod handling_the_torrent_whitelist {
1040+ use crate :: tracker:: tests:: the_tracker:: { sample_info_hash, whitelisted_tracker} ;
1041+
1042+ #[ tokio:: test]
1043+ async fn it_should_add_a_torrent_to_the_whitelist ( ) {
1044+ let tracker = whitelisted_tracker ( ) ;
1045+
1046+ let info_hash = sample_info_hash ( ) ;
1047+
1048+ tracker. add_torrent_to_whitelist ( & info_hash) . await . unwrap ( ) ;
1049+
1050+ assert ! ( tracker. is_info_hash_whitelisted( & info_hash) . await ) ;
1051+ }
1052+
1053+ #[ tokio:: test]
1054+ async fn it_should_remove_a_torrent_from_the_whitelist ( ) {
1055+ let tracker = whitelisted_tracker ( ) ;
1056+
1057+ let info_hash = sample_info_hash ( ) ;
1058+
1059+ tracker. add_torrent_to_whitelist ( & info_hash) . await . unwrap ( ) ;
1060+
1061+ tracker. remove_torrent_from_whitelist ( & info_hash) . await . unwrap ( ) ;
1062+
1063+ assert ! ( !tracker. is_info_hash_whitelisted( & info_hash) . await ) ;
1064+ }
1065+
1066+ mod persistence {
1067+ use crate :: tracker:: tests:: the_tracker:: { sample_info_hash, whitelisted_tracker} ;
1068+
1069+ #[ tokio:: test]
1070+ async fn it_should_load_the_whitelist_from_the_database ( ) {
1071+ let tracker = whitelisted_tracker ( ) ;
1072+
1073+ let info_hash = sample_info_hash ( ) ;
1074+
1075+ tracker. add_torrent_to_whitelist ( & info_hash) . await . unwrap ( ) ;
1076+
1077+ // Remove torrent from the in-memory whitelist
1078+ tracker. whitelist . write ( ) . await . remove ( & info_hash) ;
1079+ assert ! ( !tracker. is_info_hash_whitelisted( & info_hash) . await ) ;
1080+
1081+ tracker. load_whitelist_from_database ( ) . await . unwrap ( ) ;
1082+
1083+ assert ! ( tracker. is_info_hash_whitelisted( & info_hash) . await ) ;
1084+ }
1085+ }
1086+ }
1087+
9871088 mod handling_an_announce_request { }
9881089
9891090 mod handling_an_scrape_request {
@@ -1112,7 +1213,7 @@ mod tests {
11121213 // Remove the newly generated key in memory
11131214 tracker. keys . write ( ) . await . remove ( & key. id ( ) ) ;
11141215
1115- let result = tracker. load_keys ( ) . await ;
1216+ let result = tracker. load_keys_from_database ( ) . await ;
11161217
11171218 assert ! ( result. is_ok( ) ) ;
11181219 assert ! ( tracker. verify_auth_key( & key. id( ) ) . await . is_ok( ) ) ;
@@ -1152,13 +1253,10 @@ mod tests {
11521253 let swarm_stats = tracker. update_torrent_with_peer_and_get_stats ( & info_hash, & peer) . await ;
11531254 assert_eq ! ( swarm_stats. completed, 1 ) ;
11541255
1155- let torrents = tracker. get_all_torrent_peers ( & info_hash) . await ;
1156- assert_eq ! ( torrents. len( ) , 1 ) ;
1157-
11581256 // Remove the newly updated torrent from memory
11591257 tracker. torrents . write ( ) . await . remove ( & info_hash) ;
11601258
1161- tracker. load_persistent_torrents ( ) . await . unwrap ( ) ;
1259+ tracker. load_torrents_from_database ( ) . await . unwrap ( ) ;
11621260
11631261 let torrents = tracker. get_torrents ( ) . await ;
11641262 assert ! ( torrents. contains_key( & info_hash) ) ;
0 commit comments