Skip to content

Commit

Permalink
Statistical test
Browse files Browse the repository at this point in the history
  • Loading branch information
lemaitre-aneo committed Aug 22, 2024
1 parent f05dd00 commit 087caf4
Showing 1 changed file with 46 additions and 23 deletions.
69 changes: 46 additions & 23 deletions Common/tests/RendezvousChannelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Channels;
Expand Down Expand Up @@ -74,39 +75,59 @@ public async Task WriteShouldWork([Values(0,
.ToList()));
}

[Test]
[Timeout(10000)]
public async Task TimeoutDuration([Values] bool isReader,
[Values] bool useCancellation)
{
var timeout = 15;
var queue = new RendezvousChannel<int>();
var times = new double[100];
var t0 = Stopwatch.GetTimestamp();

for (var i = 0; i < times.Length; ++i)
{
await WaitException(Interact(queue,
isReader,
useCancellation,
timeout))
.ConfigureAwait(false);

var t1 = Stopwatch.GetTimestamp();
times[i] = (t1 - t0) * 1000.0 / Stopwatch.Frequency;
t0 = t1;
}

Array.Sort(times);

var mean = times.Skip(5)
.Take(times.Length - 10)
.Average();

Console.WriteLine($"Expected: {timeout} Mean: {mean:F} Raw: {string.Join(", ", times)}");

Assert.That(mean,
Is.GreaterThanOrEqualTo(timeout - 5));
Assert.That(mean,
Is.LessThanOrEqualTo(timeout + 5));
}

[Test]
[Timeout(10000)]
[Repeat(20)]
public void TimeoutNoCounterParty([Values(0,
15,
45)]
int timeout,
[Values] bool isReader,
public void TimeoutNoCounterParty([Values] bool isReader,
[Values] bool useCancellation)
{
var queue = new RendezvousChannel<int>();

var t0 = DateTime.UtcNow;

Assert.That(() => Interact(queue,
isReader,
useCancellation,
timeout),
5),
Throws.InstanceOf(ExceptionType(useCancellation)));

var t1 = DateTime.UtcNow;
var elapsed = (t1 - t0).TotalMilliseconds;

Assert.That(() => Interact(queue,
!isReader),
Throws.InstanceOf(ExceptionType()));

Console.WriteLine($"Timeout {timeout,7} ms got {elapsed,7} ms");

Assert.That(elapsed,
Is.GreaterThanOrEqualTo(timeout / 2));
Assert.That(elapsed,
Is.LessThanOrEqualTo(timeout * 3 + 50));
}


Expand Down Expand Up @@ -167,9 +188,11 @@ public async Task TimeoutRaceCounterParty([Values] bool isReader,
var waiter = Interact(queue,
isReader,
useCancellation,
30,
15,
true);

await Task.Delay(15)
.ConfigureAwait(false);
var triggerer = Interact(queue,
!isReader,
close: true);
Expand Down Expand Up @@ -199,7 +222,7 @@ waitException is null

[Test]
[Timeout(10000)]
[Repeat(50)]
[Repeat(200)]
public async Task TimeoutRaceClose([Values] bool isReader,
[Values] bool useCancellation)
{
Expand All @@ -208,9 +231,9 @@ public async Task TimeoutRaceClose([Values] bool isReader,
var waiter = Interact(queue,
isReader,
useCancellation,
30);
15);

await Task.Delay(30)
await Task.Delay(15)
.ConfigureAwait(false);
Assert.That(() => Close(queue,
!isReader),
Expand Down

0 comments on commit 087caf4

Please sign in to comment.