summaryrefslogtreecommitdiffstats
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
parenta10c6c119899a8a9b36884e4e93e3f6bd873d793 (diff)
Unit test for latest health response validation logic
-rw-r--r--crates/core/tedge_agent/src/agent.rs2
-rw-r--r--crates/core/tedge_mapper/src/collectd/monitor.rs2
-rw-r--r--crates/core/tedge_mapper/src/core/mapper.rs2
-rw-r--r--crates/core/tedge_watchdog/src/systemd_watchdog.rs37
4 files changed, 40 insertions, 3 deletions
diff --git a/crates/core/tedge_agent/src/agent.rs b/crates/core/tedge_agent/src/agent.rs
index 18a1617c..755fc7af 100644
--- a/crates/core/tedge_agent/src/agent.rs
+++ b/crates/core/tedge_agent/src/agent.rs
@@ -298,7 +298,7 @@ impl SmAgent {
let health_status = json!({
"status": "up",
"pid": process::id(),
- "time": OffsetDateTime::now_utc().unix_timestamp()
+ "time": OffsetDateTime::now_utc().unix_timestamp(),
})
.to_string();
let health_message =
diff --git a/crates/core/tedge_mapper/src/collectd/monitor.rs b/crates/core/tedge_mapper/src/collectd/monitor.rs
index 61de30a5..b21f6dcf 100644
--- a/crates/core/tedge_mapper/src/collectd/monitor.rs
+++ b/crates/core/tedge_mapper/src/collectd/monitor.rs
@@ -111,7 +111,7 @@ impl DeviceMonitor {
let health_status = json!({
"status": "up",
"pid": process::id(),
- "time": OffsetDateTime::now_utc().unix_timestamp()
+ "time": OffsetDateTime::now_utc().unix_timestamp(),
})
.to_string();
let health_message = Message::new(&health_status_topic, health_status);
diff --git a/crates/core/tedge_mapper/src/core/mapper.rs b/crates/core/tedge_mapper/src/core/mapper.rs
index 58a212a1..fb5713f4 100644
--- a/crates/core/tedge_mapper/src/core/mapper.rs
+++ b/crates/core/tedge_mapper/src/core/mapper.rs
@@ -135,7 +135,7 @@ impl Mapper {
let health_status = json!({
"status": "up",
"pid": process::id(),
- "time": OffsetDateTime::now_utc().unix_timestamp()
+ "time": OffsetDateTime::now_utc().unix_timestamp(),
})
.to_string();
let health_message = Message::new(&self.health_status_topic, health_status);
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(())
+ }
+}