Skip to main content

nexo_core/agent/
mock_plugin.rs

1use super::plugin::{Command, Plugin, Response};
2use async_trait::async_trait;
3use nexo_broker::AnyBroker;
4use std::sync::{
5    atomic::{AtomicBool, Ordering},
6    Arc, Mutex,
7};
8pub struct MockPlugin {
9    pub plugin_name: String,
10    pub received: Arc<Mutex<Vec<Command>>>,
11    pub response: Response,
12    pub started: Arc<AtomicBool>,
13    pub stopped: Arc<AtomicBool>,
14}
15impl MockPlugin {
16    pub fn new(name: impl Into<String>) -> Self {
17        Self::with_response(name, Response::Ok)
18    }
19    pub fn with_response(name: impl Into<String>, response: Response) -> Self {
20        Self {
21            plugin_name: name.into(),
22            received: Arc::new(Mutex::new(Vec::new())),
23            response,
24            started: Arc::new(AtomicBool::new(false)),
25            stopped: Arc::new(AtomicBool::new(false)),
26        }
27    }
28}
29#[async_trait]
30impl Plugin for MockPlugin {
31    fn name(&self) -> &str {
32        &self.plugin_name
33    }
34    async fn start(&self, _broker: AnyBroker) -> anyhow::Result<()> {
35        self.started.store(true, Ordering::SeqCst);
36        Ok(())
37    }
38    async fn stop(&self) -> anyhow::Result<()> {
39        self.stopped.store(true, Ordering::SeqCst);
40        Ok(())
41    }
42    async fn send_command(&self, cmd: Command) -> anyhow::Result<Response> {
43        self.received.lock().unwrap().push(cmd);
44        Ok(self.response.clone())
45    }
46}