memfault_ssf/
service_jig.rs

1//
2// Copyright (c) Memfault, Inc.
3// See License.txt for details
4use std::sync::mpsc::Receiver;
5
6use crate::{Envelope, Mailbox, Service};
7
8/// The ServiceJig allows you to create a mailbox for a service and control when
9/// messages will be processed. It does not use thread and is specifically well
10/// suited for unit tests.
11pub struct ServiceJig<S: Service> {
12    pub mailbox: Mailbox<S>,
13    service: S,
14    receiver: Receiver<Envelope<S>>,
15}
16
17impl<S: Service> ServiceJig<S> {
18    pub fn prepare(service: S) -> Self {
19        let (mailbox, receiver) = Mailbox::create();
20
21        ServiceJig {
22            service,
23            receiver,
24            mailbox,
25        }
26    }
27
28    /// Process all waiting messages
29    pub fn process_all(&mut self) {
30        let iter = self.receiver.try_iter();
31        for mut m in iter {
32            let _x = m.deliver_to(&mut self.service);
33        }
34    }
35
36    pub fn get_service(&self) -> &S {
37        &self.service
38    }
39
40    pub fn get_service_mut(&mut self) -> &mut S {
41        &mut self.service
42    }
43}