From c08507e1b854c3303b21559f7cd43ab2c7619be1 Mon Sep 17 00:00:00 2001 From: benbierens Date: Fri, 23 Jun 2023 11:38:30 +0200 Subject: [PATCH] wip: figuring out log handling for continous tests --- ContinuousTests/AllTestsRun.cs | 8 +-- ContinuousTests/ContinuousTest.cs | 5 ++ ContinuousTests/ContinuousTestRunner.cs | 27 ++++++---- ContinuousTests/TestHandle.cs | 69 ++++++++++++++++++++++++ ContinuousTests/TestMomentAttribute.cs | 12 +++++ ContinuousTests/Tests/MarketplaceTest.cs | 28 ++++++++++ 6 files changed, 137 insertions(+), 12 deletions(-) create mode 100644 ContinuousTests/TestHandle.cs create mode 100644 ContinuousTests/TestMomentAttribute.cs create mode 100644 ContinuousTests/Tests/MarketplaceTest.cs diff --git a/ContinuousTests/AllTestsRun.cs b/ContinuousTests/AllTestsRun.cs index d825cbe..965523a 100644 --- a/ContinuousTests/AllTestsRun.cs +++ b/ContinuousTests/AllTestsRun.cs @@ -18,11 +18,13 @@ namespace ContinuousTests public ContinuousTestResult RunAll() { - var remainingTests = testFinder.CreateTests().ToList(); + var tests = testFinder.CreateTests().ToList(); + var handles = tests.Select(t => new TestHandle(t)).ToArray(); + var result = ContinuousTestResult.Passed; - while (remainingTests.Any()) + while (tests.Any()) { - var test = remainingTests.PickOneRandom(); + var test = tests.PickOneRandom(); var testLog = log.CreateTestLog(test.Name); var singleTestRun = new SingleTestRun(config, test, testLog); diff --git a/ContinuousTests/ContinuousTest.cs b/ContinuousTests/ContinuousTest.cs index 725f456..fcdaecb 100644 --- a/ContinuousTests/ContinuousTest.cs +++ b/ContinuousTests/ContinuousTest.cs @@ -11,6 +11,11 @@ namespace ContinuousTests public abstract class ContinuousTest { + protected const int Zero = 0; + protected const int HourOne = 3600; + protected const int DayOne = HourOne * 24; + protected const int DayThree = DayOne * 3; + private const string UploadFailedMessage = "Unable to store block"; public void Initialize(CodexNode[] nodes, BaseLog log, FileManager fileManager) diff --git a/ContinuousTests/ContinuousTestRunner.cs b/ContinuousTests/ContinuousTestRunner.cs index 117cedc..2c3fe82 100644 --- a/ContinuousTests/ContinuousTestRunner.cs +++ b/ContinuousTests/ContinuousTestRunner.cs @@ -4,7 +4,13 @@ using Logging; namespace ContinuousTests { - public class ContinuousTestRunner + public interface ITestResultHandler + { + void TestPassed(ContinuousTest test); + void TestFailed(ContinuousTest test); + } + + public class ContinuousTestRunner : ITestResultHandler { private readonly ConfigLoader configLoader = new ConfigLoader(); private readonly TestFactory testFactory = new TestFactory(); @@ -18,12 +24,11 @@ namespace ContinuousTests while (true) { var log = new FixtureLog(new LogConfig(config.LogPath, false), "ContinuousTestsRun"); - var allTestsRun = new AllTestsRun(config, log, testFactory); + var allTestsRun = new AllTestsRun(config, log, testFactory, this); - var result = ContinuousTestResult.Passed; try { - result = allTestsRun.RunAll(); + allTestsRun.RunAll(); } catch (Exception ex) { @@ -119,11 +124,15 @@ namespace ContinuousTests } return true; } - } - public enum ContinuousTestResult - { - Passed, - Failed + public void TestPassed(ContinuousTest test) + { + throw new NotImplementedException(); + } + + public void TestFailed(ContinuousTest test) + { + throw new NotImplementedException(); + } } } diff --git a/ContinuousTests/TestHandle.cs b/ContinuousTests/TestHandle.cs new file mode 100644 index 0000000..8bbc28b --- /dev/null +++ b/ContinuousTests/TestHandle.cs @@ -0,0 +1,69 @@ +using System.Reflection; + +namespace ContinuousTests +{ + public class TestHandle + { + private readonly List moments = new List(); + + public TestHandle(ContinuousTest test) + { + Test = test; + + ReflectTestMoments(); + + if (!moments.Any()) throw new Exception("Test has no moments."); + if (moments.Count != moments.Select(m => m.Moment).Distinct().Count()) throw new Exception("Test has duplicate moments"); + } + + public ContinuousTest Test { get; } + + public int? GetNextMoment(int currentMoment) + { + var remainingMoments = moments.Where(m => m.Moment >= currentMoment).ToArray(); + if (!remainingMoments.Any()) return null; + return remainingMoments.Min(m => m.Moment); + } + + public int GetLastMoment() + { + return moments.Max(m => m.Moment); + } + + public void InvokeMoment(int currentMoment) + { + var moment = moments.SingleOrDefault(m => m.Moment == currentMoment); + if (moment == null) return; + + moment.Method.Invoke(Test, Array.Empty()); + } + + private void ReflectTestMoments() + { + var methods = Test.GetType().GetMethods() + .Where(m => m.GetCustomAttributes(typeof(TestMomentAttribute), false).Length > 0) + .ToArray(); + + foreach (var method in methods) + { + var moment = method.GetCustomAttribute(); + if (moment != null && moment.T >= 0) + { + moments.Add(new MethodMoment(method, moment.T)); + } + } + } + } + + public class MethodMoment + { + public MethodMoment(MethodInfo method, int moment) + { + Method = method; + Moment = moment; + } + + public MethodInfo Method { get; } + public int Moment { get; } + } +} diff --git a/ContinuousTests/TestMomentAttribute.cs b/ContinuousTests/TestMomentAttribute.cs new file mode 100644 index 0000000..991725e --- /dev/null +++ b/ContinuousTests/TestMomentAttribute.cs @@ -0,0 +1,12 @@ +namespace ContinuousTests +{ + public class TestMomentAttribute : Attribute + { + public TestMomentAttribute(int t) + { + T = t; + } + + public int T { get; } + } +} diff --git a/ContinuousTests/Tests/MarketplaceTest.cs b/ContinuousTests/Tests/MarketplaceTest.cs new file mode 100644 index 0000000..c5fff94 --- /dev/null +++ b/ContinuousTests/Tests/MarketplaceTest.cs @@ -0,0 +1,28 @@ +using DistTestCore.Codex; + +namespace ContinuousTests.Tests +{ + public class MarketplaceTest : ContinuousTest + { + public override int RequiredNumberOfNodes => 1; + + public override void Run() + { + } + + [TestMoment(t: Zero)] + public void NodePostsStorageRequest() + { + //var c = new KubernetesWorkflow.WorkflowCreator(Log, new KubernetesWorkflow.Configuration()); + //var flow = c.CreateWorkflow(); + //var rc = flow.Start(10, KubernetesWorkflow.Location.Unspecified, new CodexContainerRecipe(), new KubernetesWorkflow.StartupConfig()); + } + + [TestMoment(t: DayThree)] + public void NodeDownloadsStorageRequestData() + { + + } + } + +}