From a447760b931877fb1f30b23c6793492a4cd75494 Mon Sep 17 00:00:00 2001 From: Sergey Popov Date: Fri, 27 Jan 2023 02:08:21 +0300 Subject: [PATCH] Type of LogEvent of UnhandledMessage was changed to INFO in Akka.TestKit (#6354). (#6360) Co-authored-by: Aaron Stannard --- .../UnhandledMessageEventFilterTests.cs | 55 +++++++++++++++++++ .../EventFilter/TestEventListener.cs | 25 ++++----- .../TestActors/UnhandledMessageActor.cs | 20 +++++++ 3 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 src/core/Akka.TestKit.Tests/TestEventListenerTests/UnhandledMessageEventFilterTests.cs create mode 100644 src/core/Akka.TestKit/TestActors/UnhandledMessageActor.cs diff --git a/src/core/Akka.TestKit.Tests/TestEventListenerTests/UnhandledMessageEventFilterTests.cs b/src/core/Akka.TestKit.Tests/TestEventListenerTests/UnhandledMessageEventFilterTests.cs new file mode 100644 index 00000000000..400c8533928 --- /dev/null +++ b/src/core/Akka.TestKit.Tests/TestEventListenerTests/UnhandledMessageEventFilterTests.cs @@ -0,0 +1,55 @@ +// //----------------------------------------------------------------------- +// // +// // Copyright (C) 2009-2023 Lightbend Inc. +// // Copyright (C) 2013-2023 .NET Foundation +// // +// //----------------------------------------------------------------------- + +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(); + } + + 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"); + }); + } + } +} \ No newline at end of file diff --git a/src/core/Akka.TestKit/EventFilter/TestEventListener.cs b/src/core/Akka.TestKit/EventFilter/TestEventListener.cs index 7260024ef89..0b3270c49e6 100644 --- a/src/core/Akka.TestKit/EventFilter/TestEventListener.cs +++ b/src/core/Akka.TestKit/EventFilter/TestEventListener.cs @@ -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); } @@ -64,7 +64,7 @@ protected override bool Receive(object message) } case LogEvent logEvent: { - if(!ShouldFilter(logEvent)) + if (!ShouldFilter(logEvent)) { Print(logEvent); } @@ -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; } @@ -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); } } - } } @@ -129,11 +128,11 @@ 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 @@ -141,8 +140,8 @@ private bool ShouldFilter(LogEvent message) { } } - return false; + return false; } } } diff --git a/src/core/Akka.TestKit/TestActors/UnhandledMessageActor.cs b/src/core/Akka.TestKit/TestActors/UnhandledMessageActor.cs new file mode 100644 index 00000000000..13481f9c4a4 --- /dev/null +++ b/src/core/Akka.TestKit/TestActors/UnhandledMessageActor.cs @@ -0,0 +1,20 @@ +// //----------------------------------------------------------------------- +// // +// // Copyright (C) 2009-2023 Lightbend Inc. +// // Copyright (C) 2013-2023 .NET Foundation +// // +// //----------------------------------------------------------------------- + +using Akka.Actor; + +namespace Akka.TestKit.TestActors +{ + /// + /// An is an actor that don't handle any message + /// sent to it. An UnhandledMessage will be generated as result of handling any message + /// + public class UnhandledMessageActor : ReceiveActor + { + + } +} \ No newline at end of file