2023-11-09 10:35:45 +00:00
|
|
|
|
using DistTestCore.Logs;
|
|
|
|
|
using Logging;
|
2023-12-20 14:56:03 +00:00
|
|
|
|
using TaskFactory = Utils.TaskFactory;
|
2023-06-25 09:06:47 +00:00
|
|
|
|
|
|
|
|
|
namespace ContinuousTests
|
2023-06-25 07:53:10 +00:00
|
|
|
|
{
|
2023-06-27 08:16:59 +00:00
|
|
|
|
public class TestLoop
|
2023-06-25 07:53:10 +00:00
|
|
|
|
{
|
2023-09-21 08:33:09 +00:00
|
|
|
|
private readonly EntryPointFactory entryPointFactory;
|
2023-06-28 14:19:37 +00:00
|
|
|
|
private readonly TaskFactory taskFactory;
|
2023-06-25 07:53:10 +00:00
|
|
|
|
private readonly Configuration config;
|
2023-09-21 08:33:09 +00:00
|
|
|
|
private readonly ILog overviewLog;
|
2023-11-09 10:35:45 +00:00
|
|
|
|
private readonly StatusLog statusLog;
|
2023-06-25 07:53:10 +00:00
|
|
|
|
private readonly Type testType;
|
|
|
|
|
private readonly TimeSpan runsEvery;
|
2023-08-30 08:57:20 +00:00
|
|
|
|
private readonly StartupChecker startupChecker;
|
2023-06-28 14:19:37 +00:00
|
|
|
|
private readonly CancellationToken cancelToken;
|
2023-06-29 11:39:05 +00:00
|
|
|
|
private readonly EventWaitHandle runFinishedHandle = new EventWaitHandle(true, EventResetMode.ManualReset);
|
2023-10-02 09:18:27 +00:00
|
|
|
|
private static object testLock = new object();
|
2023-06-25 07:53:10 +00:00
|
|
|
|
|
2023-11-09 10:35:45 +00:00
|
|
|
|
public TestLoop(EntryPointFactory entryPointFactory, TaskFactory taskFactory, Configuration config, ILog overviewLog, StatusLog statusLog, Type testType, TimeSpan runsEvery, StartupChecker startupChecker, CancellationToken cancelToken)
|
2023-06-25 07:53:10 +00:00
|
|
|
|
{
|
2023-09-21 08:33:09 +00:00
|
|
|
|
this.entryPointFactory = entryPointFactory;
|
2023-06-28 14:19:37 +00:00
|
|
|
|
this.taskFactory = taskFactory;
|
2023-06-25 07:53:10 +00:00
|
|
|
|
this.config = config;
|
2023-06-25 09:06:47 +00:00
|
|
|
|
this.overviewLog = overviewLog;
|
2023-11-09 10:35:45 +00:00
|
|
|
|
this.statusLog = statusLog;
|
2023-06-25 07:53:10 +00:00
|
|
|
|
this.testType = testType;
|
|
|
|
|
this.runsEvery = runsEvery;
|
2023-08-30 08:57:20 +00:00
|
|
|
|
this.startupChecker = startupChecker;
|
2023-06-28 14:19:37 +00:00
|
|
|
|
this.cancelToken = cancelToken;
|
2023-06-27 08:16:59 +00:00
|
|
|
|
Name = testType.Name;
|
2023-06-25 07:53:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 08:16:59 +00:00
|
|
|
|
public string Name { get; }
|
2023-09-28 07:39:15 +00:00
|
|
|
|
public int NumberOfPasses { get; private set; }
|
|
|
|
|
public int NumberOfFailures { get; private set; }
|
2023-06-27 08:16:59 +00:00
|
|
|
|
|
2023-06-25 07:53:10 +00:00
|
|
|
|
public void Begin()
|
|
|
|
|
{
|
2023-06-28 14:19:37 +00:00
|
|
|
|
taskFactory.Run(() =>
|
2023-06-25 07:53:10 +00:00
|
|
|
|
{
|
2023-06-27 08:16:59 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-28 07:39:15 +00:00
|
|
|
|
NumberOfPasses = 0;
|
|
|
|
|
NumberOfFailures = 0;
|
2023-10-01 08:52:05 +00:00
|
|
|
|
while (!cancelToken.IsCancellationRequested)
|
2023-06-27 08:16:59 +00:00
|
|
|
|
{
|
2023-10-02 09:18:27 +00:00
|
|
|
|
lock (testLock)
|
|
|
|
|
// In the original design, multiple tests are allowed to interleave their test-moments, increasing test through-put.
|
|
|
|
|
// Since we're still stabilizing some of the basics, this lock limits us to 1 test run at a time.
|
|
|
|
|
{
|
|
|
|
|
WaitHandle.WaitAny(new[] { runFinishedHandle, cancelToken.WaitHandle });
|
2023-06-29 11:39:05 +00:00
|
|
|
|
|
2023-10-02 09:18:27 +00:00
|
|
|
|
cancelToken.ThrowIfCancellationRequested();
|
2023-06-29 11:39:05 +00:00
|
|
|
|
|
2023-10-02 09:18:27 +00:00
|
|
|
|
StartTest();
|
2023-06-28 14:19:37 +00:00
|
|
|
|
|
2023-10-02 09:18:27 +00:00
|
|
|
|
cancelToken.WaitHandle.WaitOne(runsEvery);
|
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(100);
|
2023-06-27 08:16:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-28 14:19:37 +00:00
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
overviewLog.Log("Test-loop " + testType.Name + " is cancelled.");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2023-06-25 07:53:10 +00:00
|
|
|
|
{
|
2023-06-27 08:16:59 +00:00
|
|
|
|
overviewLog.Error("Test infra failure: TestLoop failed with " + ex);
|
|
|
|
|
Environment.Exit(-1);
|
2023-06-25 07:53:10 +00:00
|
|
|
|
}
|
2023-11-14 12:28:50 +00:00
|
|
|
|
}, nameof(TestLoop));
|
2023-06-25 07:53:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StartTest()
|
|
|
|
|
{
|
|
|
|
|
var test = (ContinuousTest)Activator.CreateInstance(testType)!;
|
|
|
|
|
var handle = new TestHandle(test);
|
2023-11-09 10:35:45 +00:00
|
|
|
|
var run = new SingleTestRun(entryPointFactory, taskFactory, config, overviewLog, statusLog, handle, startupChecker, cancelToken);
|
2023-06-29 11:39:05 +00:00
|
|
|
|
|
|
|
|
|
runFinishedHandle.Reset();
|
2023-09-28 07:39:15 +00:00
|
|
|
|
run.Run(runFinishedHandle, result =>
|
|
|
|
|
{
|
|
|
|
|
if (result) NumberOfPasses++;
|
|
|
|
|
else NumberOfFailures++;
|
|
|
|
|
});
|
2023-06-25 07:53:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|