detect/
test_invoker.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::{DetectionOpts, RunsDetection};
use fidl_fuchsia_diagnostics_test::{
    DetectControllerRequest, DetectControllerRequestStream, TestCaseControllerRequest,
    TestCaseControllerRequestStream,
};
use futures::lock::Mutex;
use futures::StreamExt;
use std::sync::Arc;

/// This file serves the fuchsia.diagnostics.test.DetectController protocol,
/// which is used for performance testing.
/// This file is unrelated to lib.rs::Mode::Testing which is used for integration testing.
pub(crate) async fn run_test_service(
    mut stream: DetectControllerRequestStream,
    detection_runner: Arc<Mutex<impl RunsDetection>>,
) {
    while let Some(Ok(request)) = stream.next().await {
        match request {
            DetectControllerRequest::EnterTestMode { test_controller, responder } => {
                let mut detection_runner = detection_runner.lock().await;
                let _: Result<_, _> = responder.send();
                run_test_case_service(test_controller.into_stream(), &mut *detection_runner).await;
            }
        }
    }
}

async fn run_test_case_service(
    mut stream: TestCaseControllerRequestStream,
    detection_runner: &mut impl RunsDetection,
) {
    while let Some(Ok(request)) = stream.next().await {
        let TestCaseControllerRequest::RunDefaultCycle { responder } = request;
        detection_runner.run_detection(DetectionOpts { cpu_test: true }).await;
        let _: Result<_, _> = responder.send();
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::Stats;
    use fidl_fuchsia_diagnostics_test::{
        DetectControllerMarker, DetectControllerProxy, TestCaseControllerMarker,
    };
    use fuchsia_async::{Scope, TestExecutor};
    use futures::channel::mpsc::{self, UnboundedReceiver, UnboundedSender};

    struct TestDetectionRunner {
        run_sender: UnboundedSender<()>,
        stats: Stats,
    }

    impl TestDetectionRunner {
        fn create() -> (Arc<Mutex<Self>>, UnboundedReceiver<()>) {
            let (run_sender, run_receiver) = mpsc::unbounded();
            (Arc::new(Mutex::new(Self { run_sender, stats: Stats::default() })), run_receiver)
        }
    }

    impl RunsDetection for TestDetectionRunner {
        async fn run_detection(&mut self, _opts: DetectionOpts) {
            self.run_sender.unbounded_send(()).unwrap();
        }

        fn stats(&self) -> &Stats {
            &self.stats
        }
    }

    trait CountsMessages {
        async fn expect_n_messages(&mut self, n: usize);
    }

    impl CountsMessages for UnboundedReceiver<()> {
        async fn expect_n_messages(&mut self, n: usize) {
            for _ in 0..n {
                assert_eq!(self.next().await, Some(()));
            }
            assert!(self.try_next().is_err());
        }
    }

    fn get_controller_proxy(
        scope: &Scope,
        detection_runner: Arc<Mutex<impl RunsDetection + Send + 'static>>,
    ) -> DetectControllerProxy {
        let (client_end, server_end) =
            fidl::endpoints::create_endpoints::<DetectControllerMarker>();
        scope.spawn(run_test_service(server_end.into_stream(), detection_runner));
        client_end.into_proxy()
    }

    /// Make sure that the FIDL server doesn't hang or crash, and does call run_detection
    /// when it should.
    #[fuchsia::test(allow_stalls = false)]
    async fn exercise_server() {
        let (detection_runner, mut run_receiver) = TestDetectionRunner::create();
        let scope = Scope::new();
        let controller_proxy = get_controller_proxy(&scope, detection_runner);
        // Create the tearoff for a test-mode session
        let (test_case_proxy, first_test_case_server_end) =
            fidl::endpoints::create_proxy::<TestCaseControllerMarker>();
        controller_proxy.enter_test_mode(first_test_case_server_end).await.unwrap();
        run_receiver.expect_n_messages(0).await;
        test_case_proxy.run_default_cycle().await.unwrap();
        run_receiver.expect_n_messages(1).await;
        test_case_proxy.run_default_cycle().await.unwrap();
        run_receiver.expect_n_messages(1).await;
        std::mem::drop(test_case_proxy);
        std::mem::drop(controller_proxy);
        scope.await;
    }

    /// Make sure that the program doesn't run a test in a second session while
    /// a first session is still open.
    #[fuchsia::test(allow_stalls = false)]
    async fn ensure_non_overlapping_sessions() {
        let (detection_runner, mut run_receiver) = TestDetectionRunner::create();
        let scope = Scope::new();
        let controller_proxy = get_controller_proxy(&scope, detection_runner);
        // Create the tearoff for a test-mode session. This test should demonstrate correct behavior
        // even though we don't run a cycle on this proxy.
        let (first_test_case_proxy, first_test_case_server_end) =
            fidl::endpoints::create_proxy::<TestCaseControllerMarker>();
        controller_proxy.enter_test_mode(first_test_case_server_end).await.unwrap();
        let controller_proxy_clone = controller_proxy.clone();
        // The second test-mode session should run, but only after the first is done.
        scope.spawn(async move {
            let (second_test_case_proxy, second_test_case_server_end) =
                fidl::endpoints::create_proxy::<TestCaseControllerMarker>();
            controller_proxy_clone.enter_test_mode(second_test_case_server_end).await.unwrap();
            second_test_case_proxy.run_default_cycle().await.unwrap();
        });
        // Test the test-mode-lockout logic by giving the spawned second_test_case code an
        // opportunity to run as far as it can. It shouldn't run its test cycle yet.
        let _ = TestExecutor::poll_until_stalled(std::future::pending::<()>()).await;
        // Yup, no tests ran yet - right?
        run_receiver.expect_n_messages(0).await;
        // End the first session. Now the second session should run.
        std::mem::drop(first_test_case_proxy);
        run_receiver.expect_n_messages(1).await;
        std::mem::drop(controller_proxy);
        scope.await;
    }

    /// Make sure the program doesn't run a test in a second session while a first session is open,
    /// even if the sessions are on different connections.
    #[fuchsia::test(allow_stalls = false)]
    async fn ensure_non_overlapping_connections() {
        let (detection_runner, mut run_receiver) = TestDetectionRunner::create();
        let scope = Scope::new();
        let controller_proxy = get_controller_proxy(&scope, detection_runner.clone());
        // Create the tearoff for a test-mode session. This test should demonstrate correct behavior
        // even though we don't run a cycle on this proxy.
        let (first_test_case_proxy, first_test_case_server_end) =
            fidl::endpoints::create_proxy::<TestCaseControllerMarker>();
        // We've had controller proxy. What about second controller proxy?
        let second_controller_proxy = get_controller_proxy(&scope, detection_runner);
        // Second controller connected, but it's not in test mode, so first controller should work
        // normally.
        controller_proxy.enter_test_mode(first_test_case_server_end).await.unwrap();
        run_receiver.expect_n_messages(0).await;
        first_test_case_proxy.run_default_cycle().await.unwrap();
        run_receiver.expect_n_messages(1).await;
        // Now we'll try to run a test cycle on second controller, while first controller is still
        // in test mode.
        scope.spawn(async move {
            let (second_test_case_proxy, second_test_case_server_end) =
                fidl::endpoints::create_proxy::<TestCaseControllerMarker>();
            second_controller_proxy.enter_test_mode(second_test_case_server_end).await.unwrap();
            second_test_case_proxy.run_default_cycle().await.unwrap();
        });
        // Test the test-mode-lockout logic by giving the spawned second_test_case code an
        // opportunity to run as far as it can. It shouldn't run its test cycle yet.
        let _ = TestExecutor::poll_until_stalled(std::future::pending::<()>()).await;
        // The first controller is still in test mode. The second was able to connect,
        // but can't enter test mode to run its test cycle. (It'll be waiting for a
        // response to enter_test_mode().)
        run_receiver.expect_n_messages(0).await;
        // End the first session. Now the second session (on the second controller) should run.
        std::mem::drop(first_test_case_proxy);
        run_receiver.expect_n_messages(1).await;
        // Drop the controller_proxy client end of the FIDL server
        // (second_controller_proxy was dropped in the spawned block)
        std::mem::drop(controller_proxy);
        scope.await;
    }
}