mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-07-20 12:33:00 +01:00
Add TestJobHandler
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Tgstation.Server.Host.Core.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public sealed class TestJobHandler
|
||||
{
|
||||
Task currentWaitTask;
|
||||
bool cancelled;
|
||||
|
||||
async Task TestJob(CancellationToken cancellationToken)
|
||||
{
|
||||
await currentWaitTask.ConfigureAwait(false);
|
||||
cancelled = cancellationToken.IsCancellationRequested;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestConstructionAndDisposal()
|
||||
{
|
||||
cancelled = false;
|
||||
Assert.ThrowsException<ArgumentNullException>(() => new JobHandler(null));
|
||||
|
||||
currentWaitTask = Task.CompletedTask;
|
||||
new JobHandler(TestJob).Dispose();
|
||||
Assert.IsFalse(cancelled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TestWait()
|
||||
{
|
||||
cancelled = false;
|
||||
//test with a cancelled cts
|
||||
using (var cts = new CancellationTokenSource())
|
||||
{
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
currentWaitTask = tcs.Task;
|
||||
cts.Cancel();
|
||||
using(var handler = new JobHandler(TestJob))
|
||||
{
|
||||
await Assert.ThrowsExceptionAsync<OperationCanceledException>(() => handler.Wait(cts.Token)).ConfigureAwait(false);
|
||||
tcs.SetResult(null);
|
||||
await handler.Wait(default).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
Assert.IsFalse(cancelled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestProgress()
|
||||
{
|
||||
currentWaitTask = Task.CompletedTask;
|
||||
cancelled = false;
|
||||
using (var handler = new JobHandler(TestJob))
|
||||
{
|
||||
Assert.IsFalse(handler.Progress.HasValue);
|
||||
handler.Progress = 42;
|
||||
Assert.AreEqual(42, handler.Progress);
|
||||
}
|
||||
Assert.IsFalse(cancelled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TestCancellation()
|
||||
{
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
currentWaitTask = tcs.Task;
|
||||
cancelled = false;
|
||||
using(var handler = new JobHandler(TestJob))
|
||||
{
|
||||
handler.Cancel();
|
||||
tcs.SetResult(null);
|
||||
await handler.Wait(default).ConfigureAwait(false);
|
||||
}
|
||||
Assert.IsTrue(cancelled);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user