mock_health_verification/
lib.rs1#![allow(unused_crate_dependencies)]
2use fidl_fuchsia_update_verify as fidl;
7use fuchsia_async::Task;
8use futures::{FutureExt as _, StreamExt as _, future};
9use std::sync::Arc;
10
11pub trait Hook: Send + Sync {
12 fn query_health_checks(&self) -> future::BoxFuture<'static, Result<(), zx::Status>>;
13}
14
15impl<F> Hook for F
16where
17 F: Fn() -> Result<(), zx::Status> + Send + Sync,
18{
19 fn query_health_checks(&self) -> future::BoxFuture<'static, Result<(), zx::Status>> {
20 future::ready(self()).boxed()
21 }
22}
23
24pub struct MockHealthVerificationService {
25 call_hook: Box<dyn Hook>,
26}
27
28impl MockHealthVerificationService {
29 pub fn new(hook: impl Hook + 'static) -> Self {
31 Self { call_hook: Box::new(hook) }
32 }
33
34 pub fn spawn_health_verification_service(
35 self: Arc<Self>,
36 ) -> (fidl::HealthVerificationProxy, Task<()>) {
37 let (proxy, stream) =
38 ::fidl::endpoints::create_proxy_and_stream::<fidl::HealthVerificationMarker>();
39
40 let task = Task::spawn(self.run_health_verification_service(stream));
41
42 (proxy, task)
43 }
44
45 pub async fn run_health_verification_service(
47 self: Arc<Self>,
48 stream: fidl::HealthVerificationRequestStream,
49 ) {
50 let Self { call_hook } = &*self;
51 stream
52 .for_each(|request| match request.expect("received verifier request") {
53 fidl::HealthVerificationRequest::QueryHealthChecks { responder } => {
54 call_hook.query_health_checks().map(|res| {
55 responder
56 .send(zx::Status::result_into_raw(res))
57 .expect("sent verifier response")
58 })
59 }
60 })
61 .await
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[fuchsia::test]
70 async fn test_mock_verifier() {
71 let mock = Arc::new(MockHealthVerificationService::new(|| Ok(())));
72 let (proxy, _server) = mock.spawn_health_verification_service();
73
74 let verify_result = proxy.query_health_checks().await.expect("made fidl call");
75
76 assert_eq!(verify_result, zx::sys::ZX_OK);
77 }
78}