Skip to content

Commit

Permalink
Type of LogEvent of UnhandledMessage was changed to INFO in Akka.Test…
Browse files Browse the repository at this point in the history
…Kit (akkadotnet#6354). (akkadotnet#6360)

Co-authored-by: Aaron Stannard <aaron@petabridge.com>
  • Loading branch information
F0b0s and Aaronontheweb authored Jan 26, 2023
1 parent d43a8d0 commit a447760
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// //-----------------------------------------------------------------------
// // <copyright file="UnhandledMessageEventFilterTestsBase.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.Text.RegularExpressions;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Event;
using Akka.TestKit.TestActors;
using Xunit;

namespace Akka.TestKit.Tests.TestEventListenerTests
{
public class UnhandledMessageEventFilterTests : EventFilterTestBase
{
private readonly IActorRef _unhandledMessageActor;

public UnhandledMessageEventFilterTests() : base("akka.loglevel=INFO")
{
_unhandledMessageActor = Sys.ActorOf<UnhandledMessageActor>();
}

protected override void SendRawLogEventMessage(object message)
{
Sys.EventStream.Publish(new Error(null, "UnhandledMessageEventFilterTests", GetType(), message));
}

[Fact]
public async Task Unhandled_message_should_produce_info_message()
{
await EventFilter
.Info(new Regex("^Unhandled message from"))
.ExpectOneAsync(async () =>
{
_unhandledMessageActor.Tell("whatever");
});
}

[Fact]
public async Task Unhandled_message_should_not_produce_warn_and_error_message()
{
await EventFilter
.Warning()
.And
.Error()
.ExpectAsync(0, async () =>
{
_unhandledMessageActor.Tell("whatever");
});
}
}
}
25 changes: 12 additions & 13 deletions src/core/Akka.TestKit/EventFilter/TestEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected override bool Receive(object message)
}
case Mute mute:
{
foreach(var filter in mute.Filters)
foreach (var filter in mute.Filters)
{
AddFilter(filter);
}
Expand All @@ -64,7 +64,7 @@ protected override bool Receive(object message)
}
case LogEvent logEvent:
{
if(!ShouldFilter(logEvent))
if (!ShouldFilter(logEvent))
{
Print(logEvent);
}
Expand All @@ -78,9 +78,9 @@ protected override bool Receive(object message)
case UnhandledMessage un:
{
var rcp = un.Recipient;
var warning = new Warning(rcp.Path.ToString(), rcp.GetType(), "Unhandled message from " + un.Sender + ": " + un.Message);
if(!ShouldFilter(warning))
Print(warning);
var info = new Info(rcp.Path.ToString(), rcp.GetType(), "Unhandled message from " + un.Sender + ": " + un.Message);
if (!ShouldFilter(info))
Print(info);
break;
}

Expand All @@ -97,23 +97,22 @@ private void HandleDeadLetter(DeadLetter message)
var msg = message.Message;
var rcp = message.Recipient;
var snd = message.Sender;
if(!(msg is Terminate))
if (!(msg is Terminate))
{
var recipientPath = rcp.Path.ToString();
var recipientType = rcp.GetType();
var warning = new Warning(recipientPath, recipientType, message);
if(!ShouldFilter(warning))
if (!ShouldFilter(warning))
{
var msgStr = (msg is ISystemMessage)
? "Received dead system message: " + msg
: "Received dead letter from " + snd + ": " + msg;
var warning2 = new Warning(recipientPath, recipientType, new DeadLetter(msgStr,snd,rcp));
if(!ShouldFilter(warning2))
var warning2 = new Warning(recipientPath, recipientType, new DeadLetter(msgStr, snd, rcp));
if (!ShouldFilter(warning2))
{
Print(warning2);
}
}

}
}

Expand All @@ -129,20 +128,20 @@ private void RemoveFilter(IEventFilter filter)

private bool ShouldFilter(LogEvent message)
{
foreach(var filter in _filters)
foreach (var filter in _filters)
{
try
{
if(filter.Apply(message))
if (filter.Apply(message))
return true;
}
// ReSharper disable once EmptyGeneralCatchClause
catch
{
}
}
return false;

return false;
}
}
}
20 changes: 20 additions & 0 deletions src/core/Akka.TestKit/TestActors/UnhandledMessageActor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// //-----------------------------------------------------------------------
// // <copyright file="NoMessageHandlingActor.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 Akka.Actor;

namespace Akka.TestKit.TestActors
{
/// <summary>
/// An <see cref="UnhandledMessageActor"/> is an actor that don't handle any message
/// sent to it. An UnhandledMessage will be generated as result of handling any message
/// </summary>
public class UnhandledMessageActor : ReceiveActor
{

}
}

0 comments on commit a447760

Please sign in to comment.