Skip to content

Commit

Permalink
refactor: [#932] make all Tracker fields private
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jul 1, 2024
1 parent a5b9e14 commit 5a16ea1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,13 +481,14 @@ use crate::CurrentClock;
/// > Typically, the `Tracker` is used by a higher application service that handles
/// > the network layer.
pub struct Tracker {
// The tracker configuration.
config: Core,
/// A database driver implementation: [`Sqlite3`](crate::core::databases::sqlite)
/// or [`MySQL`](crate::core::databases::mysql)
pub database: Arc<Box<dyn Database>>,
database: Arc<Box<dyn Database>>,
keys: tokio::sync::RwLock<std::collections::HashMap<Key, auth::ExpiringKey>>,
whitelist: tokio::sync::RwLock<std::collections::HashSet<InfoHash>>,
pub torrents: Arc<Torrents>,
torrents: Arc<Torrents>,
stats_event_sender: Option<Box<dyn statistics::EventSender>>,
stats_repository: statistics::Repo,
}
Expand Down Expand Up @@ -987,6 +988,17 @@ impl Tracker {
Some(stats_event_sender) => stats_event_sender.send_event(event).await,
}
}

/// It drops the database tables.
///
/// # Errors
///
/// Will return `Err` if unable to drop tables.
pub fn drop_database_tables(&self) -> Result<(), databases::error::Error> {
// todo: this is only used for testing. WE have to pass the database
// reference directly to the tests instead of via the tracker.
self.database.drop_database_tables()
}
}

#[must_use]
Expand Down
7 changes: 5 additions & 2 deletions tests/servers/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ pub type Started = environment::Environment<server::Running>;

/// It forces a database error by dropping all tables.
/// That makes any query fail.
/// code-review: alternatively we could inject a database mock in the future.
/// code-review:
/// Alternatively we could:
/// - Inject a database mock in the future.
/// - Inject directly the database reference passed to the Tracker type.
pub fn force_database_error(tracker: &Arc<Tracker>) {
tracker.database.drop_database_tables().unwrap();
tracker.drop_database_tables().unwrap();
}

0 comments on commit 5a16ea1

Please sign in to comment.