Make tests not care about the machine's timezone

This commit is contained in:
Scott Lamb 2016-11-30 11:17:46 -08:00
parent b15ec58865
commit 59051f960d
4 changed files with 26 additions and 17 deletions

View File

@ -1151,7 +1151,7 @@ mod tests {
#[test] #[test]
fn test_adjust_days() { fn test_adjust_days() {
testutil::init_logging(); testutil::init();
let mut m = BTreeMap::new(); let mut m = BTreeMap::new();
// Create a day. // Create a day.
@ -1206,7 +1206,7 @@ mod tests {
/// Basic test of running some queries on an empty database. /// Basic test of running some queries on an empty database.
#[test] #[test]
fn test_empty_db() { fn test_empty_db() {
testutil::init_logging(); testutil::init();
let conn = setup_conn(); let conn = setup_conn();
let db = Database::new(conn).unwrap(); let db = Database::new(conn).unwrap();
let db = db.lock(); let db = db.lock();
@ -1216,7 +1216,7 @@ mod tests {
/// Basic test of the full lifecycle of recording. Does not exercise error cases. /// Basic test of the full lifecycle of recording. Does not exercise error cases.
#[test] #[test]
fn test_full_lifecycle() { fn test_full_lifecycle() {
testutil::init_logging(); testutil::init();
let conn = setup_conn(); let conn = setup_conn();
let camera_uuid = Uuid::new_v4(); let camera_uuid = Uuid::new_v4();
let camera_id = setup_camera(&conn, camera_uuid, "testcam"); let camera_id = setup_camera(&conn, camera_uuid, "testcam");
@ -1289,7 +1289,7 @@ mod tests {
#[test] #[test]
fn test_drop_tx() { fn test_drop_tx() {
testutil::init_logging(); testutil::init();
let conn = setup_conn(); let conn = setup_conn();
let db = Database::new(conn).unwrap(); let db = Database::new(conn).unwrap();
let mut db = db.lock(); let mut db = db.lock();

View File

@ -1342,7 +1342,7 @@ mod tests {
#[test] #[test]
fn test_round_trip() { fn test_round_trip() {
testutil::init_logging(); testutil::init();
let db = setup_db(); let db = setup_db();
copy_mp4_to_db(&db); copy_mp4_to_db(&db);
let mp4 = create_mp4_from_db(db.db.clone(), db.dir.clone(), 0, 0, false); let mp4 = create_mp4_from_db(db.db.clone(), db.dir.clone(), 0, 0, false);
@ -1362,7 +1362,7 @@ mod tests {
#[test] #[test]
fn test_round_trip_with_subtitles() { fn test_round_trip_with_subtitles() {
testutil::init_logging(); testutil::init();
let db = setup_db(); let db = setup_db();
copy_mp4_to_db(&db); copy_mp4_to_db(&db);
let mp4 = create_mp4_from_db(db.db.clone(), db.dir.clone(), 0, 0, true); let mp4 = create_mp4_from_db(db.db.clone(), db.dir.clone(), 0, 0, true);
@ -1382,7 +1382,7 @@ mod tests {
#[test] #[test]
fn test_round_trip_with_edit_list() { fn test_round_trip_with_edit_list() {
testutil::init_logging(); testutil::init();
let db = setup_db(); let db = setup_db();
copy_mp4_to_db(&db); copy_mp4_to_db(&db);
let mp4 = create_mp4_from_db(db.db.clone(), db.dir.clone(), 1, 0, false); let mp4 = create_mp4_from_db(db.db.clone(), db.dir.clone(), 1, 0, false);
@ -1402,7 +1402,7 @@ mod tests {
#[test] #[test]
fn test_round_trip_with_shorten() { fn test_round_trip_with_shorten() {
testutil::init_logging(); testutil::init();
let db = setup_db(); let db = setup_db();
copy_mp4_to_db(&db); copy_mp4_to_db(&db);
let mp4 = create_mp4_from_db(db.db.clone(), db.dir.clone(), 0, 1, false); let mp4 = create_mp4_from_db(db.db.clone(), db.dir.clone(), 0, 1, false);
@ -1478,7 +1478,7 @@ mod tests {
/// Benchmarks serving the generated part of a `.mp4` file (up to the first byte from disk). /// Benchmarks serving the generated part of a `.mp4` file (up to the first byte from disk).
#[bench] #[bench]
fn serve_generated_bytes_fresh_client(b: &mut Bencher) { fn serve_generated_bytes_fresh_client(b: &mut Bencher) {
testutil::init_logging(); testutil::init();
let server = &*SERVER; let server = &*SERVER;
let p = server.generated_len; let p = server.generated_len;
let mut buf = Vec::with_capacity(p as usize); let mut buf = Vec::with_capacity(p as usize);
@ -1503,7 +1503,7 @@ mod tests {
/// algorithm. /// algorithm.
#[bench] #[bench]
fn serve_generated_bytes_reuse_client(b: &mut Bencher) { fn serve_generated_bytes_reuse_client(b: &mut Bencher) {
testutil::init_logging(); testutil::init();
let server = &*SERVER; let server = &*SERVER;
let p = server.generated_len; let p = server.generated_len;
let mut buf = Vec::with_capacity(p as usize); let mut buf = Vec::with_capacity(p as usize);
@ -1524,7 +1524,7 @@ mod tests {
#[bench] #[bench]
fn mp4_construction(b: &mut Bencher) { fn mp4_construction(b: &mut Bencher) {
testutil::init_logging(); testutil::init();
let db = setup_db(); let db = setup_db();
add_dummy_recordings_to_db(&db.db); add_dummy_recordings_to_db(&db.db);
b.iter(|| { b.iter(|| {

View File

@ -418,7 +418,7 @@ mod tests {
#[test] #[test]
fn serve_without_etag() { fn serve_without_etag() {
testutil::init_logging(); testutil::init();
*RESOURCE.lock().unwrap() = Some(FakeResource{ *RESOURCE.lock().unwrap() = Some(FakeResource{
etag: None, etag: None,
mime: mime!(Application/OctetStream), mime: mime!(Application/OctetStream),
@ -561,7 +561,7 @@ mod tests {
#[test] #[test]
fn serve_with_strong_etag() { fn serve_with_strong_etag() {
testutil::init_logging(); testutil::init();
*RESOURCE.lock().unwrap() = Some(FakeResource{ *RESOURCE.lock().unwrap() = Some(FakeResource{
etag: Some(EntityTag::strong("foo".to_owned())), etag: Some(EntityTag::strong("foo".to_owned())),
mime: mime!(Application/OctetStream), mime: mime!(Application/OctetStream),
@ -630,7 +630,7 @@ mod tests {
#[test] #[test]
fn serve_with_weak_etag() { fn serve_with_weak_etag() {
testutil::init_logging(); testutil::init();
*RESOURCE.lock().unwrap() = Some(FakeResource{ *RESOURCE.lock().unwrap() = Some(FakeResource{
etag: Some(EntityTag::weak("foo".to_owned())), etag: Some(EntityTag::weak("foo".to_owned())),
mime: mime!(Application/OctetStream), mime: mime!(Application/OctetStream),

View File

@ -28,18 +28,27 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
use std::env;
use std::sync; use std::sync;
use slog::{self, DrainExt}; use slog::{self, DrainExt};
use slog_envlogger; use slog_envlogger;
use slog_stdlog; use slog_stdlog;
use slog_term; use slog_term;
use time;
static INIT_LOGGING: sync::Once = sync::ONCE_INIT; static INIT: sync::Once = sync::ONCE_INIT;
pub fn init_logging() { /// Performs global initialization for tests.
INIT_LOGGING.call_once(|| { /// * set up logging. (Note the output can be confusing unless `RUST_TEST_THREADS=1` is set in
/// the program's environment prior to running.)
/// * set `TZ=America/Los_Angeles` so that tests that care about calendar time get the expected
/// results regardless of machine setup.)
pub fn init() {
INIT.call_once(|| {
let drain = slog_term::StreamerBuilder::new().async().full().build(); let drain = slog_term::StreamerBuilder::new().async().full().build();
let drain = slog_envlogger::new(drain); let drain = slog_envlogger::new(drain);
slog_stdlog::set_logger(slog::Logger::root(drain.ignore_err(), None)).unwrap(); slog_stdlog::set_logger(slog::Logger::root(drain.ignore_err(), None)).unwrap();
env::set_var("TZ", "America/Los_Angeles");
time::tzset();
}); });
} }