2023-06-25 09:06:47 +00:00
|
|
|
|
using Logging;
|
|
|
|
|
|
|
|
|
|
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-06-28 14:19:37 +00:00
|
|
|
|
private readonly TaskFactory taskFactory;
|
2023-06-25 07:53:10 +00:00
|
|
|
|
private readonly Configuration config;
|
2023-06-25 09:06:47 +00:00
|
|
|
|
private readonly BaseLog overviewLog;
|
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-06-25 07:53:10 +00:00
|
|
|
|
|
2023-08-30 08:57:20 +00:00
|
|
|
|
public TestLoop(TaskFactory taskFactory, Configuration config, BaseLog overviewLog, Type testType, TimeSpan runsEvery, StartupChecker startupChecker, CancellationToken cancelToken)
|
2023-06-25 07:53:10 +00:00
|
|
|
|
{
|
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-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-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
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2023-06-29 11:39:05 +00:00
|
|
|
|
WaitHandle.WaitAny(new[] { runFinishedHandle, cancelToken.WaitHandle });
|
|
|
|
|
|
2023-06-28 14:19:37 +00:00
|
|
|
|
cancelToken.ThrowIfCancellationRequested();
|
2023-06-29 11:39:05 +00:00
|
|
|
|
|
2023-06-27 08:16:59 +00:00
|
|
|
|
StartTest();
|
2023-06-28 14:19:37 +00:00
|
|
|
|
|
|
|
|
|
cancelToken.WaitHandle.WaitOne(runsEvery);
|
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
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StartTest()
|
|
|
|
|
{
|
|
|
|
|
var test = (ContinuousTest)Activator.CreateInstance(testType)!;
|
|
|
|
|
var handle = new TestHandle(test);
|
2023-08-30 08:57:20 +00:00
|
|
|
|
var run = new SingleTestRun(taskFactory, config, overviewLog, handle, startupChecker, cancelToken);
|
2023-06-29 11:39:05 +00:00
|
|
|
|
|
|
|
|
|
runFinishedHandle.Reset();
|
|
|
|
|
run.Run(runFinishedHandle);
|
2023-06-25 07:53:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|