Skip to content

Commit 34c159a

Browse files
committed
refactor: [torrust#1495] update method Swarm::meets_retaining_policy
Changed from: ``` /// Returns true if the torrents meets the retention policy, meaning that /// it should be kept in the tracker. pub fn meets_retaining_policy(&self, policy: &TrackerPolicy) -> bool { if policy.persistent_torrent_completed_stat && self.metadata().downloaded > 0 { return true; } if policy.remove_peerless_torrents && self.is_empty() { return false; } true } ``` To: ``` pub fn meets_retaining_policy(&self, policy: &TrackerPolicy) -> bool { !(policy.remove_peerless_torrents && self.is_empty()) } ``` I think the first condition was introduced to avoid loosing the number of downloads we¡hen the torrent is removed becuase there are no peers. Now, we load that number from database when the torrent is added again after removing it from the tracker.
1 parent 0f4596e commit 34c159a

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

packages/torrent-repository/src/swarm.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,20 +190,11 @@ impl Swarm {
190190
self.peers.is_empty()
191191
}
192192

193-
/// Returns true if the torrents meets the retention policy, meaning that
193+
/// Returns true if the swarm meets the retention policy, meaning that
194194
/// it should be kept in the tracker.
195195
#[must_use]
196196
pub fn meets_retaining_policy(&self, policy: &TrackerPolicy) -> bool {
197-
// code-review: why?
198-
if policy.persistent_torrent_completed_stat && self.metadata().downloaded > 0 {
199-
return true;
200-
}
201-
202-
if policy.remove_peerless_torrents && self.is_empty() {
203-
return false;
204-
}
205-
206-
true
197+
!(policy.remove_peerless_torrents && self.is_empty())
207198
}
208199
}
209200

@@ -214,6 +205,7 @@ mod tests {
214205
use std::sync::Arc;
215206

216207
use aquatic_udp_protocol::PeerId;
208+
use torrust_tracker_configuration::TrackerPolicy;
217209
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
218210
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
219211
use torrust_tracker_primitives::DurationSinceUnixEpoch;
@@ -384,6 +376,30 @@ mod tests {
384376
assert_eq!(swarm.len(), 1);
385377
}
386378

379+
#[test]
380+
fn it_should_be_kept_when_empty_if_the_tracker_policy_is_not_to_remove_peerless_torrents() {
381+
let empty_swarm = Swarm::default();
382+
383+
let policy = TrackerPolicy {
384+
remove_peerless_torrents: false,
385+
..Default::default()
386+
};
387+
388+
assert!(empty_swarm.meets_retaining_policy(&policy));
389+
}
390+
391+
#[test]
392+
fn it_should_be_removed_when_empty_if_the_tracker_policy_is_to_remove_peerless_torrents() {
393+
let empty_swarm = Swarm::default();
394+
395+
let policy = TrackerPolicy {
396+
remove_peerless_torrents: true,
397+
..Default::default()
398+
};
399+
400+
assert!(!empty_swarm.meets_retaining_policy(&policy));
401+
}
402+
387403
#[test]
388404
fn it_should_allow_inserting_two_identical_peers_except_for_the_socket_address() {
389405
let mut swarm = Swarm::default();

0 commit comments

Comments
 (0)