Skip to content

Commit

Permalink
Merge branch 'dev' into dependabot/nuget/Google.Protobuf-3.17.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkatufus authored Jun 23, 2021
2 parents a1b67b5 + 43bfec7 commit 5b350a5
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions src/core/Akka.TestKit.Tests/TestKitBaseTests/DilatedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
//-----------------------------------------------------------------------

using System;
using System.Diagnostics;
using Akka.TestKit;
using Xunit;
using Xunit.Sdk;

namespace Akka.Testkit.Tests.TestKitBaseTests
{
public class DilatedTests : AkkaSpec
{
private const int TimeFactor = 4;
private const int Timeout = 50;
private const int Timeout = 1000;
private const int ExpectedTimeout = Timeout * TimeFactor;
private const int DiffDelta = 80;
private const int Margin = 1000; // margin for GC
private const int DiffDelta = 100;

public DilatedTests()
: base("akka.test.timefactor=" + TimeFactor)
Expand All @@ -26,47 +29,49 @@ public DilatedTests()
[Fact]
public void Dilates_correctly_using_timeFactor()
{
Assert.Equal(Dilated(TimeSpan.FromSeconds(1)), TimeSpan.FromSeconds(4));
Assert.Equal(Dilated(TimeSpan.FromMilliseconds(Timeout)), TimeSpan.FromMilliseconds(ExpectedTimeout));
}

[Fact]
public void AwaitCondition_should_dilate_timeout()
{
var before = Now;
Intercept(() => AwaitCondition(() => false, TimeSpan.FromMilliseconds(Timeout)));
var after = Now;
var diff = (after - before).TotalMilliseconds;
Assert.True(Math.Abs(diff - ExpectedTimeout) <= DiffDelta);
var stopwatch = Stopwatch.StartNew();
AssertThrows<TrueException>(() => AwaitCondition(() => false, TimeSpan.FromMilliseconds(Timeout)));
stopwatch.Stop();
AssertDilated(stopwatch.ElapsedMilliseconds, $"Expected the timeout to be {ExpectedTimeout} but in fact it was {stopwatch.ElapsedMilliseconds}.");
}


[Fact]
public void ReceiveN_should_dilate_timeout()
{
var before = Now;
Intercept(() => ReceiveN(42, TimeSpan.FromMilliseconds(Timeout)));
var after = Now;
var diff = (after - before).TotalMilliseconds;
Assert.True(Math.Abs(diff - ExpectedTimeout) <= DiffDelta);
var stopwatch = Stopwatch.StartNew();
AssertThrows<TrueException>(() => ReceiveN(42, TimeSpan.FromMilliseconds(Timeout)));
stopwatch.Stop();
AssertDilated(stopwatch.ElapsedMilliseconds, $"Expected the timeout to be {ExpectedTimeout} but in fact it was {stopwatch.ElapsedMilliseconds}.");
}

[Fact]
public void ExpectMsgAllOf_should_dilate_timeout()
{
var before = Now;
Intercept(() => ExpectMsgAllOf(TimeSpan.FromMilliseconds(Timeout), "1", "2"));
var after = Now;
var diff = (after - before).TotalMilliseconds;
Assert.True(Math.Abs(diff - ExpectedTimeout) <= DiffDelta, string.Format("Expected the timeout to be {0} but in fact it was {1}.", ExpectedTimeout, diff));
var stopwatch = Stopwatch.StartNew();
AssertThrows<TrueException>(() => ExpectMsgAllOf(TimeSpan.FromMilliseconds(Timeout), "1", "2"));
stopwatch.Stop();
AssertDilated(stopwatch.ElapsedMilliseconds, $"Expected the timeout to be {ExpectedTimeout} but in fact it was {stopwatch.ElapsedMilliseconds}.");
}

[Fact]
public void FishForMessage_should_dilate_timeout()
{
var before = Now;
Intercept(() => FishForMessage(_=>false, TimeSpan.FromMilliseconds(Timeout)));
var after = Now;
var diff = (after - before).TotalMilliseconds;
Assert.True(Math.Abs(diff - ExpectedTimeout) <= DiffDelta, string.Format("Expected the timeout to be {0} but in fact it was {1}.", ExpectedTimeout, diff));
var stopwatch = Stopwatch.StartNew();
AssertThrows<TrueException>(() => FishForMessage(_=>false, TimeSpan.FromMilliseconds(Timeout)));
stopwatch.Stop();
AssertDilated(stopwatch.ElapsedMilliseconds, $"Expected the timeout to be {ExpectedTimeout} but in fact it was {stopwatch.ElapsedMilliseconds}.");
}

private static void AssertDilated(double diff, string message = null)
{
Assert.True(diff >= ExpectedTimeout - DiffDelta, message);
Assert.True(diff < ExpectedTimeout + Margin, message); // margin for GC
}
}
}
Expand Down

0 comments on commit 5b350a5

Please sign in to comment.