Added test for stopping producers

This commit is contained in:
Thorsten Sommer 2020-09-26 13:32:27 +02:00
parent aa7f21bbce
commit 2b0e92a91d

View File

@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FastRng.Double;
using FastRng.Double.Distributions;
@ -261,5 +262,36 @@ namespace FastRngTests.Double
Assert.That(distribution[..^1].Max() - distribution[..^1].Min(), Is.InRange(0, 6_000));
}
[Test]
[Category(TestCategories.COVER)]
[Category(TestCategories.NORMAL)]
public async Task TestStoppingProducers01()
{
var rng = new MultiThreadedRng();
rng.StopProducer();
var masterToken = new CancellationTokenSource(TimeSpan.FromSeconds(16)).Token;
var wasCanceled = false;
while(true)
{
var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3));
await rng.GetUniform(tokenSource.Token);
if (tokenSource.IsCancellationRequested)
{
wasCanceled = true;
break;
}
if (masterToken.IsCancellationRequested)
{
break;
}
}
Assert.That(masterToken.IsCancellationRequested, Is.False, "Master token was used to stop test");
Assert.That(wasCanceled, Is.True, "The consumer was not canceled");
}
}
}