Skip to content

Commit

Permalink
fix flaky snapshot tests (#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinPostma authored Feb 19, 2024
1 parent 27222a5 commit db5d64c
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions libsql-server/src/replication/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ mod test {

use super::*;

async fn assert_dir_is_empty(p: &Path) {
async fn dir_is_empty(p: &Path) -> bool {
// there is nothing left in the to_compact directory
if p.try_exists().unwrap() {
let mut dir = tokio::fs::read_dir(p).await.unwrap();
Expand All @@ -599,7 +599,9 @@ mod test {
}
}

assert_eq!(count, 0);
count == 0
} else {
true
}
}

Expand Down Expand Up @@ -689,8 +691,7 @@ mod test {

// assert that all indexes are covered
assert_eq!((start_idx, end_idx), (0, 3));

assert_dir_is_empty(&to_compact_path).await;
assert!(dir_is_empty(&to_compact_path).await);
}

/// Simulate an empty pending snapshot left by the logger if the logswapping operation was
Expand All @@ -715,8 +716,8 @@ mod test {
tokio::time::sleep(Duration::from_millis(1000)).await;

// emtpy snapshot was discarded
assert_dir_is_empty(&tmp.path().join("to_compact")).await;
assert_dir_is_empty(&tmp.path().join("snapshots")).await;
assert!(dir_is_empty(&tmp.path().join("to_compact")).await);
assert!(dir_is_empty(&tmp.path().join("snapshots")).await);
}

/// In this test, we send a bunch of snapshot to the compactor, and see if it handles it
Expand Down Expand Up @@ -784,30 +785,33 @@ mod test {
.await
.unwrap();

// wait a bit for snapshot to be compated
tokio::time::sleep(Duration::from_millis(1500)).await;

// no error occured: the loop is still running.
assert!(!compactor.sender.is_closed());
assert!(tmp.path().join("snapshots").exists());
let mut dir = tokio::fs::read_dir(tmp.path().join("snapshots"))
.await
.unwrap();
let mut start_idx = u64::MAX;
let mut end_idx = u64::MIN;
while let Some(entry) = dir.next_entry().await.unwrap() {
if entry.file_type().await.unwrap().is_file() {
let (_, start, end) =
parse_snapshot_name(entry.file_name().to_str().unwrap()).unwrap();
start_idx = start_idx.min(start);
end_idx = end_idx.max(end);
while end_idx != 9 {
let mut dir = tokio::fs::read_dir(tmp.path().join("snapshots"))
.await
.unwrap();
while let Some(entry) = dir.next_entry().await.unwrap() {
if entry.file_type().await.unwrap().is_file() {
let name = entry.file_name();
let name = name.to_str().unwrap();
let (_, start, end) = parse_snapshot_name(name).unwrap();
start_idx = start_idx.min(start);
end_idx = end_idx.max(end);
}
}
tokio::time::sleep(Duration::from_millis(100)).await;
}

// assert that all indexes are covered
assert_eq!((start_idx, end_idx), (0, 9));
assert_eq!(start_idx, 0);

assert_dir_is_empty(&to_compact_path).await;
while !dir_is_empty(&to_compact_path).await {
tokio::time::sleep(Duration::from_millis(100)).await;
}
}

#[tokio::test]
Expand Down

0 comments on commit db5d64c

Please sign in to comment.