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.wait_handle(zx::Signals::USER_0, zx::MonotonicInstant::INFINITE_PAST) {
24Ok(_) => Ok(CommitStatus::Committed),
25Err(zx::Status::TIMED_OUT) => Ok(CommitStatus::Pending),
26Err(status) => Err(anyhow!("unexpected status while asserting signal: {:?}", status)),
27 }
28}
2930#[cfg(test)]
31mod tests {
32use super::*;
33use fidl::endpoints::create_proxy_and_stream;
34use fidl_fuchsia_update::{CommitStatusProviderMarker, CommitStatusProviderRequest};
35use fuchsia_async::{self as fasync};
36use futures::StreamExt;
37use zx::{HandleBased, Peered};
3839// Verifies that query_commit_status returns the expected CommitStatus.
40#[fasync::run_singlethreaded(test)]
41async fn test_query_commit_status() {
42let (proxy, mut stream) = create_proxy_and_stream::<CommitStatusProviderMarker>();
43let (p0, p1) = zx::EventPair::create();
4445let _fidl_server = fasync::Task::local(async move {
46while let Some(Ok(req)) = stream.next().await {
47let CommitStatusProviderRequest::IsCurrentSystemCommitted { responder } = req;
48let () = responder.send(p1.duplicate_handle(zx::Rights::BASIC).unwrap()).unwrap();
49 }
50 });
5152// When no signals are asserted, we should report Pending.
53assert_eq!(query_commit_status(&proxy).await.unwrap(), CommitStatus::Pending);
5455// When USER_0 is asserted, we should report Committed.
56let () = p0.signal_peer(zx::Signals::NONE, zx::Signals::USER_0).unwrap();
57assert_eq!(query_commit_status(&proxy).await.unwrap(), CommitStatus::Committed,);
58 }
59}