Skip to content

Commit

Permalink
fix: all http tracker tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mickvandijke authored and josecelano committed Mar 9, 2023
1 parent d914e5c commit fac2be8
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/apis/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ mod tests {
async fn it_should_be_able_to_start_from_stopped_state_and_then_stop_again() {
let cfg = tracker_configuration();

let tracker = Arc::new(tracker::Tracker::new(&cfg, None, statistics::Repo::new()).unwrap());
let tracker = Arc::new(tracker::Tracker::new(cfg.clone(), None, statistics::Repo::new()).unwrap());

let stopped_api_server = ApiServer::new(cfg.http_api.clone(), tracker);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub enum Error {
Error(String),
}

pub struct Server;
pub struct Launcher;

impl Server {
impl Launcher {
pub fn start_from_tcp_listener_with_graceful_shutdown<F>(
tcp_listener: std::net::TcpListener,
tracker: Arc<Tracker>,
Expand All @@ -30,12 +30,12 @@ impl Server {
where
F: Future<Output = ()> + Send + 'static,
{
let app = router(&tracker);
let app = router(tracker);

Box::pin(async {
axum::Server::from_tcp(tcp_listener)
.expect("Could not bind to tcp listener.")
.serve(app.into_make_service())
.serve(app.into_make_service_with_connect_info::<std::net::SocketAddr>())
.with_graceful_shutdown(shutdown_signal)
.await
.expect("Axum server crashed.");
Expand All @@ -51,7 +51,7 @@ impl Server {
where
F: Future<Output = ()> + Send + 'static,
{
let app = router(&tracker);
let app = router(tracker);

let handle = Handle::new();

Expand All @@ -69,15 +69,15 @@ impl Server {

axum_server::from_tcp_rustls(tcp_listener, tls_config)
.handle(handle)
.serve(app.into_make_service())
.serve(app.into_make_service_with_connect_info::<std::net::SocketAddr>())
.await
.expect("Axum server crashed.");
})
}
}

#[async_trait]
impl HttpServerLauncher for Server {
impl HttpServerLauncher for Launcher {
fn new() -> Self {
Self {}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ impl HttpServerLauncher for Server {
}
}

pub fn start(socket_addr: std::net::SocketAddr, tracker: &Arc<Tracker>) -> impl Future<Output = hyper::Result<()>> {
pub fn start(socket_addr: std::net::SocketAddr, tracker: Arc<Tracker>) -> impl Future<Output = hyper::Result<()>> {
let app = router(tracker);

let server = axum::Server::bind(&socket_addr).serve(app.into_make_service_with_connect_info::<std::net::SocketAddr>());
Expand All @@ -128,7 +128,7 @@ pub fn start(socket_addr: std::net::SocketAddr, tracker: &Arc<Tracker>) -> impl
pub fn start_tls(
socket_addr: std::net::SocketAddr,
ssl_config: RustlsConfig,
tracker: &Arc<Tracker>,
tracker: Arc<Tracker>,
) -> impl Future<Output = Result<(), std::io::Error>> {
let app = router(tracker);

Expand Down
2 changes: 1 addition & 1 deletion src/http/axum_implementation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod extractors;
pub mod handlers;
pub mod launcher;
pub mod query;
pub mod requests;
pub mod responses;
pub mod routes;
pub mod server;
pub mod services;
2 changes: 1 addition & 1 deletion src/http/axum_implementation/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use axum_client_ip::SecureClientIpSource;
use super::handlers::{announce, scrape};
use crate::tracker::Tracker;

pub fn router(tracker: &Arc<Tracker>) -> Router {
pub fn router(tracker: Arc<Tracker>) -> Router {
Router::new()
// Announce request
.route("/announce", get(announce::handle_without_key).with_state(tracker.clone()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pub enum Error {
Error(String),
}

pub struct Server;
pub struct Launcher;

impl Server {
impl Launcher {
pub fn start_with_graceful_shutdown<F>(
addr: SocketAddr,
tracker: Arc<Tracker>,
Expand Down Expand Up @@ -50,7 +50,7 @@ impl Server {
}
}

impl HttpServerLauncher for Server {
impl HttpServerLauncher for Launcher {
fn new() -> Self {
Self {}
}
Expand Down
2 changes: 1 addition & 1 deletion src/http/warp_implementation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ pub mod error;
pub mod filter_helpers;
pub mod filters;
pub mod handlers;
pub mod launcher;
pub mod peer_builder;
pub mod request;
pub mod response;
pub mod routes;
pub mod server;

use warp::Rejection;

Expand Down
8 changes: 4 additions & 4 deletions src/jobs/http_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use tokio::sync::oneshot;
use tokio::task::JoinHandle;
use torrust_tracker_configuration::HttpTracker;

use crate::http::axum_implementation::server;
use crate::http::warp_implementation::server::Http;
use crate::http::axum_implementation::launcher;
use crate::http::warp_implementation::launcher::Http;
use crate::http::Version;
use crate::tracker;

Expand Down Expand Up @@ -98,7 +98,7 @@ async fn start_axum(config: &HttpTracker, tracker: Arc<tracker::Tracker>) -> Joi
if !ssl_enabled {
info!("Starting Torrust HTTP tracker server on: http://{}", bind_addr);

let handle = server::start(bind_addr, &tracker);
let handle = launcher::start(bind_addr, tracker);

tx.send(ServerJobStarted())
.expect("the HTTP tracker server should not be dropped");
Expand All @@ -113,7 +113,7 @@ async fn start_axum(config: &HttpTracker, tracker: Arc<tracker::Tracker>) -> Joi
.await
.unwrap();

let handle = server::start_tls(bind_addr, ssl_config, &tracker);
let handle = launcher::start_tls(bind_addr, ssl_config, tracker);

tx.send(ServerJobStarted())
.expect("the HTTP tracker server should not be dropped");
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async fn main() {
let (stats_event_sender, stats_repository) = setup_statistics(config.tracker_usage_statistics);

// Initialize Torrust tracker
let tracker = match tracker::Tracker::new(&config.clone(), stats_event_sender, stats_repository) {
let tracker = match tracker::Tracker::new(config.clone(), stats_event_sender, stats_repository) {
Ok(tracker) => Arc::new(tracker),
Err(error) => {
panic!("{}", error)
Expand Down
29 changes: 7 additions & 22 deletions src/tracker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,17 @@ impl Tracker {
///
/// Will return a `databases::error::Error` if unable to connect to database.
pub fn new(
config: &Arc<Configuration>,
config: Arc<Configuration>,
stats_event_sender: Option<Box<dyn statistics::EventSender>>,
stats_repository: statistics::Repo,
) -> Result<Tracker, databases::error::Error> {
let database = databases::driver::build(&config.db_driver, &config.db_path)?;

let mode = config.mode;

Ok(Tracker {
config: config.clone(),
mode: config.mode,
config,
mode,
keys: RwLock::new(std::collections::HashMap::new()),
whitelist: RwLock::new(std::collections::HashSet::new()),
torrents: RwLock::new(std::collections::BTreeMap::new()),
Expand Down Expand Up @@ -550,17 +552,15 @@ mod tests {

use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::str::FromStr;
use std::sync::Arc;

use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
use torrust_tracker_configuration::Configuration;
use torrust_tracker_primitives::TrackerMode;
use torrust_tracker_test_helpers::configuration::{self};
use torrust_tracker_test_helpers::configuration;

use crate::protocol::clock::DurationSinceUnixEpoch;
use crate::protocol::info_hash::InfoHash;
use crate::tracker::peer::{self, Peer};
use crate::tracker::statistics::Keeper;
use crate::tracker::services::common::tracker_factory;
use crate::tracker::{TorrentsMetrics, Tracker};

pub fn public_tracker() -> Tracker {
Expand All @@ -587,21 +587,6 @@ mod tests {
tracker_factory(configuration)
}

pub fn tracker_factory(configuration: Configuration) -> Tracker {
// code-review: the tracker initialization is duplicated in many places. Consider make this function public.

// Initialize stats tracker
let (stats_event_sender, stats_repository) = Keeper::new_active_instance();

// Initialize Torrust tracker
match Tracker::new(&Arc::new(configuration), Some(stats_event_sender), stats_repository) {
Ok(tracker) => tracker,
Err(error) => {
panic!("{}", error)
}
}
}

fn sample_info_hash() -> InfoHash {
"3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::<InfoHash>().unwrap()
}
Expand Down
2 changes: 1 addition & 1 deletion src/tracker/services/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::tracker::Tracker;
///
/// Will panic if tracker cannot be instantiated.
#[must_use]
pub fn tracker_factory(configuration: &Arc<Configuration>) -> Tracker {
pub fn tracker_factory(configuration: Arc<Configuration>) -> Tracker {
// todo: the tracker initialization is duplicated in many places.

// Initialize stats tracker
Expand Down
2 changes: 1 addition & 1 deletion src/tracker/services/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod tests {

#[tokio::test]
async fn the_statistics_service_should_return_the_tracker_metrics() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let tracker = Arc::new(tracker_factory(tracker_configuration()));

let tracker_metrics = get_metrics(tracker.clone()).await;

Expand Down
14 changes: 7 additions & 7 deletions src/tracker/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ mod tests {

#[tokio::test]
async fn should_return_none_if_the_tracker_does_not_have_the_torrent() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let tracker = Arc::new(tracker_factory(tracker_configuration()));

let torrent_info = get_torrent_info(
tracker.clone(),
Expand All @@ -163,7 +163,7 @@ mod tests {

#[tokio::test]
async fn should_return_the_torrent_info_if_the_tracker_has_the_torrent() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let tracker = Arc::new(tracker_factory(tracker_configuration()));

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();
Expand Down Expand Up @@ -204,7 +204,7 @@ mod tests {

#[tokio::test]
async fn should_return_an_empty_result_if_the_tracker_does_not_have_any_torrent() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let tracker = Arc::new(tracker_factory(tracker_configuration()));

let torrents = get_torrents(tracker.clone(), &Pagination::default()).await;

Expand All @@ -213,7 +213,7 @@ mod tests {

#[tokio::test]
async fn should_return_a_summarized_info_for_all_torrents() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let tracker = Arc::new(tracker_factory(tracker_configuration()));

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();
Expand All @@ -237,7 +237,7 @@ mod tests {

#[tokio::test]
async fn should_allow_limiting_the_number_of_torrents_in_the_result() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let tracker = Arc::new(tracker_factory(tracker_configuration()));

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
Expand All @@ -261,7 +261,7 @@ mod tests {

#[tokio::test]
async fn should_allow_using_pagination_in_the_result() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let tracker = Arc::new(tracker_factory(tracker_configuration()));

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
Expand Down Expand Up @@ -294,7 +294,7 @@ mod tests {

#[tokio::test]
async fn should_return_torrents_ordered_by_info_hash() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let tracker = Arc::new(tracker_factory(tracker_configuration()));

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
Expand Down
Loading

0 comments on commit fac2be8

Please sign in to comment.