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

Issue #6948 unit test #6967

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/core/Akka.Tests/Actor/BugFix6948Spec.cs
@@ -0,0 +1,91 @@
//-----------------------------------------------------------------------
// <copyright file="BugFix6948Spec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Event;
using Akka.TestKit;
using FluentAssertions;
using FluentAssertions.Extensions;
using Xunit;
using Xunit.Abstractions;

namespace Akka.Tests.Actor;

public class BugFix6948Spec : AkkaSpec
{
public BugFix6948Spec(ITestOutputHelper output): base("akka.loglevel=DEBUG", output)
{
}

[Fact(DisplayName = "#6948 ActorSystem should terminate cleanly on fatal exceptions")]
public async Task ActorSystem_ShouldTerminateCleanlyOnOOM()
{
var actor = Sys.ActorOf(Props.Create(() => new OutOfMemoryActor()));
actor.Tell("die");
ExpectMsg("dead");

var shutdown = CoordinatedShutdown.Get(Sys);
shutdown.AddTask(CoordinatedShutdown.PhaseBeforeActorSystemTerminate, "z", () =>
{
var pressure = new List<long>();
try
{
foreach (var i in Enumerable.Range(0, 50_000_000))
{
pressure.Add(i);
}
}
catch (OutOfMemoryException ex)
{
Log.Error(ex, "I died (CoordinatedShutdown)");
throw;
}

return Task.FromResult(Done.Instance);
});

var terminated = false;
Sys.RegisterOnTermination(() =>
{
terminated = true;
});

await shutdown.Run(CoordinatedShutdown.UnknownReason.Instance);
await Sys.Terminate().WaitAsync(5.Seconds());
terminated.Should().BeTrue();
}

private class OutOfMemoryActor : ReceiveActor
{
private readonly List<long> _memoryPressure = new ();

public OutOfMemoryActor()
{
var log = Context.GetLogger();

Receive<string>(_ =>
{
try
{
foreach (var i in Enumerable.Range(0, 50_000_000))
{
_memoryPressure.Add(i);
}
}
catch (OutOfMemoryException e)
{
log.Error(e, "I died (Actor)");
Sender.Tell("dead");
}
});
}
}
}
1 change: 1 addition & 0 deletions src/core/Akka.Tests/Akka.Tests.csproj
Expand Up @@ -5,6 +5,7 @@
<AssemblyTitle>Akka.Tests</AssemblyTitle>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(NetFrameworkTestVersion);$(NetTestVersion)</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">$(NetTestVersion)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down