2022-08-30 15:42:25 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using NUnit.Framework;
|
2022-06-17 14:17:58 +02:00
|
|
|
|
using TINK.ViewModel;
|
|
|
|
|
|
|
|
|
|
namespace TestTINKLib.Fixtures.ObjectTests
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class TestPollingUpdateTask
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verify that no exception is thrown when invoking Terminate.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task TestCtorPollingOff()
|
|
|
|
|
{
|
|
|
|
|
var task = new PollingUpdateTask(() => throw new System.Exception("This must not be called."), null);
|
|
|
|
|
|
|
|
|
|
await task.Terminate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task TestCtorPollingOn()
|
|
|
|
|
{
|
|
|
|
|
int index = 0;
|
|
|
|
|
var task = new PollingUpdateTask(() => index++, new System.TimeSpan(10));
|
2022-08-30 15:42:25 +02:00
|
|
|
|
|
2022-06-17 14:17:58 +02:00
|
|
|
|
System.Threading.SpinWait.SpinUntil(() => index > 2);
|
|
|
|
|
|
|
|
|
|
await task.Terminate();
|
|
|
|
|
|
|
|
|
|
Assert.That(index, Is.AtLeast(3), "Delegate which increments index must be called.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestTerminateRepeated()
|
|
|
|
|
{
|
|
|
|
|
var tasks = new PollingUpdateTask(async () => await Task.Delay(1000), new System.TimeSpan(0, 0, 2));
|
|
|
|
|
|
|
|
|
|
tasks.Terminate().Wait();
|
|
|
|
|
|
|
|
|
|
// Verify that calling terminate twice does not lead to hang of call.
|
|
|
|
|
tasks.Terminate().Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|