diff --git a/hammond-data/src/lib.rs b/hammond-data/src/lib.rs index 06481c1..d3039e6 100644 --- a/hammond-data/src/lib.rs +++ b/hammond-data/src/lib.rs @@ -74,27 +74,21 @@ lazy_static! { #[cfg(test)] struct TempDB(tempdir::TempDir, PathBuf, utils::Pool); -#[cfg(test)] -extern crate rand; #[cfg(test)] extern crate tempdir; -#[cfg(test)] -use rand::Rng; #[cfg(test)] /// Create and return a Temporary DB. /// Will be destroed once the returned variable(s) is dropped. fn get_temp_db() -> TempDB { - let mut rng = rand::thread_rng(); - let tmp_dir = tempdir::TempDir::new("hammond_unit_test").unwrap(); - let db_path = tmp_dir - .path() - .join("test.db"); + let db_path = tmp_dir.path().join("test.db"); let pool = utils::init_pool(db_path.to_str().unwrap()); - let db = pool.clone().get().unwrap(); - utils::run_migration_on(&*db).unwrap(); + { + let db = pool.clone().get().unwrap(); + utils::run_migration_on(&*db).unwrap(); + } TempDB(tmp_dir, db_path, pool) } diff --git a/hammond-data/src/utils.rs b/hammond-data/src/utils.rs index cb0acc3..c54cdfd 100644 --- a/hammond-data/src/utils.rs +++ b/hammond-data/src/utils.rs @@ -1,5 +1,4 @@ use rayon::prelude::*; -use diesel::prelude::*; use chrono::prelude::*; use r2d2; @@ -12,24 +11,28 @@ use models::Episode; use std::path::Path; use std::fs; +use std::sync::Arc; +use std::time::Duration; -use DB_PATH; +use POOL; embed_migrations!("migrations/"); -pub type Pool = r2d2::Pool>; +pub type Pool = Arc>>; pub fn init() -> Result<()> { - let conn = establish_connection(); - run_migration_on(&conn) + let con = POOL.clone().get().unwrap(); + run_migration_on(&*con) } pub fn init_pool(db_path: &str) -> Pool { - let config = r2d2::Config::default(); + let config = r2d2::Config::builder() + .connection_timeout(Duration::from_secs(60)) + .build(); let manager = ConnectionManager::::new(db_path); let pool = r2d2::Pool::new(config, manager).expect("Failed to create pool."); info!("Database pool initialized."); - pool + Arc::new(pool) } pub fn run_migration_on(connection: &SqliteConnection) -> Result<()> { @@ -39,12 +42,6 @@ pub fn run_migration_on(connection: &SqliteConnection) -> Result<()> { Ok(()) } -pub fn establish_connection() -> SqliteConnection { - let database_url = DB_PATH.to_str().unwrap(); - SqliteConnection::establish(database_url) - .expect(&format!("Error connecting to {}", database_url)) -} - // TODO: Write unit test. fn download_checker() -> Result<()> { let episodes = dbqueries::get_downloaded_episodes()?; diff --git a/hammond-gtk/src/utils.rs b/hammond-gtk/src/utils.rs index 54d99af..62d93ee 100644 --- a/hammond-gtk/src/utils.rs +++ b/hammond-gtk/src/utils.rs @@ -19,11 +19,7 @@ thread_local!(static GLOBAL: Foo = RefCell::new(None)); /// If `source` is None, Fetches all the `Source` entries in the database and updates them. /// `delay` represents the desired time in seconds for the thread to sleep before executing. /// When It's done,it queues up a `podcast_view` refresh. -pub fn refresh_feed( - stack: >k::Stack, - source: Option>, - delay: Option, -) { +pub fn refresh_feed(stack: >k::Stack, source: Option>, delay: Option) { // Create a async channel. let (sender, receiver) = channel(); @@ -33,7 +29,7 @@ pub fn refresh_feed( })); thread::spawn(move || { - if let Some(s) = delay{ + if let Some(s) = delay { let t = time::Duration::from_secs(s); thread::sleep(t); } diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index 75baaca..8cff449 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -68,23 +68,19 @@ fn epidose_widget(episode: &mut Episode, pd_title: &str) -> gtk::Box { delete_button.show(); } - play_button.connect_clicked( - clone!(episode, played_button, unplayed_button => move |_| { + play_button.connect_clicked(clone!(episode, played_button, unplayed_button => move |_| { on_play_bttn_clicked(*episode.id()); let _ = set_played_now(&mut episode.clone()); played_button.hide(); unplayed_button.show(); - }), - ); + })); - delete_button.connect_clicked( - clone!(episode, play_button, download_button => move |del| { + delete_button.connect_clicked(clone!(episode, play_button, download_button => move |del| { on_delete_bttn_clicked(*episode.id()); del.hide(); play_button.hide(); download_button.show(); - }), - ); + })); played_button.connect_clicked(clone!(episode, unplayed_button => move |played| { let _ = set_played_now(&mut episode.clone()); @@ -101,8 +97,7 @@ fn epidose_widget(episode: &mut Episode, pd_title: &str) -> gtk::Box { })); let pd_title = pd_title.to_owned(); - download_button.connect_clicked( - clone!(play_button, delete_button, episode => move |dl| { + download_button.connect_clicked(clone!(play_button, delete_button, episode => move |dl| { on_download_clicked( &pd_title, &mut episode.clone(), @@ -110,8 +105,7 @@ fn epidose_widget(episode: &mut Episode, pd_title: &str) -> gtk::Box { &play_button, &delete_button, ); - }), - ); + })); ep } diff --git a/hammond-gtk/src/widgets/podcast.rs b/hammond-gtk/src/widgets/podcast.rs index 7801b25..296a49b 100644 --- a/hammond-gtk/src/widgets/podcast.rs +++ b/hammond-gtk/src/widgets/podcast.rs @@ -53,11 +53,7 @@ pub fn podcast_widget(stack: >k::Stack, pd: &Podcast) -> gtk::Box { pd_widget } -fn on_unsub_button_clicked( - stack: >k::Stack, - pd: &Podcast, - unsub_button: >k::Button, -) { +fn on_unsub_button_clicked(stack: >k::Stack, pd: &Podcast, unsub_button: >k::Button) { let res = dbqueries::remove_feed(pd); if res.is_ok() { info!("{} was removed succesfully.", pd.title());