I have this test case for a function that allows sorting files by either created or modified date. I create files in sequence, but probably due to OS filesystem caching(?), it's not really creating them on the order I anticipated. This happens on the CI server, but not locally.
I am looking for alternatives to make this test outcome consistent. I thought of introducing a delay, but not quite sure how to even do that, but I think the best idea would be to maybe flush/commit pending writes to disk? Not sure, but open to ideas
Below's the test case in question, and repo.save
is where I actually write the file to disk:
#[test]
fn test_sorting_list() {
let tmpdir = tmpdir("test_list").unwrap();
temp_env::with_var("REPO_ROOT", Some(tmpdir), || {
let repo = Repo::new("user", "1".to_string()).unwrap();
let items = repo.list::<Item>(None).unwrap();
let exp: Vec<String> = vec![];
assert_eq!(items, exp);
for n in (1..=10).rev() {
let item = common::Item {
name: format!("t{}", n),
content: "content".to_string(),
};
repo.save(&item).unwrap();
}
assert_eq!(
repo.list::<Item>(Some(ListSort::new(SortField::Name, SortDirection::Asc)))
.unwrap(),
vec!["t1", "t10", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9",]
);
assert_eq!(
repo.list::<Item>(Some(ListSort::new(SortField::Name, SortDirection::Desc)))
.unwrap(),
vec!["t9", "t8", "t7", "t6", "t5", "t4", "t3", "t2", "t10", "t1",]
);
assert_eq!(
repo.list::<Item>(Some(ListSort::new(SortField::Created, SortDirection::Asc)))
.unwrap(),
vec!["t10", "t9", "t8", "t7", "t6", "t5", "t4", "t3", "t2", "t1",]
);
assert_eq!(
repo.list::<Item>(Some(ListSort::new(SortField::Created, SortDirection::Desc)))
.unwrap(),
vec!["t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9", "t10",]
);
assert_eq!(
repo.list::<Item>(Some(ListSort::new(SortField::Modified, SortDirection::Asc)))
.unwrap(),
vec!["t10", "t9", "t8", "t7", "t6", "t5", "t4", "t3", "t2", "t1",]
);
assert_eq!(
repo.list::<Item>(Some(ListSort::new(
SortField::Modified,
SortDirection::Desc
)))
.unwrap(),
vec!["t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9", "t10",]
);
let item = common::Item {
name: "t4".to_string(),
content: "content".to_string(),
};
repo.save(&item).unwrap();
assert_eq!(
repo.list::<Item>(Some(ListSort::new(SortField::Modified, SortDirection::Asc)))
.unwrap(),
vec!["t10", "t9", "t8", "t7", "t6", "t5", "t3", "t2", "t1", "t4",]
);
assert_eq!(
repo.list::<Item>(Some(ListSort::new(
SortField::Modified,
SortDirection::Desc
)))
.unwrap(),
vec!["t4", "t1", "t2", "t3", "t5", "t6", "t7", "t8", "t9", "t10",]
);
});
}
And here's the failure:
failures:
---- test_sorting_list stdout ----
thread 'test_sorting_list' panicked at 'assertion failed: `(left == right)`
left: `["t10", "t9", "t8", "t7", "t5", "t6", "t4", "t2", "t3", "t1"]`,
right: `["t10", "t9", "t8", "t7", "t6", "t5", "t4", "t3", "t2", "t1"]`', tests/repo.rs:141:9
And here's line 141 where the error occurs:
assert_eq!(
repo.list::<Item>(Some(ListSort::new(SortField::Created, SortDirection::Asc)))
.unwrap(),
vec!["t10", "t9", "t8", "t7", "t6", "t5", "t4", "t3", "t2", "t1",]
);
EDIT: Adding the relevant code for the repo.save
method:
let file_path = self.path_for::<T>()?.join(&file_name);
let file = fs::File::create(&file_path)?;
let mut writer = BufWriter::new(file);
serde_json::to_writer_pretty(&mut writer, &record)?;
writer.flush()?;