Skip to content

Commit

Permalink
Fix ReceiveAsync resetting ReceiveTimeout (#6718)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkatufus authored May 3, 2023
1 parent 95000a6 commit 541ffc9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
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);
}
else
{
Expand Down

0 comments on commit 541ffc9

Please sign in to comment.