2023-03-29 09:29:43 +00:00
|
|
|
|
using NUnit.Framework;
|
2023-03-29 08:07:16 +00:00
|
|
|
|
using NUnit.Framework.Constraints;
|
2023-03-27 14:24:04 +00:00
|
|
|
|
|
|
|
|
|
namespace CodexDistTestCore
|
2023-03-27 12:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
public interface IMetricsAccess
|
|
|
|
|
{
|
2023-03-29 08:07:16 +00:00
|
|
|
|
void AssertThat(IOnlineCodexNode node, string metricName, IResolveConstraint constraint, string message = "");
|
2023-03-27 12:49:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MetricsAccess : IMetricsAccess
|
|
|
|
|
{
|
2023-03-29 09:29:43 +00:00
|
|
|
|
private readonly MetricsQuery query;
|
2023-03-28 11:43:25 +00:00
|
|
|
|
private readonly OnlineCodexNode[] nodes;
|
2023-03-27 14:24:04 +00:00
|
|
|
|
|
2023-03-29 09:29:43 +00:00
|
|
|
|
public MetricsAccess(MetricsQuery query, OnlineCodexNode[] nodes)
|
2023-03-27 14:24:04 +00:00
|
|
|
|
{
|
2023-03-29 09:29:43 +00:00
|
|
|
|
this.query = query;
|
2023-03-28 11:43:25 +00:00
|
|
|
|
this.nodes = nodes;
|
2023-03-27 14:24:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 08:07:16 +00:00
|
|
|
|
public void AssertThat(IOnlineCodexNode node, string metricName, IResolveConstraint constraint, string message = "")
|
|
|
|
|
{
|
2023-03-29 09:29:43 +00:00
|
|
|
|
var n = (OnlineCodexNode)node;
|
|
|
|
|
CollectionAssert.Contains(nodes, n, "Incorrect test setup: Attempt to get metrics for OnlineCodexNode from the wrong MetricsAccess object. " +
|
|
|
|
|
"(This CodexNode is tracked by a different instance.)");
|
|
|
|
|
|
|
|
|
|
var metricSet = GetMetricWithTimeout(metricName, n);
|
|
|
|
|
var metricValue = metricSet.Values[0].Value;
|
2023-03-29 08:07:16 +00:00
|
|
|
|
Assert.That(metricValue, constraint, message);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 09:29:43 +00:00
|
|
|
|
private MetricsSet GetMetricWithTimeout(string metricName, OnlineCodexNode node)
|
2023-03-29 08:07:16 +00:00
|
|
|
|
{
|
|
|
|
|
var start = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
var mostRecent = GetMostRecent(metricName, node);
|
2023-03-29 09:29:43 +00:00
|
|
|
|
if (mostRecent != null) return mostRecent;
|
2023-03-29 08:07:16 +00:00
|
|
|
|
if (DateTime.UtcNow - start > Timing.WaitForMetricTimeout())
|
|
|
|
|
{
|
|
|
|
|
Assert.Fail($"Timeout: Unable to get metric '{metricName}'.");
|
|
|
|
|
throw new TimeoutException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils.Sleep(TimeSpan.FromSeconds(2));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 09:29:43 +00:00
|
|
|
|
private MetricsSet? GetMostRecent(string metricName, OnlineCodexNode node)
|
2023-03-27 12:49:34 +00:00
|
|
|
|
{
|
2023-03-30 08:43:17 +00:00
|
|
|
|
var result = query.GetMostRecent(metricName, node);
|
2023-03-29 09:29:43 +00:00
|
|
|
|
if (result == null) return null;
|
2023-03-30 08:43:17 +00:00
|
|
|
|
return result.Sets.LastOrDefault();
|
2023-03-27 12:49:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|