Skip to content

Commit

Permalink
Fix ReceiveTimeoutSpecs
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorFedchenko committed Feb 9, 2021
1 parent e5ad251 commit 6ce0d74
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/core/Akka.Docs.Tests/Actors/ReceiveTimeoutSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,38 @@ public class ReceiveTimeoutSpecs : TestKit
public class CheckTimeout : INotInfluenceReceiveTimeout { }
public class ReceiveTimeoutActor : ReceiveActor
{
private bool _timedOut = false;
private readonly TimeSpan _inactivityTimeout;

public ReceiveTimeoutActor()
public ReceiveTimeoutActor(TimeSpan inactivityTimeout, IActorRef receiver)
{
_inactivityTimeout = inactivityTimeout;

// if we don't
Receive<ReceiveTimeout>(_ =>
{
_timedOut = true;
});

Receive<CheckTimeout>(_ =>
{
Sender.Tell(_timedOut);
receiver.Tell("timeout");
});
}

protected override void PreStart()
{
Context.SetReceiveTimeout(TimeSpan.FromMilliseconds(100));
Context.SetReceiveTimeout(_inactivityTimeout);
}
}
// </ReceiveTimeoutActor>

[Fact]
public async Task ShouldReceiveTimeoutActors()
{
var receiveTimeout = Sys.ActorOf(Props.Create(() => new ReceiveTimeoutActor()), "receive-timeout");
var timedout1 = await receiveTimeout.Ask<bool>(new CheckTimeout(), TimeSpan.FromMilliseconds(500));
timedout1.Should().BeFalse();

await Task.Delay(200); // wait 200 ms

var timedout2 = await receiveTimeout.Ask<bool>(new CheckTimeout(), TimeSpan.FromMilliseconds(500));
timedout2.Should().BeTrue();
var receiveTimeout = Sys.ActorOf(
Props.Create(() => new ReceiveTimeoutActor(TimeSpan.FromMilliseconds(100), TestActor)),
"receive-timeout");

// should not receive timeout initially
ExpectNoMsg(TimeSpan.FromMilliseconds(50));

// then should receive timeout due to inactivity
ExpectMsg("timeout", TimeSpan.FromSeconds(30));
}
}
}

0 comments on commit 6ce0d74

Please sign in to comment.