Skip to content

Commit

Permalink
Chore: allow asynchronous purge for test
Browse files Browse the repository at this point in the history
A storage implementation may choose to `purge()` logs in background.
The test should wait a short while for the purge to complete.
  • Loading branch information
drmingdrmer committed Jan 31, 2024
1 parent 3ed4228 commit 84bbde0
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions openraft/src/testing/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::Debug;
use std::future::Future;
use std::marker::PhantomData;
use std::option::Option::None;
use std::time::Duration;

use anyerror::AnyError;
use maplit::btreeset;
Expand Down Expand Up @@ -668,6 +669,10 @@ where

store.purge(LogId::new(CommittedLeaderId::new(0, C::NodeId::default()), 0)).await?;

// `purge()` does not have to do the purge at once.
// The implementation may choose to do it in the background.
tokio::time::sleep(Duration::from_millis(1_000)).await;

let ent = store.try_get_log_entry(3).await?;
assert_eq!(Some(log_id_0(1, 3)), ent.map(|x| *x.get_log_id()));

Expand Down Expand Up @@ -732,6 +737,10 @@ where
{
store.purge(log_id_0(2, 3)).await?;

// `purge()` does not have to do the purge at once.
// The implementation may choose to do it in the background.
tokio::time::sleep(Duration::from_millis(1_000)).await;

let st = store.get_log_state().await?;
assert_eq!(Some(log_id_0(2, 3)), st.last_purged_log_id);
assert_eq!(Some(log_id_0(2, 3)), st.last_log_id);
Expand All @@ -745,6 +754,10 @@ where

store.purge(log_id_0(1, 3)).await?;

// `purge()` does not have to do the purge at once.
// The implementation may choose to do it in the background.
tokio::time::sleep(Duration::from_millis(1_000)).await;

let res = store.get_log_id(0).await;
assert!(res.is_err());

Expand Down Expand Up @@ -791,6 +804,10 @@ where
{
store.purge(log_id_0(1, 2)).await?;

// `purge()` does not have to do the purge at once.
// The implementation may choose to do it in the background.
tokio::time::sleep(Duration::from_millis(1_000)).await;

let last_log_id = store.get_log_state().await?.last_log_id;
assert_eq!(Some(log_id_0(1, 2)), last_log_id);
}
Expand Down Expand Up @@ -837,6 +854,10 @@ where

store.purge(log_id_0(0, 0)).await?;

// `purge()` does not have to do the purge at once.
// The implementation may choose to do it in the background.
tokio::time::sleep(Duration::from_millis(1_000)).await;

let logs = store.try_get_log_entries(0..100).await?;
assert_eq!(logs.len(), 10);
assert_eq!(logs[0].get_log_id().index, 1);
Expand All @@ -858,6 +879,10 @@ where

store.purge(log_id_0(1, 5)).await?;

// `purge()` does not have to do the purge at once.
// The implementation may choose to do it in the background.
tokio::time::sleep(Duration::from_millis(1_000)).await;

let logs = store.try_get_log_entries(0..100).await?;
assert_eq!(logs.len(), 5);
assert_eq!(logs[0].get_log_id().index, 6);
Expand All @@ -879,6 +904,10 @@ where

store.purge(log_id_0(1, 20)).await?;

// `purge()` does not have to do the purge at once.
// The implementation may choose to do it in the background.
tokio::time::sleep(Duration::from_millis(1_000)).await;

let logs = store.try_get_log_entries(0..100).await?;
assert_eq!(logs.len(), 0);

Expand Down Expand Up @@ -938,6 +967,10 @@ where

store.purge(log_id_0(0, 0)).await?;

// `purge()` does not have to do the purge at once.
// The implementation may choose to do it in the background.
tokio::time::sleep(Duration::from_millis(1_000)).await;

append(&mut store, [blank_ent_0::<C>(2, 11)]).await?;

let l = store.try_get_log_entries(0..).await?.len();
Expand Down

0 comments on commit 84bbde0

Please sign in to comment.