@@ -11,7 +11,7 @@ use torrust_tracker_primitives::pagination::Pagination;
11
11
use torrust_tracker_primitives:: peer;
12
12
use torrust_tracker_torrent_repository:: entry:: EntrySync ;
13
13
14
- use crate :: core:: Tracker ;
14
+ use crate :: core:: torrent :: repository :: in_memory :: InMemoryTorrentRepository ;
15
15
16
16
/// It contains all the information the tracker has about a torrent
17
17
#[ derive( Debug , PartialEq ) ]
@@ -44,8 +44,11 @@ pub struct BasicInfo {
44
44
}
45
45
46
46
/// It returns all the information the tracker has about one torrent in a [Info] struct.
47
- pub async fn get_torrent_info ( tracker : Arc < Tracker > , info_hash : & InfoHash ) -> Option < Info > {
48
- let torrent_entry_option = tracker. in_memory_torrent_repository . get ( info_hash) ;
47
+ pub async fn get_torrent_info (
48
+ in_memory_torrent_repository : Arc < InMemoryTorrentRepository > ,
49
+ info_hash : & InfoHash ,
50
+ ) -> Option < Info > {
51
+ let torrent_entry_option = in_memory_torrent_repository. get ( info_hash) ;
49
52
50
53
let torrent_entry = torrent_entry_option?;
51
54
@@ -65,10 +68,13 @@ pub async fn get_torrent_info(tracker: Arc<Tracker>, info_hash: &InfoHash) -> Op
65
68
}
66
69
67
70
/// It returns all the information the tracker has about multiple torrents in a [`BasicInfo`] struct, excluding the peer list.
68
- pub async fn get_torrents_page ( tracker : Arc < Tracker > , pagination : Option < & Pagination > ) -> Vec < BasicInfo > {
71
+ pub async fn get_torrents_page (
72
+ in_memory_torrent_repository : Arc < InMemoryTorrentRepository > ,
73
+ pagination : Option < & Pagination > ,
74
+ ) -> Vec < BasicInfo > {
69
75
let mut basic_infos: Vec < BasicInfo > = vec ! [ ] ;
70
76
71
- for ( info_hash, torrent_entry) in tracker . in_memory_torrent_repository . get_paginated ( pagination) {
77
+ for ( info_hash, torrent_entry) in in_memory_torrent_repository. get_paginated ( pagination) {
72
78
let stats = torrent_entry. get_swarm_metadata ( ) ;
73
79
74
80
basic_infos. push ( BasicInfo {
@@ -83,15 +89,14 @@ pub async fn get_torrents_page(tracker: Arc<Tracker>, pagination: Option<&Pagina
83
89
}
84
90
85
91
/// It returns all the information the tracker has about multiple torrents in a [`BasicInfo`] struct, excluding the peer list.
86
- pub async fn get_torrents ( tracker : Arc < Tracker > , info_hashes : & [ InfoHash ] ) -> Vec < BasicInfo > {
92
+ pub async fn get_torrents (
93
+ in_memory_torrent_repository : Arc < InMemoryTorrentRepository > ,
94
+ info_hashes : & [ InfoHash ] ,
95
+ ) -> Vec < BasicInfo > {
87
96
let mut basic_infos: Vec < BasicInfo > = vec ! [ ] ;
88
97
89
98
for info_hash in info_hashes {
90
- if let Some ( stats) = tracker
91
- . in_memory_torrent_repository
92
- . get ( info_hash)
93
- . map ( |t| t. get_swarm_metadata ( ) )
94
- {
99
+ if let Some ( stats) = in_memory_torrent_repository. get ( info_hash) . map ( |t| t. get_swarm_metadata ( ) ) {
95
100
basic_infos. push ( BasicInfo {
96
101
info_hash : * info_hash,
97
102
seeders : u64:: from ( stats. complete ) ,
@@ -115,9 +120,10 @@ mod tests {
115
120
116
121
use crate :: app_test:: initialize_tracker_dependencies;
117
122
use crate :: core:: services:: initialize_tracker;
123
+ use crate :: core:: torrent:: repository:: in_memory:: InMemoryTorrentRepository ;
118
124
use crate :: core:: Tracker ;
119
125
120
- fn initialize_tracker_and_deps ( config : & Configuration ) -> Arc < Tracker > {
126
+ fn initialize_tracker_and_deps ( config : & Configuration ) -> ( Arc < Tracker > , Arc < InMemoryTorrentRepository > ) {
121
127
let (
122
128
_database,
123
129
_in_memory_whitelist,
@@ -128,12 +134,14 @@ mod tests {
128
134
_torrents_manager,
129
135
) = initialize_tracker_dependencies ( config) ;
130
136
131
- Arc :: new ( initialize_tracker (
137
+ let tracker = Arc :: new ( initialize_tracker (
132
138
config,
133
139
& whitelist_authorization,
134
140
& in_memory_torrent_repository,
135
141
& db_torrent_repository,
136
- ) )
142
+ ) ) ;
143
+
144
+ ( tracker, in_memory_torrent_repository)
137
145
}
138
146
139
147
fn sample_peer ( ) -> peer:: Peer {
@@ -157,40 +165,20 @@ mod tests {
157
165
use torrust_tracker_configuration:: Configuration ;
158
166
use torrust_tracker_test_helpers:: configuration;
159
167
160
- use crate :: app_test:: initialize_tracker_dependencies;
161
- use crate :: core:: services:: initialize_tracker;
162
168
use crate :: core:: services:: torrent:: tests:: { initialize_tracker_and_deps, sample_peer} ;
163
169
use crate :: core:: services:: torrent:: { get_torrent_info, Info } ;
170
+ use crate :: core:: torrent:: repository:: in_memory:: InMemoryTorrentRepository ;
164
171
165
172
pub fn tracker_configuration ( ) -> Configuration {
166
173
configuration:: ephemeral ( )
167
174
}
168
175
169
176
#[ tokio:: test]
170
177
async fn should_return_none_if_the_tracker_does_not_have_the_torrent ( ) {
171
- let config = tracker_configuration ( ) ;
172
-
173
- let (
174
- _database,
175
- _in_memory_whitelist,
176
- whitelist_authorization,
177
- _authentication_service,
178
- in_memory_torrent_repository,
179
- db_torrent_repository,
180
- _torrents_manager,
181
- ) = initialize_tracker_dependencies ( & config) ;
182
-
183
- let tracker = initialize_tracker (
184
- & config,
185
- & whitelist_authorization,
186
- & in_memory_torrent_repository,
187
- & db_torrent_repository,
188
- ) ;
189
-
190
- let tracker = Arc :: new ( tracker) ;
178
+ let in_memory_torrent_repository = Arc :: new ( InMemoryTorrentRepository :: default ( ) ) ;
191
179
192
180
let torrent_info = get_torrent_info (
193
- tracker . clone ( ) ,
181
+ in_memory_torrent_repository . clone ( ) ,
194
182
& InfoHash :: from_str ( "0b3aea4adc213ce32295be85d3883a63bca25446" ) . unwrap ( ) ,
195
183
)
196
184
. await ;
@@ -202,13 +190,15 @@ mod tests {
202
190
async fn should_return_the_torrent_info_if_the_tracker_has_the_torrent ( ) {
203
191
let config = tracker_configuration ( ) ;
204
192
205
- let tracker = initialize_tracker_and_deps ( & config) ;
193
+ let ( tracker, in_memory_torrent_repository ) = initialize_tracker_and_deps ( & config) ;
206
194
207
195
let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d" . to_owned ( ) ;
208
196
let info_hash = InfoHash :: from_str ( & hash) . unwrap ( ) ;
209
197
let _ = tracker. upsert_peer_and_get_stats ( & info_hash, & sample_peer ( ) ) ;
210
198
211
- let torrent_info = get_torrent_info ( tracker. clone ( ) , & info_hash) . await . unwrap ( ) ;
199
+ let torrent_info = get_torrent_info ( in_memory_torrent_repository. clone ( ) , & info_hash)
200
+ . await
201
+ . unwrap ( ) ;
212
202
213
203
assert_eq ! (
214
204
torrent_info,
@@ -226,25 +216,25 @@ mod tests {
226
216
mod searching_for_torrents {
227
217
228
218
use std:: str:: FromStr ;
219
+ use std:: sync:: Arc ;
229
220
230
221
use bittorrent_primitives:: info_hash:: InfoHash ;
231
222
use torrust_tracker_configuration:: Configuration ;
232
223
use torrust_tracker_test_helpers:: configuration;
233
224
234
225
use crate :: core:: services:: torrent:: tests:: { initialize_tracker_and_deps, sample_peer} ;
235
226
use crate :: core:: services:: torrent:: { get_torrents_page, BasicInfo , Pagination } ;
227
+ use crate :: core:: torrent:: repository:: in_memory:: InMemoryTorrentRepository ;
236
228
237
229
pub fn tracker_configuration ( ) -> Configuration {
238
230
configuration:: ephemeral ( )
239
231
}
240
232
241
233
#[ tokio:: test]
242
234
async fn should_return_an_empty_result_if_the_tracker_does_not_have_any_torrent ( ) {
243
- let config = tracker_configuration ( ) ;
244
-
245
- let tracker = initialize_tracker_and_deps ( & config) ;
235
+ let in_memory_torrent_repository = Arc :: new ( InMemoryTorrentRepository :: default ( ) ) ;
246
236
247
- let torrents = get_torrents_page ( tracker . clone ( ) , Some ( & Pagination :: default ( ) ) ) . await ;
237
+ let torrents = get_torrents_page ( in_memory_torrent_repository . clone ( ) , Some ( & Pagination :: default ( ) ) ) . await ;
248
238
249
239
assert_eq ! ( torrents, vec![ ] ) ;
250
240
}
@@ -253,14 +243,14 @@ mod tests {
253
243
async fn should_return_a_summarized_info_for_all_torrents ( ) {
254
244
let config = tracker_configuration ( ) ;
255
245
256
- let tracker = initialize_tracker_and_deps ( & config) ;
246
+ let ( tracker, in_memory_torrent_repository ) = initialize_tracker_and_deps ( & config) ;
257
247
258
248
let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d" . to_owned ( ) ;
259
249
let info_hash = InfoHash :: from_str ( & hash) . unwrap ( ) ;
260
250
261
251
let _ = tracker. upsert_peer_and_get_stats ( & info_hash, & sample_peer ( ) ) ;
262
252
263
- let torrents = get_torrents_page ( tracker . clone ( ) , Some ( & Pagination :: default ( ) ) ) . await ;
253
+ let torrents = get_torrents_page ( in_memory_torrent_repository . clone ( ) , Some ( & Pagination :: default ( ) ) ) . await ;
264
254
265
255
assert_eq ! (
266
256
torrents,
@@ -277,7 +267,7 @@ mod tests {
277
267
async fn should_allow_limiting_the_number_of_torrents_in_the_result ( ) {
278
268
let config = tracker_configuration ( ) ;
279
269
280
- let tracker = initialize_tracker_and_deps ( & config) ;
270
+ let ( tracker, in_memory_torrent_repository ) = initialize_tracker_and_deps ( & config) ;
281
271
282
272
let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d" . to_owned ( ) ;
283
273
let info_hash1 = InfoHash :: from_str ( & hash1) . unwrap ( ) ;
@@ -290,7 +280,7 @@ mod tests {
290
280
let offset = 0 ;
291
281
let limit = 1 ;
292
282
293
- let torrents = get_torrents_page ( tracker . clone ( ) , Some ( & Pagination :: new ( offset, limit) ) ) . await ;
283
+ let torrents = get_torrents_page ( in_memory_torrent_repository . clone ( ) , Some ( & Pagination :: new ( offset, limit) ) ) . await ;
294
284
295
285
assert_eq ! ( torrents. len( ) , 1 ) ;
296
286
}
@@ -299,7 +289,7 @@ mod tests {
299
289
async fn should_allow_using_pagination_in_the_result ( ) {
300
290
let config = tracker_configuration ( ) ;
301
291
302
- let tracker = initialize_tracker_and_deps ( & config) ;
292
+ let ( tracker, in_memory_torrent_repository ) = initialize_tracker_and_deps ( & config) ;
303
293
304
294
let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d" . to_owned ( ) ;
305
295
let info_hash1 = InfoHash :: from_str ( & hash1) . unwrap ( ) ;
@@ -312,7 +302,7 @@ mod tests {
312
302
let offset = 1 ;
313
303
let limit = 4000 ;
314
304
315
- let torrents = get_torrents_page ( tracker . clone ( ) , Some ( & Pagination :: new ( offset, limit) ) ) . await ;
305
+ let torrents = get_torrents_page ( in_memory_torrent_repository . clone ( ) , Some ( & Pagination :: new ( offset, limit) ) ) . await ;
316
306
317
307
assert_eq ! ( torrents. len( ) , 1 ) ;
318
308
assert_eq ! (
@@ -330,7 +320,7 @@ mod tests {
330
320
async fn should_return_torrents_ordered_by_info_hash ( ) {
331
321
let config = tracker_configuration ( ) ;
332
322
333
- let tracker = initialize_tracker_and_deps ( & config) ;
323
+ let ( tracker, in_memory_torrent_repository ) = initialize_tracker_and_deps ( & config) ;
334
324
335
325
let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d" . to_owned ( ) ;
336
326
let info_hash1 = InfoHash :: from_str ( & hash1) . unwrap ( ) ;
@@ -340,7 +330,7 @@ mod tests {
340
330
let info_hash2 = InfoHash :: from_str ( & hash2) . unwrap ( ) ;
341
331
let _ = tracker. upsert_peer_and_get_stats ( & info_hash2, & sample_peer ( ) ) ;
342
332
343
- let torrents = get_torrents_page ( tracker . clone ( ) , Some ( & Pagination :: default ( ) ) ) . await ;
333
+ let torrents = get_torrents_page ( in_memory_torrent_repository . clone ( ) , Some ( & Pagination :: default ( ) ) ) . await ;
344
334
345
335
assert_eq ! (
346
336
torrents,
0 commit comments