This test is dumb

This commit is contained in:
Jordan Brown
2020-05-12 19:56:38 -04:00
parent f403a7529d
commit e9afdfc6be
3 changed files with 37 additions and 82 deletions
@@ -66,81 +66,5 @@ namespace Tgstation.Server.Host.Components.Watchdog.Tests
mockServerControl.VerifyAll();
mockChat.VerifyAll();
}
[TestMethod]
public async Task TestSuccessfulLaunchAndShutdown()
{
var mockChat = new Mock<IChatManager>();
mockChat.Setup(x => x.RegisterCommandHandler(It.IsNotNull<ICustomCommandHandler>())).Verifiable();
var mockSessionControllerFactory = new Mock<ISessionControllerFactory>();
var mockDmbFactory = new Mock<IDmbFactory>();
var mockLogger = new Mock<ILogger<ExperimentalWatchdog>>();
var mockReattachInfoHandler = new Mock<IReattachInfoHandler>();
var mockDatabaseContextFactory = new Mock<IDatabaseContextFactory>();
var mockJobManager = new Mock<IJobManager>();
var mockRestartRegistration = new Mock<IRestartRegistration>();
mockRestartRegistration.Setup(x => x.Dispose()).Verifiable();
var mockServerControl = new Mock<IServerControl>();
mockServerControl.Setup(x => x.RegisterForRestart(It.IsNotNull<IRestartHandler>())).Returns(mockRestartRegistration.Object).Verifiable();
var mockLaunchParameters = new DreamDaemonLaunchParameters();
var mockInstance = new Models.Instance();
var mockAsyncDelayer = new Mock<IAsyncDelayer>();
using (var wd = new ExperimentalWatchdog(mockChat.Object, mockSessionControllerFactory.Object, mockDmbFactory.Object, mockReattachInfoHandler.Object, mockDatabaseContextFactory.Object, mockJobManager.Object, mockServerControl.Object, mockAsyncDelayer.Object, mockLogger.Object, mockLaunchParameters, mockInstance, default))
using (var cts = new CancellationTokenSource())
{
var mockCompileJob = new Models.CompileJob();
var mockDmbProvider = new Mock<IDmbProvider>();
mockDmbProvider.SetupGet(x => x.CompileJob).Returns(mockCompileJob).Verifiable();
var mDmbP = mockDmbProvider.Object;
var infiniteTask = new TaskCompletionSource<int>().Task;
mockDmbFactory.SetupGet(x => x.OnNewerDmb).Returns(infiniteTask);
mockDmbFactory.SetupGet(x => x.DmbAvailable).Returns(true).Verifiable();
mockDmbFactory.Setup(x => x.LockNextDmb(2)).Returns(mDmbP).Verifiable();
var sessionsToVerify = new List<Mock<ISessionController>>();
var cancellationToken = cts.Token;
mockSessionControllerFactory.Setup(x => x.LaunchNew(mDmbP, null, mockLaunchParameters, It.IsAny<bool>(), It.IsAny<bool>(), false, cancellationToken)).Returns(() =>
{
var mockSession = new Mock<ISessionController>();
mockSession.SetupGet(x => x.Lifetime).Returns(infiniteTask).Verifiable();
mockSession.SetupGet(x => x.OnReboot).Returns(infiniteTask).Verifiable();
mockSession.SetupGet(x => x.Dmb).Returns(mDmbP).Verifiable();
mockSession.SetupGet(x => x.LaunchResult).Returns(Task.FromResult(new LaunchResult
{
StartupTime = TimeSpan.FromSeconds(1)
})).Verifiable();
sessionsToVerify.Add(mockSession);
return Task.FromResult(mockSession.Object);
}).Verifiable();
mockAsyncDelayer.Setup(x => x.Delay(It.IsAny<TimeSpan>(), cancellationToken)).Returns(Task.CompletedTask).Verifiable();
cts.CancelAfter(TimeSpan.FromSeconds(15));
try
{
await wd.Launch(cancellationToken).ConfigureAwait(false);
await wd.Terminate(false, cancellationToken).ConfigureAwait(false);
}
finally
{
cts.Cancel();
}
Assert.AreEqual(2, sessionsToVerify.Count);
foreach (var I in sessionsToVerify)
I.VerifyAll();
mockDmbProvider.VerifyAll();
}
mockSessionControllerFactory.VerifyAll();
mockDmbFactory.VerifyAll();
mockRestartRegistration.VerifyAll();
mockServerControl.VerifyAll();
mockChat.VerifyAll();
mockAsyncDelayer.VerifyAll();
}
}
}
@@ -9,16 +9,16 @@ using Tgstation.Server.Client.Components;
namespace Tgstation.Server.Tests.Instance
{
abstract class JobsRequiredTest
class JobsRequiredTest
{
protected IJobsClient JobsClient { get; }
protected JobsRequiredTest(IJobsClient jobsClient)
public JobsRequiredTest(IJobsClient jobsClient)
{
this.JobsClient = jobsClient;
}
protected async Task<Job> WaitForJob(Job originalJob, int timeout, bool expectFailure, CancellationToken cancellationToken)
public async Task<Job> WaitForJob(Job originalJob, int timeout, bool expectFailure, CancellationToken cancellationToken)
{
var job = originalJob;
do
@@ -249,15 +249,46 @@ namespace Tgstation.Server.Tests
using (var adminClient = await CreateAdminClient())
{
var instanceClient = adminClient.Instances.CreateClient(instance);
var dd = instanceClient.DreamDaemon.Read(cancellationToken);
await Task.Delay(3000, cancellationToken);
var dd = await instanceClient.DreamDaemon.Read(cancellationToken);
Assert.IsTrue(dd.Running.Value);
await instanceClient.DreamDaemon.Shutdown(cancellationToken);
await instanceClient.DreamDaemon.Update(new DreamDaemon
{
AutoStart = true
}, cancellationToken);
await adminClient.Administration.Restart(cancellationToken);
}
await Task.WhenAny(serverTask, Task.Delay(30000, cancellationToken));
Assert.IsTrue(serverTask.IsCompleted);
serverTask = server.Run(cancellationToken);
using (var adminClient = await CreateAdminClient())
{
var instanceClient = adminClient.Instances.CreateClient(instance);
var jobs = await instanceClient.Jobs.ListActive(cancellationToken);
if (jobs.Any())
{
Assert.AreEqual(1, jobs.Count);
await new JobsRequiredTest(instanceClient.Jobs).WaitForJob(jobs.Single(), 40, false, cancellationToken);
}
await Task.Delay(3000, cancellationToken);
var dd = await instanceClient.DreamDaemon.Read(cancellationToken);
Assert.IsTrue(dd.Running.Value);
var repoTest = new RepositoryTest(instanceClient.Repository, instanceClient.Jobs).RunPostTest(cancellationToken);
await new ChatTest(instanceClient.ChatBots, adminClient.Instances, instance).RunPostTest(cancellationToken);
await repoTest;
await new InstanceManagerTest(adminClient.Instances, adminClient.Users, server.Directory).RunPostTest(cancellationToken);
Assert.IsTrue((await dd).Running.Value);
}
}
catch (Exception ex)