Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ReceiveAsync resetting ReceiveTimeout #6718

Merged
merged 1 commit into from
May 3, 2023
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
65 changes: 61 additions & 4 deletions src/core/Akka.Tests/Actor/ReceiveTimeoutSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
using FluentAssertions;
using FluentAssertions.Extensions;
using Xunit;
using Xunit.Abstractions;


namespace Akka.Tests.Actor
{
public class Tick { }

public class TransparentTick : INotInfluenceReceiveTimeout { }

public class ReceiveTimeoutSpec : AkkaSpec
{
public class Tick { }

public class TransparentTick : INotInfluenceReceiveTimeout { }

public class TimeoutActor : ActorBase
{
private TestLatch _timeoutLatch;
Expand Down Expand Up @@ -62,6 +63,38 @@ protected override bool Receive(object message)
return false;
}
}

public class AsyncTimeoutActor : ReceiveActor
{
public AsyncTimeoutActor(TestLatch timeoutLatch)
: this(timeoutLatch, TimeSpan.FromMilliseconds(500))
{
}

public AsyncTimeoutActor(TestLatch timeoutLatch, TimeSpan? timeout)
{
var log = Context.GetLogger();

Context.SetReceiveTimeout(timeout.GetValueOrDefault());

ReceiveAsync<ReceiveTimeout>(async _ =>
{
log.Info($"Received {nameof(ReceiveTimeout)}");
timeoutLatch.Open();
});

ReceiveAsync<TransparentTick>(async _ =>
{
log.Info($"Received {nameof(TransparentTick)}");
});

ReceiveAsync<Tick>(async _ =>
{
log.Info($"Received {nameof(Tick)}");
});
}

}

public class TurnOffTimeoutActor : ActorBase
{
Expand Down Expand Up @@ -120,6 +153,10 @@ protected override bool Receive(object message)
}
}

public ReceiveTimeoutSpec(ITestOutputHelper output): base(output)
{
}

[Fact]
public void An_actor_with_receive_timeout_must_get_timeout()
{
Expand Down Expand Up @@ -186,6 +223,26 @@ public void An_actor_with_receive_timeout_must_get_timeout_while_receiving_NotIn
Sys.Stop(timeoutActor);
}

[Fact]
public void An_async_actor_with_receive_timeout_must_get_timeout_while_receiving_NotInfluenceReceiveTimeout_messages()
{
var timeoutLatch = new TestLatch();
var timeoutActor = Sys.ActorOf(Props.Create(() => new AsyncTimeoutActor(timeoutLatch, TimeSpan.FromSeconds(1))));

var cancelable = Sys.Scheduler.Advanced.ScheduleRepeatedlyCancelable(
TimeSpan.FromMilliseconds(100),
TimeSpan.FromMilliseconds(100),
() =>
{
timeoutActor.Tell(new TransparentTick());
//timeoutActor.Tell(new Identify(null));
});

timeoutLatch.Ready(TestKitSettings.DefaultTimeout);
cancelable.Cancel();
Sys.Stop(timeoutActor);
}

[Fact]
public void An_actor_with_receive_timeout_must_get_timeout_while_receiving_only_NotInfluenceReceiveTimeout_messages()
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka/Dispatch/ActorTaskScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public static void RunTask(Func<Task> asyncAction)
if (exception == null)
{
dispatcher.Resume(context);
context.CheckReceiveTimeout();
context.CheckReceiveTimeout(context.CurrentMessage is not INotInfluenceReceiveTimeout);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reschedule flag defaults to true, if we don't set this, ReceiveAsync will always resets receive timeout.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}
else
{
Expand Down