fn test_rotation_time_files_time()

in src/flowgger/utils/rotating_file.rs [378:434]


    fn test_rotation_time_files_time() -> Result<(), io::Error> {
        // Create static time for the different writes to test to mock the current time during the test
        let ts1 = utc_from_date_time(2015, 8, 6, 11, 15, 24, 637);
        let ts2 = utc_from_date_time(2015, 8, 6, 11, 15, 34, 637);
        let ts3 = utc_from_date_time(2015, 8, 6, 11, 16, 26, 637);
        let ts4 = utc_from_date_time(2015, 8, 6, 11, 21, 28, 637);

        // Build the expected filenames that should be created in the test
        let tmp_dir = TempDir::new("test_rotation_time_files_time")?;
        let file_base = tmp_dir.path().join("test_log.log");
        let file1 = tmp_dir.path().join("test_log-20150806T1115Z.log");
        let file2 = tmp_dir.path().join("test_log-20150806T1116Z.log");
        let file3 = tmp_dir.path().join("test_log-20150806T1121Z.log");

        // Create a list of 6 bytes patterns
        let test_patterns = build_pattern_list(7, 6);

        // Open the rotating file
        let mut rotating_file = RotatingFile::new(&file_base, 16, 5, 10, "%Y%m%dT%H%MZ");
        rotating_file.now_time_mock = ts1;
        assert!(rotating_file.open().is_ok());

        // Write more than the file is allowed in the same minute, no rotation yet
        let _ = &rotating_file.write(test_patterns[0].as_bytes());
        rotating_file.now_time_mock = ts2;
        let _ = &rotating_file.write(test_patterns[1].as_bytes());
        let _ = &rotating_file.write(test_patterns[2].as_bytes());
        assert_eq!(
            fs::read_to_string(file1.as_path()).unwrap(),
            format!("{}{}{}", test_patterns[0], test_patterns[1], test_patterns[2])
        );
        assert!(std::fs::metadata(file2.as_path()).is_err());
        assert!(std::fs::metadata(file3.as_path()).is_err());

        // Write more than the file is allowed in another minute, before rotation time expires,
        // we should have a rotation anyway
        rotating_file.now_time_mock = ts3;
        let _ = rotating_file.write(test_patterns[3].as_bytes());
        assert_eq!(
            fs::read_to_string(file1.as_path()).unwrap(),
            format!("{}{}{}", test_patterns[0], test_patterns[1], test_patterns[2])
        );
        assert_eq!(fs::read_to_string(file2.as_path()).unwrap(), test_patterns[3]);
        assert!(std::fs::metadata(file3.as_path()).is_err());

        // Write after rotation time expire, rotation expected even if the file size is below the max
        rotating_file.now_time_mock = ts4;
        let _ = rotating_file.write(test_patterns[4].as_bytes());
        assert_eq!(
            fs::read_to_string(file1.as_path()).unwrap(),
            format!("{}{}{}", test_patterns[0], test_patterns[1], test_patterns[2])
        );
        assert_eq!(fs::read_to_string(file2.as_path()).unwrap(), test_patterns[3]);
        assert_eq!(fs::read_to_string(file3.as_path()).unwrap(), test_patterns[4]);

        Ok(())
    }