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, zx::Status>;
13}
14
15impl<F> Hook for F
16where
17 F: Fn() -> zx::Status + Send + Sync,
18{
19 fn query_health_checks(&self) -> future::BoxFuture<'static, 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 } => call_hook
54 .query_health_checks()
55 .map(|res| responder.send(res.into_raw()).expect("sent verifier response")),
56 })
57 .await
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[fuchsia::test]
66 async fn test_mock_verifier() {
67 let mock = Arc::new(MockHealthVerificationService::new(|| zx::Status::OK));
68 let (proxy, _server) = mock.spawn_health_verification_service();
69
70 let verify_result = proxy.query_health_checks().await.expect("made fidl call");
71
72 assert_eq!(verify_result, 0);
73 }
74}