Skip to content

Commit 1a78c7a

Browse files
committed
udp-tracker-core: scrape service test + helper fn
1 parent 3d87fc3 commit 1a78c7a

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

packages/udp-tracker-core/src/services/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub mod scrape;
77
pub(crate) mod tests {
88

99
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
10+
use std::ops::Range;
1011

1112
use futures::future::BoxFuture;
1213
use mockall::mock;
@@ -39,6 +40,10 @@ pub(crate) mod tests {
3940
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 8080)
4041
}
4142

43+
pub(crate) fn sample_cookie_valid_range() -> Range<f64> {
44+
sample_issue_time() - 10.0..sample_issue_time() + 10.0
45+
}
46+
4247
pub(crate) fn sample_issue_time() -> f64 {
4348
1_000_000_000_f64
4449
}

packages/udp-tracker-core/src/services/scrape.rs

+55
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,58 @@ impl From<WhitelistError> for UdpScrapeError {
127127
Self::TrackerCoreWhitelistError { source: whitelist_error }
128128
}
129129
}
130+
131+
#[cfg(test)]
132+
mod tests {
133+
use aquatic_udp_protocol::{ConnectionId, InfoHash, ScrapeRequest, TransactionId};
134+
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
135+
use bittorrent_tracker_core::whitelist;
136+
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
137+
use torrust_tracker_test_helpers::configuration;
138+
139+
use super::*;
140+
//use crate::connection_cookie::make;
141+
use crate::services::tests::{
142+
sample_cookie_valid_range,
143+
// sample_ipv4_remote_addr_fingerprint,
144+
// sample_issue_time
145+
sample_ipv4_remote_addr,
146+
};
147+
use crate::statistics::repository::Repository;
148+
149+
#[tokio::test]
150+
async fn should_increase_scrape_event_when_handling_scrape() {
151+
let config = configuration::ephemeral_public();
152+
let _stats_repository = Repository::new();
153+
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
154+
let whitelist_authorization = Arc::new(whitelist::authorization::WhitelistAuthorization::new(
155+
&config.core,
156+
&in_memory_whitelist.clone(),
157+
));
158+
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
159+
let (udp_core_stats_event_sender, _udp_core_stats_repository) = statistics::setup::factory(false);
160+
let udp_core_stats_event_sender = Arc::new(udp_core_stats_event_sender);
161+
let info_hash = InfoHash([0u8; 20]);
162+
let info_hashes = vec![info_hash];
163+
let request = ScrapeRequest {
164+
connection_id: ConnectionId(0i64.into()),
165+
transaction_id: TransactionId(0i32.into()),
166+
info_hashes,
167+
};
168+
169+
let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository));
170+
let scrape_service = ScrapeService::new(scrape_handler, udp_core_stats_event_sender);
171+
let _result = scrape_service
172+
.handle_scrape(sample_ipv4_remote_addr(), &request, sample_cookie_valid_range())
173+
.await;
174+
/*let _stats = stats_repository.get_stats().await;
175+
assert_eq!(
176+
response.unwrap().files.keys().next().unwrap().0,
177+
info_hash.0,
178+
179+
);
180+
*/
181+
// );
182+
//assert_eq!(stats.udp4_scrapes_handled, 1)
183+
}
184+
}

0 commit comments

Comments
 (0)