Add test verifying basic functionality and state persistence.
Signed-off-by: Arthur Bied-Charreton <[email protected]>
---
proxmox-notify/src/lib.rs | 56 ++++++++++++++++++++++++++++++++++++++-
1 file changed, 55 insertions(+), 1 deletion(-)
diff --git a/proxmox-notify/src/lib.rs b/proxmox-notify/src/lib.rs
index 3bd83ce4..f4fd3aa2 100644
--- a/proxmox-notify/src/lib.rs
+++ b/proxmox-notify/src/lib.rs
@@ -683,10 +683,25 @@ mod tests {
messages: Rc<RefCell<Vec<Notification>>>,
}
+ #[derive(Default, Clone, Serialize, Deserialize)]
+ struct MockEndpointState {
+ notifications_sent: usize,
+ }
+
+ impl EndpointState for MockEndpointState {}
+
impl Endpoint for MockEndpoint {
- fn send(&self, message: &Notification, _: &mut State) -> Result<(),
Error> {
+ fn send(&self, message: &Notification, state: &mut State) ->
Result<(), Error> {
+ let endpoint_state =
state.get_or_default::<MockEndpointState>(self.name)?;
self.messages.borrow_mut().push(message.clone());
+ state.set(
+ self.name,
+ &MockEndpointState {
+ notifications_sent: endpoint_state.notifications_sent + 1,
+ },
+ )?;
+
Ok(())
}
@@ -791,4 +806,43 @@ mod tests {
Ok(())
}
+
+ #[test]
+ fn test_endpoint_with_state() -> Result<(), Error> {
+ let mut state = State::default();
+ let mock = MockEndpoint::new("endpoint");
+
+ let mut bus = Bus::default();
+ bus.add_endpoint(Box::new(mock.clone()));
+
+ let matcher = MatcherConfig {
+ target: vec!["endpoint".into()],
+ ..Default::default()
+ };
+
+ bus.add_matcher(matcher);
+
+ // Send directly to endpoint
+ bus.send(
+ &Notification::from_template(
+ Severity::Info,
+ "test",
+ Default::default(),
+ Default::default(),
+ ),
+ &mut state,
+ );
+
+ let endpoint_state =
state.get::<MockEndpointState>("endpoint")?.unwrap();
+
+ assert_eq!(endpoint_state.notifications_sent, 1);
+
+ state.persist(context().state_file_path())?;
+
+ let persisted_state = State::from_path(context().state_file_path())?;
+
+ assert_eq!(state, persisted_state);
+
+ Ok(())
+ }
}
--
2.47.3