fidl_fuchsia_update_ext/
commit.rsuse anyhow::anyhow;
use fidl_fuchsia_update::CommitStatusProviderProxy;
use zx::{self as zx, AsHandleRef};
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum CommitStatus {
Pending,
Committed,
}
pub async fn query_commit_status(
provider: &CommitStatusProviderProxy,
) -> Result<CommitStatus, anyhow::Error> {
let event_pair = provider.is_current_system_committed().await?;
match event_pair.wait_handle(zx::Signals::USER_0, zx::MonotonicInstant::INFINITE_PAST) {
Ok(_) => Ok(CommitStatus::Committed),
Err(zx::Status::TIMED_OUT) => Ok(CommitStatus::Pending),
Err(status) => Err(anyhow!("unexpected status while asserting signal: {:?}", status)),
}
}
#[cfg(test)]
mod tests {
use super::*;
use fidl::endpoints::create_proxy_and_stream;
use fidl_fuchsia_update::{CommitStatusProviderMarker, CommitStatusProviderRequest};
use fuchsia_async::{self as fasync};
use futures::StreamExt;
use zx::{HandleBased, Peered};
#[fasync::run_singlethreaded(test)]
async fn test_query_commit_status() {
let (proxy, mut stream) = create_proxy_and_stream::<CommitStatusProviderMarker>();
let (p0, p1) = zx::EventPair::create();
let _fidl_server = fasync::Task::local(async move {
while let Some(Ok(req)) = stream.next().await {
let CommitStatusProviderRequest::IsCurrentSystemCommitted { responder } = req;
let () = responder.send(p1.duplicate_handle(zx::Rights::BASIC).unwrap()).unwrap();
}
});
assert_eq!(query_commit_status(&proxy).await.unwrap(), CommitStatus::Pending);
let () = p0.signal_peer(zx::Signals::NONE, zx::Signals::USER_0).unwrap();
assert_eq!(query_commit_status(&proxy).await.unwrap(), CommitStatus::Committed,);
}
}