test playlist change at mindnight and at six

This commit is contained in:
jb-alvarado 2022-05-10 11:27:33 +02:00
parent 8fcd331c70
commit dda7abfa23
6 changed files with 140 additions and 32 deletions

View File

@ -17,6 +17,7 @@ mod input;
mod macros; mod macros;
mod output; mod output;
mod rpc; mod rpc;
#[cfg(test)]
mod tests; mod tests;
mod utils; mod utils;
@ -65,7 +66,7 @@ fn status_file(stat_file: &str, playout_stat: &PlayoutStatus) {
fn main() { fn main() {
// Init the config, set process controller, create logging. // Init the config, set process controller, create logging.
init_config(); init_config(None);
let config = GlobalConfig::global(); let config = GlobalConfig::global();
let play_control = PlayerControl::new(); let play_control = PlayerControl::new();
let playout_stat = PlayoutStatus::new(); let playout_stat = PlayoutStatus::new();

View File

@ -1,43 +1,78 @@
#[cfg(test)] use std::{
use chrono::prelude::*; process,
thread::{self, sleep},
time::Duration,
};
mod utils;
#[cfg(test)]
use crate::output::player;
#[cfg(test)] #[cfg(test)]
use crate::utils::*; use crate::utils::*;
#[cfg(test)] #[cfg(test)]
fn get_fake_date_time(date_time: &str) -> DateTime<Local> { use simplelog::*;
let date_obj = NaiveDateTime::parse_from_str(date_time, "%Y-%m-%dT%H:%M:%S");
Local.from_local_datetime(&date_obj.unwrap()).unwrap() fn timed_kill(sec: u64, mut proc_ctl: ProcessControl) {
sleep(Duration::from_secs(sec));
proc_ctl.kill_all();
process::exit(0);
} }
#[test] #[test]
fn mock_date_time() { #[ignore]
let fake_time = get_fake_date_time("2022-05-20T06:00:00"); fn playlist_change_at_midnight() {
mock_time::set_mock_time(fake_time); let config = TestConfig {
mode: "playlist".into(),
start: "00:00:00".into(),
length: "24:00:00".into(),
log_to_file: false,
mail_recipient: "".into(),
};
assert_eq!( init_config(Some(config));
fake_time.format("%Y-%m-%dT%H:%M:%S.2f").to_string(),
time_now().format("%Y-%m-%dT%H:%M:%S.2f").to_string() let play_control = PlayerControl::new();
); let playout_stat = PlayoutStatus::new();
let proc_control = ProcessControl::new();
let proc_ctl = proc_control.clone();
let logging = init_logging();
CombinedLogger::init(logging).unwrap();
mock_time::set_mock_time("2022-05-09T23:59:45");
thread::spawn(move || timed_kill(30, proc_ctl));
player(play_control, playout_stat, proc_control);
} }
#[test] #[test]
fn get_date_yesterday() { #[ignore]
let fake_time = get_fake_date_time("2022-05-20T05:59:24"); fn playlist_change_at_six() {
mock_time::set_mock_time(fake_time); let config = TestConfig {
mode: "playlist".into(),
start: "06:00:00".into(),
length: "24:00:00".into(),
log_to_file: false,
mail_recipient: "".into(),
};
let date = get_date(true, 21600.0, 86400.0); init_config(Some(config));
assert_eq!("2022-05-19".to_string(), date); let play_control = PlayerControl::new();
} let playout_stat = PlayoutStatus::new();
let proc_control = ProcessControl::new();
#[test] let proc_ctl = proc_control.clone();
fn get_date_tomorrow() {
let fake_time = get_fake_date_time("2022-05-20T23:59:30"); let logging = init_logging();
mock_time::set_mock_time(fake_time); CombinedLogger::init(logging).unwrap();
let date = get_date(false, 0.0, 86400.01); mock_time::set_mock_time("2022-05-09T05:59:45");
assert_eq!("2022-05-21".to_string(), date); thread::spawn(move || timed_kill(30, proc_ctl));
player(play_control, playout_stat, proc_control);
} }

37
src/tests/utils/mod.rs Normal file
View File

@ -0,0 +1,37 @@
#[cfg(test)]
use chrono::prelude::*;
#[cfg(test)]
use crate::utils::*;
#[test]
fn mock_date_time() {
let time_str = "2022-05-20T06:00:00";
let date_obj = NaiveDateTime::parse_from_str(time_str, "%Y-%m-%dT%H:%M:%S");
let time = Local.from_local_datetime(&date_obj.unwrap()).unwrap();
mock_time::set_mock_time(time_str);
assert_eq!(
time.format("%Y-%m-%dT%H:%M:%S.2f").to_string(),
time_now().format("%Y-%m-%dT%H:%M:%S.2f").to_string()
);
}
#[test]
fn get_date_yesterday() {
mock_time::set_mock_time("2022-05-20T05:59:24");
let date = get_date(true, 21600.0, 86400.0);
assert_eq!("2022-05-19".to_string(), date);
}
#[test]
fn get_date_tomorrow() {
mock_time::set_mock_time("2022-05-20T23:59:30");
let date = get_date(false, 0.0, 86400.01);
assert_eq!("2022-05-21".to_string(), date);
}

View File

@ -55,6 +55,12 @@ pub struct Args {
} }
/// Get arguments from command line, and return them. /// Get arguments from command line, and return them.
#[cfg(not(test))]
pub fn get_args() -> Args { pub fn get_args() -> Args {
Args::parse() Args::parse()
} }
#[cfg(test)]
pub fn get_args() -> Args {
Args::parse_from(["-o desktop"].iter())
}

View File

@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
use serde_yaml::{self}; use serde_yaml::{self};
use shlex::split; use shlex::split;
use crate::utils::{get_args, time_to_sec}; use crate::utils::{get_args, time_to_sec, TestConfig};
use crate::vec_strings; use crate::vec_strings;
/// Global Config /// Global Config
@ -288,7 +288,24 @@ fn pre_audio_codec(add_loudnorm: bool) -> Vec<String> {
codec codec
} }
pub fn init_config() { #[cfg(not(test))]
pub fn init_config(_: Option<TestConfig>) {
let config = GlobalConfig::new(); let config = GlobalConfig::new();
INSTANCE.set(config).unwrap(); INSTANCE.set(config).unwrap();
} }
#[cfg(test)]
pub fn init_config(test_config: Option<TestConfig>) {
let mut config = GlobalConfig::new();
config.out.mode = "desktop".into();
if let Some(cfg) = test_config {
config.logging.log_to_file = cfg.log_to_file;
config.mail.recipient = cfg.mail_recipient;
config.playlist.day_start = cfg.start.clone();
config.playlist.start_sec = Some(time_to_sec(&cfg.start));
config.playlist.length = cfg.length.clone();
config.playlist.length_sec = Some(time_to_sec(&cfg.length));
};
INSTANCE.set(config).unwrap();
}

View File

@ -506,6 +506,15 @@ pub fn validate_ffmpeg() {
} }
} }
/// In test cases we override some configuration values to fit the needs.
pub struct TestConfig {
pub mode: String,
pub start: String,
pub length: String,
pub log_to_file: bool,
pub mail_recipient: String,
}
/// Get system time, in non test case. /// Get system time, in non test case.
#[cfg(not(test))] #[cfg(not(test))]
pub fn time_now() -> DateTime<Local> { pub fn time_now() -> DateTime<Local> {
@ -529,7 +538,10 @@ pub mod mock_time {
}) })
} }
pub fn set_mock_time(time: DateTime<Local>) { pub fn set_mock_time(date_time: &str) {
let date_obj = NaiveDateTime::parse_from_str(date_time, "%Y-%m-%dT%H:%M:%S");
let time = Local.from_local_datetime(&date_obj.unwrap()).unwrap();
DATE_TIME_DIFF.with(|cell| *cell.borrow_mut() = Some(Local::now() - time)); DATE_TIME_DIFF.with(|cell| *cell.borrow_mut() = Some(Local::now() - time));
} }
} }