-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathintegration.rs
More file actions
140 lines (109 loc) · 4.63 KB
/
Copy pathintegration.rs
File metadata and controls
140 lines (109 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
mod common;
use common::fixtures::{ephemeral_configuration, remote_client_ip, sample_info_hash, sample_peer};
use common::test_env::TestEnv;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::{AnnounceData, AnnouncePolicy};
#[tokio::test]
async fn it_should_handle_the_announce_request() {
let mut test_env = TestEnv::started(ephemeral_configuration()).await;
let announce_data = test_env
.announce_peer_started(sample_peer(), &remote_client_ip(), &sample_info_hash())
.await;
assert_eq!(
announce_data,
AnnounceData {
peers: vec![],
stats: SwarmMetadata {
downloaded: 0,
complete: 1,
incomplete: 0
},
policy: AnnouncePolicy {
interval: 120,
interval_min: 120,
max_peers_per_announce: 74,
}
}
);
}
#[tokio::test]
async fn it_should_not_return_the_peer_making_the_announce_request() {
let mut test_env = TestEnv::started(ephemeral_configuration()).await;
let announce_data = test_env
.announce_peer_started(sample_peer(), &remote_client_ip(), &sample_info_hash())
.await;
assert_eq!(announce_data.peers.len(), 0);
}
#[tokio::test]
async fn it_should_handle_the_scrape_request() {
let mut test_env = TestEnv::started(ephemeral_configuration()).await;
let info_hash = sample_info_hash();
let _announce_data = test_env
.announce_peer_started(sample_peer(), &remote_client_ip(), &info_hash)
.await;
let scrape_data = test_env.scrape(&info_hash).await;
assert!(scrape_data.files.contains_key(&info_hash));
}
#[tokio::test]
async fn it_should_persist_the_number_of_completed_peers_for_each_torrent_into_the_database() {
let mut core_config = ephemeral_configuration();
core_config.tracker_policy.persistent_torrent_completed_stat = true;
let mut test_env = TestEnv::started(core_config).await;
let info_hash = sample_info_hash();
test_env
.increase_number_of_downloads(sample_peer(), &remote_client_ip(), &info_hash)
.await;
assert_eq!(test_env.get_swarm_metadata(&info_hash).await.unwrap().downloads(), 1);
test_env.remove_swarm(&info_hash).await;
// Ensure the swarm metadata is removed
assert!(test_env.get_swarm_metadata(&info_hash).await.is_none());
// Load torrents from the database to ensure the completed stats are persisted.
// Bound the wait with a timeout instead of a fixed iteration count so the
// test fails loudly on a stalled system rather than after an arbitrary
// number of immediate retries. Re-check the desired state (`downloads == 1`)
// inside the retry condition so an intermediate observation does not
// panic the test before the background listener has finished applying
// the persisted value.
let restored = tokio::time::timeout(std::time::Duration::from_secs(5), async {
loop {
test_env
.tracker_core_container
.torrents_manager
.load_torrents_from_database()
.await
.unwrap();
if let Some(swarm_metadata) = test_env.get_swarm_metadata(&info_hash).await
&& swarm_metadata.downloads() == 1
{
break true;
}
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
})
.await
.unwrap_or(false);
assert!(restored);
}
#[tokio::test]
async fn it_should_persist_the_global_number_of_completed_peers_into_the_database() {
let mut core_config = ephemeral_configuration();
core_config.tracker_policy.persistent_torrent_completed_stat = true;
let mut test_env = TestEnv::started(core_config.clone()).await;
test_env
.increase_number_of_downloads(sample_peer(), &remote_client_ip(), &sample_info_hash())
.await;
// Wait for the event listener to persist the download count to the database
// before simulating a restart. Without this, the new test environment may
// start before the background task has written to the database, causing a
// flaky failure under high-concurrency environments such as Docker builds.
test_env.wait_for_global_downloads_persisted(1).await;
// We run a new instance of the test environment to simulate a restart.
// The new instance uses the same underlying database.
let new_test_env = TestEnv::started(core_config).await;
assert_eq!(
new_test_env
.get_counter_value("tracker_core_persistent_torrents_downloads_total")
.await,
1
);
}