1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use anyhow::anyhow;
6use fidl_fuchsia_update::CommitStatusProviderProxy;
7use zx::{self as zx, AsHandleRef};
89/// Whether the current system version is pending commit.
10#[derive(Copy, Clone, Debug, PartialEq)]
11pub enum CommitStatus {
12/// The current system version is pending commit.
13Pending,
14/// The current system version is already committed.
15Committed,
16}
1718/// Queries the commit status using `provider`.
19pub async fn query_commit_status(
20 provider: &CommitStatusProviderProxy,
21) -> Result<CommitStatus, anyhow::Error> {
22let event_pair = provider.is_current_system_committed().await?;
23match event_pair
24 .wait_handle(zx::Signals::USER_0, zx::MonotonicInstant::INFINITE_PAST)
25 .to_result()
26 {
27Ok(_) => Ok(CommitStatus::Committed),
28Err(zx::Status::TIMED_OUT) => Ok(CommitStatus::Pending),
29Err(status) => Err(anyhow!("unexpected status while asserting signal: {:?}", status)),
30 }
31}
3233#[cfg(test)]
34mod tests {
35use super::*;
36use fidl::endpoints::create_proxy_and_stream;
37use fidl_fuchsia_update::{CommitStatusProviderMarker, CommitStatusProviderRequest};
38use fuchsia_async::{self as fasync};
39use futures::StreamExt;
40use zx::{HandleBased, Peered};
4142// Verifies that query_commit_status returns the expected CommitStatus.
43#[fasync::run_singlethreaded(test)]
44async fn test_query_commit_status() {
45let (proxy, mut stream) = create_proxy_and_stream::<CommitStatusProviderMarker>();
46let (p0, p1) = zx::EventPair::create();
4748let _fidl_server = fasync::Task::local(async move {
49while let Some(Ok(req)) = stream.next().await {
50let CommitStatusProviderRequest::IsCurrentSystemCommitted { responder } = req;
51let () = responder.send(p1.duplicate_handle(zx::Rights::BASIC).unwrap()).unwrap();
52 }
53 });
5455// When no signals are asserted, we should report Pending.
56assert_eq!(query_commit_status(&proxy).await.unwrap(), CommitStatus::Pending);
5758// When USER_0 is asserted, we should report Committed.
59let () = p0.signal_peer(zx::Signals::NONE, zx::Signals::USER_0).unwrap();
60assert_eq!(query_commit_status(&proxy).await.unwrap(), CommitStatus::Committed,);
61 }
62}