Skip to content

Commit 6fb632e

Browse files
committed
refactor: [torrust#1201] remove duplicate code
1 parent 4e3dbae commit 6fb632e

File tree

3 files changed

+37
-113
lines changed

3 files changed

+37
-113
lines changed

src/core/services/torrent.rs

+35-113
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,37 @@ pub async fn get_torrents(tracker: Arc<Tracker>, info_hashes: &[InfoHash]) -> Ve
107107
#[cfg(test)]
108108
mod tests {
109109
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
110+
use std::sync::Arc;
110111

111112
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
113+
use torrust_tracker_configuration::Configuration;
112114
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};
113115

116+
use crate::app_test::initialize_tracker_dependencies;
117+
use crate::core::services::initialize_tracker;
118+
use crate::core::Tracker;
119+
120+
fn initialize_tracker_and_deps(config: &Configuration) -> Arc<Tracker> {
121+
let (
122+
database,
123+
_in_memory_whitelist,
124+
whitelist_authorization,
125+
_authentication_service,
126+
in_memory_torrent_repository,
127+
db_torrent_repository,
128+
torrents_manager,
129+
) = initialize_tracker_dependencies(config);
130+
131+
Arc::new(initialize_tracker(
132+
config,
133+
&database,
134+
&whitelist_authorization,
135+
&in_memory_torrent_repository,
136+
&db_torrent_repository,
137+
&torrents_manager,
138+
))
139+
}
140+
114141
fn sample_peer() -> peer::Peer {
115142
peer::Peer {
116143
peer_id: PeerId(*b"-qB00000000000000000"),
@@ -134,7 +161,7 @@ mod tests {
134161

135162
use crate::app_test::initialize_tracker_dependencies;
136163
use crate::core::services::initialize_tracker;
137-
use crate::core::services::torrent::tests::sample_peer;
164+
use crate::core::services::torrent::tests::{initialize_tracker_and_deps, sample_peer};
138165
use crate::core::services::torrent::{get_torrent_info, Info};
139166

140167
pub fn tracker_configuration() -> Configuration {
@@ -179,24 +206,7 @@ mod tests {
179206
async fn should_return_the_torrent_info_if_the_tracker_has_the_torrent() {
180207
let config = tracker_configuration();
181208

182-
let (
183-
database,
184-
_in_memory_whitelist,
185-
whitelist_authorization,
186-
_authentication_service,
187-
in_memory_torrent_repository,
188-
db_torrent_repository,
189-
torrents_manager,
190-
) = initialize_tracker_dependencies(&config);
191-
192-
let tracker = Arc::new(initialize_tracker(
193-
&config,
194-
&database,
195-
&whitelist_authorization,
196-
&in_memory_torrent_repository,
197-
&db_torrent_repository,
198-
&torrents_manager,
199-
));
209+
let tracker = initialize_tracker_and_deps(&config);
200210

201211
let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
202212
let info_hash = InfoHash::from_str(&hash).unwrap();
@@ -220,15 +230,12 @@ mod tests {
220230
mod searching_for_torrents {
221231

222232
use std::str::FromStr;
223-
use std::sync::Arc;
224233

225234
use bittorrent_primitives::info_hash::InfoHash;
226235
use torrust_tracker_configuration::Configuration;
227236
use torrust_tracker_test_helpers::configuration;
228237

229-
use crate::app_test::initialize_tracker_dependencies;
230-
use crate::core::services::initialize_tracker;
231-
use crate::core::services::torrent::tests::sample_peer;
238+
use crate::core::services::torrent::tests::{initialize_tracker_and_deps, sample_peer};
232239
use crate::core::services::torrent::{get_torrents_page, BasicInfo, Pagination};
233240

234241
pub fn tracker_configuration() -> Configuration {
@@ -239,24 +246,7 @@ mod tests {
239246
async fn should_return_an_empty_result_if_the_tracker_does_not_have_any_torrent() {
240247
let config = tracker_configuration();
241248

242-
let (
243-
database,
244-
_in_memory_whitelist,
245-
whitelist_authorization,
246-
_authentication_service,
247-
in_memory_torrent_repository,
248-
db_torrent_repository,
249-
torrents_manager,
250-
) = initialize_tracker_dependencies(&config);
251-
252-
let tracker = Arc::new(initialize_tracker(
253-
&config,
254-
&database,
255-
&whitelist_authorization,
256-
&in_memory_torrent_repository,
257-
&db_torrent_repository,
258-
&torrents_manager,
259-
));
249+
let tracker = initialize_tracker_and_deps(&config);
260250

261251
let torrents = get_torrents_page(tracker.clone(), Some(&Pagination::default())).await;
262252

@@ -267,24 +257,7 @@ mod tests {
267257
async fn should_return_a_summarized_info_for_all_torrents() {
268258
let config = tracker_configuration();
269259

270-
let (
271-
database,
272-
_in_memory_whitelist,
273-
whitelist_authorization,
274-
_authentication_service,
275-
in_memory_torrent_repository,
276-
db_torrent_repository,
277-
torrents_manager,
278-
) = initialize_tracker_dependencies(&config);
279-
280-
let tracker = Arc::new(initialize_tracker(
281-
&config,
282-
&database,
283-
&whitelist_authorization,
284-
&in_memory_torrent_repository,
285-
&db_torrent_repository,
286-
&torrents_manager,
287-
));
260+
let tracker = initialize_tracker_and_deps(&config);
288261

289262
let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
290263
let info_hash = InfoHash::from_str(&hash).unwrap();
@@ -308,24 +281,7 @@ mod tests {
308281
async fn should_allow_limiting_the_number_of_torrents_in_the_result() {
309282
let config = tracker_configuration();
310283

311-
let (
312-
database,
313-
_in_memory_whitelist,
314-
whitelist_authorization,
315-
_authentication_service,
316-
in_memory_torrent_repository,
317-
db_torrent_repository,
318-
torrents_manager,
319-
) = initialize_tracker_dependencies(&config);
320-
321-
let tracker = Arc::new(initialize_tracker(
322-
&config,
323-
&database,
324-
&whitelist_authorization,
325-
&in_memory_torrent_repository,
326-
&db_torrent_repository,
327-
&torrents_manager,
328-
));
284+
let tracker = initialize_tracker_and_deps(&config);
329285

330286
let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
331287
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
@@ -347,24 +303,7 @@ mod tests {
347303
async fn should_allow_using_pagination_in_the_result() {
348304
let config = tracker_configuration();
349305

350-
let (
351-
database,
352-
_in_memory_whitelist,
353-
whitelist_authorization,
354-
_authentication_service,
355-
in_memory_torrent_repository,
356-
db_torrent_repository,
357-
torrents_manager,
358-
) = initialize_tracker_dependencies(&config);
359-
360-
let tracker = Arc::new(initialize_tracker(
361-
&config,
362-
&database,
363-
&whitelist_authorization,
364-
&in_memory_torrent_repository,
365-
&db_torrent_repository,
366-
&torrents_manager,
367-
));
306+
let tracker = initialize_tracker_and_deps(&config);
368307

369308
let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
370309
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
@@ -395,24 +334,7 @@ mod tests {
395334
async fn should_return_torrents_ordered_by_info_hash() {
396335
let config = tracker_configuration();
397336

398-
let (
399-
database,
400-
_in_memory_whitelist,
401-
whitelist_authorization,
402-
_authentication_service,
403-
in_memory_torrent_repository,
404-
db_torrent_repository,
405-
torrents_manager,
406-
) = initialize_tracker_dependencies(&config);
407-
408-
let tracker = Arc::new(initialize_tracker(
409-
&config,
410-
&database,
411-
&whitelist_authorization,
412-
&in_memory_torrent_repository,
413-
&db_torrent_repository,
414-
&torrents_manager,
415-
));
337+
let tracker = initialize_tracker_and_deps(&config);
416338

417339
let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
418340
let info_hash1 = InfoHash::from_str(&hash1).unwrap();

src/servers/http/v1/handlers/announce.rs

+1
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ mod tests {
283283
db_torrent_repository,
284284
torrents_manager,
285285
) = initialize_tracker_dependencies(config);
286+
286287
let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
287288
let stats_event_sender = Arc::new(stats_event_sender);
288289

src/servers/udp/handlers.rs

+1
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ mod tests {
525525
db_torrent_repository,
526526
torrents_manager,
527527
) = initialize_tracker_dependencies(config);
528+
528529
let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
529530
let stats_event_sender = Arc::new(stats_event_sender);
530531
let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone());

0 commit comments

Comments
 (0)