Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//-----------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Akka.Actor;
using FluentAssertions;
Expand All @@ -21,7 +22,7 @@ protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IService
}

[Fact]
public void A_TestFSMRef_must_allow_access_to_internal_state()
public async Task A_TestFSMRef_must_allow_access_to_internal_state()
{
var fsm = ActorOfAsTestFSMRef<StateTestFsm, int, string>("test-fsm-ref-1");

Expand All @@ -40,10 +41,14 @@ public void A_TestFSMRef_must_allow_access_to_internal_state()
fsm.StateName.Should().Be(1);
fsm.StateData.Should().Be("buh");

// Timed from before SetStateTimeout, so a stall before the wait cannot make the timeout look early.
// The upper bound is generous on purpose: the timeout travels through the scheduler and the
// dispatcher, and a starved CI agent can take seconds.
var elapsed = Stopwatch.StartNew();
fsm.SetStateTimeout(TimeSpan.FromMilliseconds(100));
Within(TimeSpan.FromMilliseconds(80), TimeSpan.FromMilliseconds(500), () =>
AwaitCondition(() => fsm is { StateName: 2, StateData: "timeout" })
);
await AwaitConditionAsync(() => Task.FromResult(fsm is { StateName: 2, StateData: "timeout" }),
Dilated(TimeSpan.FromSeconds(5)));
elapsed.Elapsed.Should().BeGreaterOrEqualTo(TimeSpan.FromMilliseconds(80));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Collections;
using System.Diagnostics;
using System.Threading.Tasks;
using Akka.Actor;
using FluentAssertions;
Expand Down Expand Up @@ -154,21 +155,24 @@ public async Task WaitForRadioSilenceAsync_should_succeed_later_with_good_post_i
}

[Fact]
public async Task WaitForRadioSilenceAsync_should_reset_timer_twice_only()
public async Task WaitForRadioSilenceAsync_should_reset_the_timer_on_each_message()
{
var probe = CreateTestProbe("probe");
var max = TimeSpan.FromMilliseconds(3000);
var halfMax = TimeSpan.FromMilliseconds(max.TotalMilliseconds / 2);
var doubleMax = TimeSpan.FromMilliseconds(max.TotalMilliseconds * 2);
var max = TimeSpan.FromSeconds(4);
var elapsed = Stopwatch.StartNew();
var task = probe.WaitForRadioSilenceAsync(max: max, maxMessages: 2);
await Task.Delay(halfMax);
probe.Ref.Tell(1, TestActor);
await Task.Delay(halfMax);
// well inside the window that opened when 1 was received, so a slow agent cannot push 2 out of it
await Task.Delay(400);
probe.Ref.Tell(2, TestActor);
await Task.Delay(doubleMax);
probe.Ref.Tell(3, TestActor);
var sent2At = elapsed.Elapsed;

var messages = await task;
messages.Should().BeEquivalentTo(new ArrayList { 1, 2 });

// Receiving 2 restarted the window, so the silence cannot end before max after 2 was sent. Without the
// reset it would have ended about max after the call, 400 ms sooner. Delays only make this larger.
elapsed.Elapsed.Should().BeGreaterOrEqualTo(sent2At + max - TimeSpan.FromMilliseconds(100));
}

[Fact]
Expand Down
11 changes: 8 additions & 3 deletions src/core/Akka.TestKit.Tests/TestFSMRefTests/TestFSMRefSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// </copyright>
//-----------------------------------------------------------------------

using System.Diagnostics;
using System.Threading.Tasks;
using Akka.Actor;
using Xunit;
Expand Down Expand Up @@ -35,10 +36,14 @@ public async Task A_TestFSMRef_must_allow_access_to_internal_state()
fsm.StateName.Should().Be(1);
fsm.StateData.Should().Be("buh");

// Timed from before SetStateTimeout, so a stall before the wait cannot make the timeout look early.
// The upper bound is generous on purpose: the timeout travels through the scheduler and the
// dispatcher, and a starved CI agent can take seconds.
var elapsed = Stopwatch.StartNew();
fsm.SetStateTimeout(100.Milliseconds());
await WithinAsync(80.Milliseconds(), 500.Milliseconds(), async () =>
await AwaitConditionAsync(() => Task.FromResult(fsm.StateName == 2 && fsm.StateData == "timeout"))
);
await AwaitConditionAsync(() => Task.FromResult(fsm.StateName == 2 && fsm.StateData == "timeout"),
Dilated(5.Seconds()));
elapsed.Elapsed.Should().BeGreaterOrEqualTo(80.Milliseconds());
}

[Fact]
Expand Down
20 changes: 12 additions & 8 deletions src/core/Akka.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Akka.Actor;
Expand Down Expand Up @@ -144,21 +145,24 @@ public async Task WaitForRadioSilenceAsync_should_succeed_later_with_good_post_i
}

[Fact]
public async Task WaitForRadioSilenceAsync_should_reset_timer_twice_only()
public async Task WaitForRadioSilenceAsync_should_reset_the_timer_on_each_message()
{
var probe = CreateTestProbe("probe");
var max = TimeSpan.FromMilliseconds(3000);
var halfMax = TimeSpan.FromMilliseconds(max.TotalMilliseconds / 2);
var doubleMax = TimeSpan.FromMilliseconds(max.TotalMilliseconds * 2);
var max = TimeSpan.FromSeconds(4);
var elapsed = Stopwatch.StartNew();
var task = probe.WaitForRadioSilenceAsync(max: max, maxMessages: 2);
await Task.Delay(halfMax);
probe.Ref.Tell(1, TestActor);
await Task.Delay(halfMax);
// well inside the window that opened when 1 was received, so a slow agent cannot push 2 out of it
await Task.Delay(400);
probe.Ref.Tell(2, TestActor);
await Task.Delay(doubleMax);
probe.Ref.Tell(3, TestActor);
var sent2At = elapsed.Elapsed;

var messages = await task;
messages.Should().BeEquivalentTo(new ArrayList { 1, 2 });

// Receiving 2 restarted the window, so the silence cannot end before max after 2 was sent. Without the
// reset it would have ended about max after the call, 400 ms sooner. Delays only make this larger.
elapsed.Elapsed.Should().BeGreaterOrEqualTo(sent2At + max - TimeSpan.FromMilliseconds(100));
}

[Fact]
Expand Down
Loading