From b4f5b64ae5ee0546326a78df617527da4780d885 Mon Sep 17 00:00:00 2001
From: s0me0ne-unkn0wn <48632512+s0me0ne-unkn0wn@users.noreply.github.com>
Date: Sat, 16 Mar 2024 14:30:14 +0100
Subject: [PATCH] Generalize metric assertions (#189)

---
 crates/orchestrator/src/network/node.rs | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/crates/orchestrator/src/network/node.rs b/crates/orchestrator/src/network/node.rs
index 5c4a4cd..ca84186 100644
--- a/crates/orchestrator/src/network/node.rs
+++ b/crates/orchestrator/src/network/node.rs
@@ -148,17 +148,27 @@ impl NetworkNode {
         &self,
         metric_name: impl Into<String>,
         value: impl Into<f64>,
+    ) -> Result<bool, anyhow::Error> {
+        let value: f64 = value.into();
+        self.assert_with(metric_name, |v| v == value).await
+    }
+
+    /// Assert on a metric value using a given predicate.
+    /// See [`assert`] description for details.
+    pub async fn assert_with(
+        &self,
+        metric_name: impl Into<String>,
+        predicate: impl Fn(f64) -> bool,
     ) -> Result<bool, anyhow::Error> {
         let metric_name = metric_name.into();
-        let value = value.into();
         let val = self.metric(&metric_name).await?;
-        if val == value {
+        if predicate(val) {
             Ok(true)
         } else {
             // reload metrics
             self.fetch_metrics().await?;
             let val = self.metric(&metric_name).await?;
-            Ok(val == value)
+            Ok(predicate(val))
         }
     }
 
-- 
GitLab