Skip to content

Commit 37e9648

Browse files
committed
Feature: add Wait::purged() to wait for purged to become the expected
1 parent 31a181f commit 37e9648

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

openraft/src/metrics/wait.rs

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use tokio::sync::watch;
55
use tokio::time::Instant;
66

77
use crate::core::ServerState;
8+
use crate::display_ext::DisplayOption;
89
use crate::metrics::RaftMetrics;
910
use crate::node::Node;
1011
use crate::LogId;
@@ -190,4 +191,14 @@ where
190191
)
191192
.await
192193
}
194+
195+
/// Wait for `purged` to become `want` or timeout.
196+
#[tracing::instrument(level = "trace", skip(self), fields(msg=msg.to_string().as_str()))]
197+
pub async fn purged(&self, want: Option<LogId<NID>>, msg: impl ToString) -> Result<RaftMetrics<NID, N>, WaitError> {
198+
self.metrics(
199+
|x| x.purged == want,
200+
&format!("{} .purged -> {}", msg.to_string(), DisplayOption(&want)),
201+
)
202+
.await
203+
}
193204
}

openraft/src/metrics/wait_test.rs

+19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::core::ServerState;
1010
use crate::log_id::LogIdOptionExt;
1111
use crate::metrics::Wait;
1212
use crate::metrics::WaitError;
13+
use crate::testing::log_id;
1314
use crate::vote::CommittedLeaderId;
1415
use crate::LogId;
1516
use crate::Membership;
@@ -170,6 +171,24 @@ async fn test_wait() -> anyhow::Result<()> {
170171
Ok(())
171172
}
172173

174+
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
175+
async fn test_wait_purged() -> anyhow::Result<()> {
176+
let (init, w, tx) = init_wait_test::<u64, ()>();
177+
178+
let h = tokio::spawn(async move {
179+
sleep(Duration::from_millis(10)).await;
180+
let mut update = init.clone();
181+
update.purged = Some(log_id(1, 2, 3));
182+
let rst = tx.send(update);
183+
assert!(rst.is_ok());
184+
});
185+
let got = w.purged(Some(log_id(1, 2, 3)), "purged").await?;
186+
h.await?;
187+
assert_eq!(Some(log_id(1, 2, 3)), got.purged);
188+
189+
Ok(())
190+
}
191+
173192
pub(crate) type InitResult<NID, N> = (RaftMetrics<NID, N>, Wait<NID, N>, watch::Sender<RaftMetrics<NID, N>>);
174193

175194
/// Build a initial state for testing of Wait:

0 commit comments

Comments
 (0)