Skip to content

Commit 4ad9815

Browse files
committed
refactor: remove duplicate code
1 parent 0097c85 commit 4ad9815

File tree

7 files changed

+70
-158
lines changed

7 files changed

+70
-158
lines changed

packages/test-helpers/src/configuration.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ pub fn ephemeral_with_reverse_proxy() -> Configuration {
5555
cfg
5656
}
5757

58+
#[must_use]
59+
pub fn ephemeral_without_reverse_proxy() -> Configuration {
60+
let mut cfg = ephemeral();
61+
62+
cfg.on_reverse_proxy = false;
63+
64+
cfg
65+
}
66+
5867
#[must_use]
5968
pub fn ephemeral_mode_public() -> Configuration {
6069
let mut cfg = ephemeral();

src/http/axum_implementation/handlers/announce.rs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -138,56 +138,30 @@ fn map_to_aquatic_event(event: &Option<Event>) -> AnnounceEvent {
138138

139139
#[cfg(test)]
140140
mod tests {
141-
use std::sync::Arc;
142141

143-
use torrust_tracker_configuration::Configuration;
144-
use torrust_tracker_primitives::TrackerMode;
145142
use torrust_tracker_test_helpers::configuration;
146143

147144
use crate::http::axum_implementation::requests::announce::Announce;
148145
use crate::http::axum_implementation::responses;
149146
use crate::http::axum_implementation::services::peer_ip_resolver::ClientIpSources;
150147
use crate::protocol::info_hash::InfoHash;
151-
use crate::tracker::statistics::Keeper;
148+
use crate::tracker::services::common::tracker_factory;
152149
use crate::tracker::{peer, Tracker};
153150

154151
fn private_tracker() -> Tracker {
155-
let mut configuration = configuration::ephemeral();
156-
configuration.mode = TrackerMode::Private;
157-
tracker_factory(configuration)
152+
tracker_factory(configuration::ephemeral_mode_private().into())
158153
}
159154

160-
fn listed_tracker() -> Tracker {
161-
let mut configuration = configuration::ephemeral();
162-
configuration.mode = TrackerMode::Listed;
163-
tracker_factory(configuration)
155+
fn whitelisted_tracker() -> Tracker {
156+
tracker_factory(configuration::ephemeral_mode_whitelisted().into())
164157
}
165158

166159
fn tracker_on_reverse_proxy() -> Tracker {
167-
let mut configuration = configuration::ephemeral();
168-
configuration.on_reverse_proxy = true;
169-
tracker_factory(configuration)
160+
tracker_factory(configuration::ephemeral_with_reverse_proxy().into())
170161
}
171162

172163
fn tracker_not_on_reverse_proxy() -> Tracker {
173-
let mut configuration = configuration::ephemeral();
174-
configuration.on_reverse_proxy = false;
175-
tracker_factory(configuration)
176-
}
177-
178-
fn tracker_factory(configuration: Configuration) -> Tracker {
179-
// code-review: the tracker initialization is duplicated in many places. Consider make this function public.
180-
181-
// Initialize stats tracker
182-
let (stats_event_sender, stats_repository) = Keeper::new_active_instance();
183-
184-
// Initialize Torrust tracker
185-
match Tracker::new(Arc::new(configuration), Some(stats_event_sender), stats_repository) {
186-
Ok(tracker) => tracker,
187-
Err(error) => {
188-
panic!("{}", error)
189-
}
190-
}
164+
tracker_factory(configuration::ephemeral_without_reverse_proxy().into())
191165
}
192166

193167
fn sample_announce_request() -> Announce {
@@ -263,13 +237,13 @@ mod tests {
263237

264238
use std::sync::Arc;
265239

266-
use super::{listed_tracker, sample_announce_request, sample_client_ip_sources};
240+
use super::{sample_announce_request, sample_client_ip_sources, whitelisted_tracker};
267241
use crate::http::axum_implementation::handlers::announce::handle_announce;
268242
use crate::http::axum_implementation::handlers::announce::tests::assert_error_response;
269243

270244
#[tokio::test]
271245
async fn it_should_fail_when_the_announced_torrent_is_not_whitelisted() {
272-
let tracker = Arc::new(listed_tracker());
246+
let tracker = Arc::new(whitelisted_tracker());
273247

274248
let announce_request = sample_announce_request();
275249

src/http/axum_implementation/handlers/scrape.rs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -96,56 +96,30 @@ fn build_response(scrape_data: ScrapeData) -> Response {
9696
mod tests {
9797
use std::net::IpAddr;
9898
use std::str::FromStr;
99-
use std::sync::Arc;
10099

101-
use torrust_tracker_configuration::Configuration;
102-
use torrust_tracker_primitives::TrackerMode;
103100
use torrust_tracker_test_helpers::configuration;
104101

105102
use crate::http::axum_implementation::requests::scrape::Scrape;
106103
use crate::http::axum_implementation::responses;
107104
use crate::http::axum_implementation::services::peer_ip_resolver::ClientIpSources;
108105
use crate::protocol::info_hash::InfoHash;
109-
use crate::tracker::statistics::Keeper;
106+
use crate::tracker::services::common::tracker_factory;
110107
use crate::tracker::Tracker;
111108

112109
fn private_tracker() -> Tracker {
113-
let mut configuration = configuration::ephemeral();
114-
configuration.mode = TrackerMode::Private;
115-
tracker_factory(configuration)
110+
tracker_factory(configuration::ephemeral_mode_private().into())
116111
}
117112

118-
fn listed_tracker() -> Tracker {
119-
let mut configuration = configuration::ephemeral();
120-
configuration.mode = TrackerMode::Listed;
121-
tracker_factory(configuration)
113+
fn whitelisted_tracker() -> Tracker {
114+
tracker_factory(configuration::ephemeral_mode_whitelisted().into())
122115
}
123116

124117
fn tracker_on_reverse_proxy() -> Tracker {
125-
let mut configuration = configuration::ephemeral();
126-
configuration.on_reverse_proxy = true;
127-
tracker_factory(configuration)
118+
tracker_factory(configuration::ephemeral_with_reverse_proxy().into())
128119
}
129120

130121
fn tracker_not_on_reverse_proxy() -> Tracker {
131-
let mut configuration = configuration::ephemeral();
132-
configuration.on_reverse_proxy = false;
133-
tracker_factory(configuration)
134-
}
135-
136-
fn tracker_factory(configuration: Configuration) -> Tracker {
137-
// code-review: the tracker initialization is duplicated in many places. Consider make this function public.
138-
139-
// Initialize stats tracker
140-
let (stats_event_sender, stats_repository) = Keeper::new_active_instance();
141-
142-
// Initialize Torrust tracker
143-
match Tracker::new(Arc::new(configuration), Some(stats_event_sender), stats_repository) {
144-
Ok(tracker) => tracker,
145-
Err(error) => {
146-
panic!("{}", error)
147-
}
148-
}
122+
tracker_factory(configuration::ephemeral_without_reverse_proxy().into())
149123
}
150124

151125
fn sample_scrape_request() -> Scrape {
@@ -214,13 +188,13 @@ mod tests {
214188

215189
use std::sync::Arc;
216190

217-
use super::{listed_tracker, sample_client_ip_sources, sample_scrape_request};
191+
use super::{sample_client_ip_sources, sample_scrape_request, whitelisted_tracker};
218192
use crate::http::axum_implementation::handlers::scrape::handle_scrape;
219193
use crate::tracker::ScrapeData;
220194

221195
#[tokio::test]
222196
async fn it_should_return_zeroed_swarm_metadata_when_the_torrent_is_not_whitelisted() {
223-
let tracker = Arc::new(listed_tracker());
197+
let tracker = Arc::new(whitelisted_tracker());
224198

225199
let scrape_request = sample_scrape_request();
226200

src/http/axum_implementation/services/announce.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,17 @@ pub async fn invoke(tracker: Arc<Tracker>, info_hash: InfoHash, peer: &mut Peer)
2626
#[cfg(test)]
2727
mod tests {
2828
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
29-
use std::sync::Arc;
3029

3130
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
32-
use torrust_tracker_configuration::Configuration;
33-
use torrust_tracker_primitives::TrackerMode;
3431
use torrust_tracker_test_helpers::configuration;
3532

3633
use crate::protocol::clock::DurationSinceUnixEpoch;
3734
use crate::protocol::info_hash::InfoHash;
38-
use crate::tracker::statistics::Keeper;
35+
use crate::tracker::services::common::tracker_factory;
3936
use crate::tracker::{peer, Tracker};
4037

4138
fn public_tracker() -> Tracker {
42-
let mut configuration = configuration::ephemeral();
43-
configuration.mode = TrackerMode::Public;
44-
tracker_factory(configuration)
45-
}
46-
47-
fn tracker_factory(configuration: Configuration) -> Tracker {
48-
// code-review: the tracker initialization is duplicated in many places. Consider make this function public.
49-
50-
// Initialize stats tracker
51-
let (stats_event_sender, stats_repository) = Keeper::new_active_instance();
52-
53-
// Initialize Torrust tracker
54-
match Tracker::new(Arc::new(configuration), Some(stats_event_sender), stats_repository) {
55-
Ok(tracker) => tracker,
56-
Err(error) => {
57-
panic!("{}", error)
58-
}
59-
}
39+
tracker_factory(configuration::ephemeral_mode_public().into())
6040
}
6141

6242
fn sample_info_hash() -> InfoHash {

src/tracker/mod.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,6 @@ mod tests {
555555
use std::sync::Arc;
556556

557557
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
558-
use torrust_tracker_primitives::TrackerMode;
559558
use torrust_tracker_test_helpers::configuration;
560559

561560
use crate::protocol::clock::DurationSinceUnixEpoch;
@@ -564,22 +563,16 @@ mod tests {
564563
use crate::tracker::services::common::tracker_factory;
565564
use crate::tracker::{TorrentsMetrics, Tracker};
566565

567-
pub fn public_tracker() -> Tracker {
568-
let mut configuration = configuration::ephemeral();
569-
configuration.mode = TrackerMode::Public;
570-
tracker_factory(Arc::new(configuration))
566+
fn public_tracker() -> Tracker {
567+
tracker_factory(configuration::ephemeral_mode_public().into())
571568
}
572569

573-
pub fn private_tracker() -> Tracker {
574-
let mut configuration = configuration::ephemeral();
575-
configuration.mode = TrackerMode::Private;
576-
tracker_factory(Arc::new(configuration))
570+
fn private_tracker() -> Tracker {
571+
tracker_factory(configuration::ephemeral_mode_private().into())
577572
}
578573

579-
pub fn whitelisted_tracker() -> Tracker {
580-
let mut configuration = configuration::ephemeral();
581-
configuration.mode = TrackerMode::Listed;
582-
tracker_factory(Arc::new(configuration))
574+
fn whitelisted_tracker() -> Tracker {
575+
tracker_factory(configuration::ephemeral_mode_whitelisted().into())
583576
}
584577

585578
pub fn tracker_persisting_torrents_in_database() -> Tracker {

0 commit comments

Comments
 (0)