Skip to content

Commit 2c9311b

Browse files
committed
test: [#1539] add integration test for persisted downloads counter
1 parent c07f366 commit 2c9311b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

packages/tracker-core/tests/common/test_env.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ use aquatic_udp_protocol::AnnounceEvent;
55
use bittorrent_primitives::info_hash::InfoHash;
66
use bittorrent_tracker_core::announce_handler::PeersWanted;
77
use bittorrent_tracker_core::container::TrackerCoreContainer;
8+
use bittorrent_tracker_core::statistics::persisted_metrics::load_persisted_metrics;
89
use tokio::task::yield_now;
910
use torrust_tracker_configuration::Core;
11+
use torrust_tracker_metrics::label::LabelSet;
12+
use torrust_tracker_metrics::metric::MetricName;
1013
use torrust_tracker_primitives::core::{AnnounceData, ScrapeData};
1114
use torrust_tracker_primitives::peer::Peer;
1215
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
16+
use torrust_tracker_primitives::DurationSinceUnixEpoch;
1317
use torrust_tracker_torrent_repository::container::TorrentRepositoryContainer;
1418

1519
pub struct TestEnv {
@@ -45,6 +49,22 @@ impl TestEnv {
4549
}
4650

4751
pub async fn start(&self) {
52+
let now = DurationSinceUnixEpoch::from_secs(0);
53+
self.load_persisted_metrics(now).await;
54+
self.run_jobs().await;
55+
}
56+
57+
async fn load_persisted_metrics(&self, now: DurationSinceUnixEpoch) {
58+
load_persisted_metrics(
59+
&self.tracker_core_container.stats_repository,
60+
&self.tracker_core_container.db_torrent_repository,
61+
now,
62+
)
63+
.await
64+
.unwrap();
65+
}
66+
67+
async fn run_jobs(&self) {
4868
let mut jobs = vec![];
4969

5070
let job = torrust_tracker_torrent_repository::statistics::event::listener::run_event_listener(
@@ -135,4 +155,15 @@ impl TestEnv {
135155
pub async fn remove_swarm(&self, info_hash: &InfoHash) {
136156
self.torrent_repository_container.swarms.remove(info_hash).await.unwrap();
137157
}
158+
159+
pub async fn get_counter_value(&self, metric_name: &str) -> u64 {
160+
self.tracker_core_container
161+
.stats_repository
162+
.get_metrics()
163+
.await
164+
.metric_collection
165+
.get_counter_value(&MetricName::new(metric_name), &LabelSet::default())
166+
.unwrap()
167+
.value()
168+
}
138169
}

packages/tracker-core/tests/integration.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,28 @@ async fn it_should_persist_the_number_of_completed_peers_for_each_torrent_into_t
8686

8787
assert!(test_env.get_swarm_metadata(&info_hash).await.unwrap().downloads() == 1);
8888
}
89+
90+
#[tokio::test]
91+
async fn it_should_persist_the_global_number_of_completed_peers_into_the_database() {
92+
let mut core_config = ephemeral_configuration();
93+
94+
core_config.tracker_policy.persistent_torrent_completed_stat = true;
95+
96+
let mut test_env = TestEnv::started(core_config.clone()).await;
97+
98+
test_env
99+
.increase_number_of_downloads(sample_peer(), &remote_client_ip(), &sample_info_hash())
100+
.await;
101+
102+
// We run a new instance of the test environment to simulate a restart.
103+
// The new instance uses the same underlying database.
104+
105+
let new_test_env = TestEnv::started(core_config).await;
106+
107+
assert_eq!(
108+
new_test_env
109+
.get_counter_value("tracker_core_persistent_torrents_downloads_total")
110+
.await,
111+
1
112+
);
113+
}

0 commit comments

Comments
 (0)