From 09e550df79c6ab4dc3376691e326f787896d82c5 Mon Sep 17 00:00:00 2001 From: benbierens Date: Thu, 1 Jun 2023 16:28:34 +0200 Subject: [PATCH] Adds AssertHelper for better less log-spamming retry-assert. --- DistTestCore/Helpers/AssertHelpers.cs | 22 +++++++++++++++++++ DistTestCore/Marketplace/MarketplaceAccess.cs | 6 ++--- DistTestCore/Metrics/MetricsAccess.cs | 10 ++++----- Utils/Time.cs | 5 +++++ 4 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 DistTestCore/Helpers/AssertHelpers.cs diff --git a/DistTestCore/Helpers/AssertHelpers.cs b/DistTestCore/Helpers/AssertHelpers.cs new file mode 100644 index 0000000..fdbffab --- /dev/null +++ b/DistTestCore/Helpers/AssertHelpers.cs @@ -0,0 +1,22 @@ +using NUnit.Framework.Constraints; +using NUnit.Framework; +using Utils; + +namespace DistTestCore.Helpers +{ + public static class AssertHelpers + { + public static void RetryAssert(IResolveConstraint constraint, Func actual, string message) + { + try + { + var c = constraint.Resolve(); + Time.WaitUntil(() => c.ApplyTo(actual()).IsSuccess); + } + catch (TimeoutException) + { + Assert.That(actual(), constraint, message); + } + } + } +} diff --git a/DistTestCore/Marketplace/MarketplaceAccess.cs b/DistTestCore/Marketplace/MarketplaceAccess.cs index ad9bc7a..7bac8b2 100644 --- a/DistTestCore/Marketplace/MarketplaceAccess.cs +++ b/DistTestCore/Marketplace/MarketplaceAccess.cs @@ -1,4 +1,5 @@ using DistTestCore.Codex; +using DistTestCore.Helpers; using NUnit.Framework; using NUnit.Framework.Constraints; using System.Numerics; @@ -97,10 +98,7 @@ namespace DistTestCore.Marketplace public void AssertThatBalance(IResolveConstraint constraint, string message = "") { - Time.Retry(() => - { - Assert.That(GetBalance(), constraint, message); - }, nameof(AssertThatBalance)); + AssertHelpers.RetryAssert(constraint, GetBalance, message); } public TestToken GetBalance() diff --git a/DistTestCore/Metrics/MetricsAccess.cs b/DistTestCore/Metrics/MetricsAccess.cs index 2032839..5ea0940 100644 --- a/DistTestCore/Metrics/MetricsAccess.cs +++ b/DistTestCore/Metrics/MetricsAccess.cs @@ -1,4 +1,5 @@ -using KubernetesWorkflow; +using DistTestCore.Helpers; +using KubernetesWorkflow; using Logging; using NUnit.Framework; using NUnit.Framework.Constraints; @@ -28,15 +29,14 @@ namespace DistTestCore.Metrics public void AssertThat(string metricName, IResolveConstraint constraint, string message = "") { - Time.Retry(() => + AssertHelpers.RetryAssert(constraint, () => { var metricSet = GetMetricWithTimeout(metricName); var metricValue = metricSet.Values[0].Value; log.Log($"{node.Name} metric '{metricName}' = {metricValue}"); - - Assert.That(metricValue, constraint, message); - }, nameof(AssertThat)); + return metricValue; + }, message); } public Metrics? GetAllMetrics() diff --git a/Utils/Time.cs b/Utils/Time.cs index 6ae2640..102831e 100644 --- a/Utils/Time.cs +++ b/Utils/Time.cs @@ -22,6 +22,11 @@ result += $"{d.Seconds} secs"; return result; } + + public static void WaitUntil(Func predicate) + { + WaitUntil(predicate, TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(1)); + } public static void WaitUntil(Func predicate, TimeSpan timeout, TimeSpan retryTime) {