summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_watchdog
diff options
context:
space:
mode:
authorAlbin Suresh <albin.suresh@softwareag.com>2022-05-27 17:47:09 +0530
committerAlbin Suresh <albin.suresh@softwareag.com>2022-05-27 17:47:09 +0530
commit34a3f2f29ee2e6eaeadf5a1f41f85f841ef3d4bb (patch)
tree612c34e477dd1ba7d094a59398e2e8dd433c77be /crates/core/tedge_watchdog
parenta10c6c119899a8a9b36884e4e93e3f6bd873d793 (diff)
Unit test for latest health response validation logic
Diffstat (limited to 'crates/core/tedge_watchdog')
-rw-r--r--crates/core/tedge_watchdog/src/systemd_watchdog.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/crates/core/tedge_watchdog/src/systemd_watchdog.rs b/crates/core/tedge_watchdog/src/systemd_watchdog.rs
index 5ee6b916..8682a9c1 100644
--- a/crates/core/tedge_watchdog/src/systemd_watchdog.rs
+++ b/crates/core/tedge_watchdog/src/systemd_watchdog.rs
@@ -188,3 +188,40 @@ fn get_watchdog_sec(service_file: &str) -> Result<u64, WatchdogError> {
})
}
}
+
+#[cfg(test)]
+mod tests {
+ use anyhow::Result;
+ use serde_json::json;
+
+ use super::*;
+
+ #[tokio::test]
+ async fn test_get_latest_health_status_message() -> Result<()> {
+ let (mut sender, mut receiver) = mpsc::unbounded::<Message>();
+ let health_topic = Topic::new("tedge/health/test-service").expect("Valid topic");
+
+ for x in 1..5i64 {
+ let health_status = json!({
+ "status": "up",
+ "pid": 123u32,
+ "time": x,
+ })
+ .to_string();
+ let health_message = Message::new(&health_topic, health_status);
+ sender.publish(health_message).await?;
+ }
+
+ let health_status = get_latest_health_status_message(3, &mut receiver).await;
+ assert_eq!(health_status.time, 3);
+
+ let timeout_error = tokio::time::timeout(
+ tokio::time::Duration::from_secs(1),
+ get_latest_health_status_message(5, &mut receiver),
+ )
+ .await;
+ assert!(timeout_error.is_err());
+
+ Ok(())
+ }
+}