mock_metrics/
lib.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
// Copyright 2022 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 fidl_fuchsia_metrics::{self as fidl, MetricEvent};
use fuchsia_async as fasync;
use fuchsia_sync::Mutex;
use futures::TryStreamExt as _;
use std::sync::Arc;
use std::time::Duration;

pub struct MockMetricEventLogger {
    cobalt_events: Mutex<Vec<MetricEvent>>,
}

impl MockMetricEventLogger {
    fn new() -> Self {
        Self { cobalt_events: Mutex::new(vec![]) }
    }

    pub fn clone_metric_events(&self) -> Vec<MetricEvent> {
        self.cobalt_events.lock().clone()
    }

    async fn run_logger(self: Arc<Self>, mut stream: fidl::MetricEventLoggerRequestStream) {
        while let Some(event) = stream.try_next().await.unwrap() {
            match event {
                fidl::MetricEventLoggerRequest::LogMetricEvents { events, responder } => {
                    self.cobalt_events.lock().extend(events);
                    let _ = responder.send(Ok(()));
                }
                _ => {
                    panic!("unhandled MetricEventLogger method {event:?}");
                }
            }
        }
    }
}

pub struct MockMetricEventLoggerFactory {
    loggers: Mutex<Vec<Arc<MockMetricEventLogger>>>,
    project_id: u32,
}

impl MockMetricEventLoggerFactory {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        Self::with_id(cobalt_sw_delivery_registry::PROJECT_ID)
    }

    pub fn with_id(id: u32) -> Self {
        Self { loggers: Mutex::new(vec![]), project_id: id }
    }

    pub fn clone_loggers(&self) -> Vec<Arc<MockMetricEventLogger>> {
        self.loggers.lock().clone()
    }

    pub async fn run_logger_factory(
        self: Arc<Self>,
        mut stream: fidl::MetricEventLoggerFactoryRequestStream,
    ) {
        while let Some(event) = stream.try_next().await.unwrap() {
            match event {
                fidl::MetricEventLoggerFactoryRequest::CreateMetricEventLogger {
                    project_spec,
                    logger,
                    responder,
                } => {
                    assert_eq!(project_spec.project_id, Some(self.project_id));
                    let mock_logger = Arc::new(MockMetricEventLogger::new());
                    self.loggers.lock().push(mock_logger.clone());
                    fasync::Task::spawn(mock_logger.run_logger(logger.into_stream())).detach();
                    let _ = responder.send(Ok(()));
                }
                _ => {
                    panic!("unhandled MetricEventLoggerFactory method: {event:?}");
                }
            }
        }
    }

    pub async fn wait_for_at_least_n_events_with_metric_id(
        &self,
        n: usize,
        id: u32,
    ) -> Vec<MetricEvent> {
        loop {
            let events: Vec<MetricEvent> = self
                .loggers
                .lock()
                .iter()
                .flat_map(|logger| logger.cobalt_events.lock().clone())
                .filter(|MetricEvent { metric_id, .. }| *metric_id == id)
                .collect();
            if events.len() >= n {
                return events;
            }
            fasync::Timer::new(Duration::from_millis(10)).await;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ::fidl::endpoints::create_proxy_and_stream;
    use fidl_fuchsia_metrics::ProjectSpec;

    #[fasync::run_singlethreaded(test)]
    async fn test_mock_metrics() {
        let mock = Arc::new(MockMetricEventLoggerFactory::new());
        let (factory, stream) = create_proxy_and_stream::<fidl::MetricEventLoggerFactoryMarker>();

        let task = fasync::Task::spawn(Arc::clone(&mock).run_logger_factory(stream));

        let (logger, server_end) = ::fidl::endpoints::create_proxy();
        factory
            .create_metric_event_logger(
                &ProjectSpec {
                    project_id: Some(cobalt_sw_delivery_registry::PROJECT_ID),
                    ..Default::default()
                },
                server_end,
            )
            .await
            .unwrap()
            .unwrap();
        drop(factory);
        task.await;

        let events = &[MetricEvent {
            metric_id: 42,
            event_codes: vec![],
            payload: fidl::MetricEventPayload::Count(0),
        }];
        logger.log_metric_events(events).await.unwrap().unwrap();
        let mock_events = mock.wait_for_at_least_n_events_with_metric_id(1, 42).await;
        assert_eq!(mock_events, events);
    }
}