2021-01-26 13:47:37 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2021-01-26 13:47:37 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using NUnit.Framework;
|
2022-01-03 08:31:12 +00:00
|
|
|
using osu.Framework.Extensions;
|
2021-01-29 07:20:25 +00:00
|
|
|
using osu.Game.Utils;
|
2021-01-26 13:47:37 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Tests.NonVisual
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class TaskChainTest
|
|
|
|
{
|
2021-01-29 07:20:25 +00:00
|
|
|
private TaskChain taskChain;
|
2021-01-26 13:47:37 +00:00
|
|
|
private int currentTask;
|
2021-01-26 16:20:50 +00:00
|
|
|
private CancellationTokenSource globalCancellationToken;
|
2021-01-26 13:47:37 +00:00
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
2021-01-26 16:20:50 +00:00
|
|
|
globalCancellationToken = new CancellationTokenSource();
|
2021-01-29 07:20:25 +00:00
|
|
|
taskChain = new TaskChain();
|
2021-01-26 13:47:37 +00:00
|
|
|
currentTask = 0;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:20:50 +00:00
|
|
|
[TearDown]
|
|
|
|
public void TearDown()
|
|
|
|
{
|
|
|
|
globalCancellationToken?.Cancel();
|
|
|
|
}
|
|
|
|
|
2021-01-26 13:47:37 +00:00
|
|
|
[Test]
|
|
|
|
public async Task TestChainedTasksRunSequentially()
|
|
|
|
{
|
|
|
|
var task1 = addTask();
|
|
|
|
var task2 = addTask();
|
|
|
|
var task3 = addTask();
|
|
|
|
|
|
|
|
task3.mutex.Set();
|
|
|
|
task2.mutex.Set();
|
|
|
|
task1.mutex.Set();
|
|
|
|
|
|
|
|
await Task.WhenAll(task1.task, task2.task, task3.task);
|
|
|
|
|
2022-01-06 13:54:43 +00:00
|
|
|
Assert.That(task1.task.GetResultSafely(), Is.EqualTo(1));
|
|
|
|
Assert.That(task2.task.GetResultSafely(), Is.EqualTo(2));
|
|
|
|
Assert.That(task3.task.GetResultSafely(), Is.EqualTo(3));
|
2021-01-26 13:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task TestChainedTaskWithIntermediateCancelRunsInSequence()
|
|
|
|
{
|
|
|
|
var task1 = addTask();
|
|
|
|
var task2 = addTask();
|
|
|
|
var task3 = addTask();
|
|
|
|
|
|
|
|
// Cancel task2, allow task3 to complete.
|
|
|
|
task2.cancellation.Cancel();
|
|
|
|
task2.mutex.Set();
|
|
|
|
task3.mutex.Set();
|
|
|
|
|
|
|
|
// Allow task3 to potentially complete.
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
|
|
|
// Allow task1 to complete.
|
|
|
|
task1.mutex.Set();
|
|
|
|
|
|
|
|
// Wait on both tasks.
|
|
|
|
await Task.WhenAll(task1.task, task3.task);
|
|
|
|
|
2022-01-06 13:54:43 +00:00
|
|
|
Assert.That(task1.task.GetResultSafely(), Is.EqualTo(1));
|
2021-01-26 13:47:37 +00:00
|
|
|
Assert.That(task2.task.IsCompleted, Is.False);
|
2022-01-06 13:54:43 +00:00
|
|
|
Assert.That(task3.task.GetResultSafely(), Is.EqualTo(2));
|
2021-01-26 13:47:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:20:50 +00:00
|
|
|
[Test]
|
|
|
|
public async Task TestChainedTaskDoesNotCompleteBeforeChildTasks()
|
|
|
|
{
|
|
|
|
var mutex = new ManualResetEventSlim(false);
|
|
|
|
|
2021-01-29 07:20:25 +00:00
|
|
|
var task = taskChain.Add(async () => await Task.Run(() => mutex.Wait(globalCancellationToken.Token)));
|
2021-01-26 16:20:50 +00:00
|
|
|
|
|
|
|
// Allow task to potentially complete
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
|
|
|
Assert.That(task.IsCompleted, Is.False);
|
|
|
|
|
|
|
|
// Allow the task to complete.
|
|
|
|
mutex.Set();
|
|
|
|
|
|
|
|
await task;
|
|
|
|
}
|
|
|
|
|
2021-01-26 13:47:37 +00:00
|
|
|
private (Task<int> task, ManualResetEventSlim mutex, CancellationTokenSource cancellation) addTask()
|
|
|
|
{
|
|
|
|
var mutex = new ManualResetEventSlim(false);
|
|
|
|
var completionSource = new TaskCompletionSource<int>();
|
|
|
|
|
2021-01-26 16:20:50 +00:00
|
|
|
var cancellationSource = new CancellationTokenSource();
|
|
|
|
var token = CancellationTokenSource.CreateLinkedTokenSource(cancellationSource.Token, globalCancellationToken.Token);
|
|
|
|
|
2021-01-29 07:20:25 +00:00
|
|
|
taskChain.Add(() =>
|
2021-01-26 13:47:37 +00:00
|
|
|
{
|
2021-01-26 16:20:50 +00:00
|
|
|
mutex.Wait(globalCancellationToken.Token);
|
2021-01-26 13:47:37 +00:00
|
|
|
completionSource.SetResult(Interlocked.Increment(ref currentTask));
|
2021-01-26 16:20:50 +00:00
|
|
|
}, token.Token);
|
2021-01-26 13:47:37 +00:00
|
|
|
|
|
|
|
return (completionSource.Task, mutex, cancellationSource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|