From 47e6775de0f4500b8d46c91161af0081afd6ea54 Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Sat, 23 Apr 2022 02:40:43 +0700 Subject: [PATCH 1/2] Convert Akka.TestKit.Tests.TestEventListenerTests to async --- .../AllTestForEventFilterBase.cs | 123 ++++++++---------- .../AllTestForEventFilterBase_Instances.cs | 1 + .../TestEventListenerTests/ConfigTests.cs | 2 +- .../CustomEventFilterTests.cs | 29 +++-- .../DeadLettersEventFilterTests.cs | 19 +-- .../EventFilterTestBase.cs | 21 +-- .../ExceptionEventFilterTests.cs | 100 +++++++------- .../ForwardAllEventsTestEventListener.cs | 13 +- 8 files changed, 152 insertions(+), 156 deletions(-) diff --git a/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase.cs b/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase.cs index 98f67a48f0a..e97ec586e56 100644 --- a/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase.cs +++ b/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase.cs @@ -7,29 +7,26 @@ using System; using System.Threading.Tasks; -using Akka.Actor; using Akka.Event; -using Akka.TestKit.Xunit2.Internals; using FluentAssertions; using Xunit; using Xunit.Sdk; +using static FluentAssertions.FluentActions; -namespace Akka.TestKit.Tests.Xunit2.TestEventListenerTests +namespace Akka.TestKit.Tests.TestEventListenerTests { public abstract class AllTestForEventFilterBase : EventFilterTestBase where TLogEvent : LogEvent { - // ReSharper disable ConvertToLambdaExpression private readonly EventFilterFactory _testingEventFilter; protected AllTestForEventFilterBase(string config) : base(config) { LogLevel = Logging.LogLevelFor(); - // ReSharper disable once VirtualMemberCallInContructor _testingEventFilter = CreateTestingEventFilter(); } - protected LogLevel LogLevel { get; private set; } + protected LogLevel LogLevel { get; } protected abstract EventFilterFactory CreateTestingEventFilter(); protected void LogMessage(string message) @@ -45,62 +42,66 @@ protected override void SendRawLogEventMessage(object message) protected abstract void PublishMessage(object message, string source); [Fact] - public void Single_message_is_intercepted() + public async Task Single_message_is_intercepted() { - _testingEventFilter.ForLogLevel(LogLevel).ExpectOne(() => LogMessage("whatever")); + await _testingEventFilter.ForLogLevel(LogLevel) + .ExpectOneAsync(() => LogMessage("whatever")); TestSuccessful = true; } [Fact] - public void Can_intercept_messages_when_start_is_specified() + public async Task Can_intercept_messages_when_start_is_specified() { - _testingEventFilter.ForLogLevel(LogLevel, start: "what").ExpectOne(() => LogMessage("whatever")); + await _testingEventFilter.ForLogLevel(LogLevel, start: "what") + .ExpectOneAsync(() => LogMessage("whatever")); TestSuccessful = true; } [Fact] - public void Do_not_intercept_messages_when_start_does_not_match() + public async Task Do_not_intercept_messages_when_start_does_not_match() { - _testingEventFilter.ForLogLevel(LogLevel, start: "what").ExpectOne(() => + await _testingEventFilter.ForLogLevel(LogLevel, start: "what").ExpectOneAsync(() => { LogMessage("let-me-thru"); LogMessage("whatever"); }); - ExpectMsg(err => (string)err.Message == "let-me-thru"); + await ExpectMsgAsync(err => (string)err.Message == "let-me-thru"); TestSuccessful = true; } [Fact] - public void Can_intercept_messages_when_message_is_specified() + public async Task Can_intercept_messages_when_message_is_specified() { - _testingEventFilter.ForLogLevel(LogLevel, message: "whatever").ExpectOne(() => LogMessage("whatever")); + await _testingEventFilter.ForLogLevel(LogLevel, message: "whatever") + .ExpectOneAsync(() => LogMessage("whatever")); TestSuccessful = true; } [Fact] - public void Do_not_intercept_messages_when_message_does_not_match() + public async Task Do_not_intercept_messages_when_message_does_not_match() { - EventFilter.ForLogLevel(LogLevel, message: "whatever").ExpectOne(() => + await EventFilter.ForLogLevel(LogLevel, message: "whatever").ExpectOneAsync(() => { LogMessage("let-me-thru"); LogMessage("whatever"); }); - ExpectMsg(err => (string)err.Message == "let-me-thru"); + await ExpectMsgAsync(err => (string)err.Message == "let-me-thru"); TestSuccessful = true; } [Fact] - public void Can_intercept_messages_when_contains_is_specified() + public async Task Can_intercept_messages_when_contains_is_specified() { - _testingEventFilter.ForLogLevel(LogLevel, contains: "ate").ExpectOne(() => LogMessage("whatever")); + await _testingEventFilter.ForLogLevel(LogLevel, contains: "ate") + .ExpectOneAsync(() => LogMessage("whatever")); TestSuccessful = true; } [Fact] - public void Do_not_intercept_messages_when_contains_does_not_match() + public async Task Do_not_intercept_messages_when_contains_does_not_match() { - _testingEventFilter.ForLogLevel(LogLevel, contains: "eve").ExpectOne(() => + await _testingEventFilter.ForLogLevel(LogLevel, contains: "eve").ExpectOneAsync(() => { LogMessage("let-me-thru"); LogMessage("whatever"); @@ -111,28 +112,29 @@ public void Do_not_intercept_messages_when_contains_does_not_match() [Fact] - public void Can_intercept_messages_when_source_is_specified() + public async Task Can_intercept_messages_when_source_is_specified() { - _testingEventFilter.ForLogLevel(LogLevel, source: LogSource.FromType(GetType(), Sys)).ExpectOne(() => LogMessage("whatever")); + await _testingEventFilter.ForLogLevel(LogLevel, source: LogSource.FromType(GetType(), Sys)) + .ExpectOneAsync(() => LogMessage("whatever")); TestSuccessful = true; } [Fact] - public void Do_not_intercept_messages_when_source_does_not_match() + public async Task Do_not_intercept_messages_when_source_does_not_match() { - _testingEventFilter.ForLogLevel(LogLevel, source: "expected-source").ExpectOne(() => + await _testingEventFilter.ForLogLevel(LogLevel, source: "expected-source").ExpectOneAsync(() => { PublishMessage("message", source: "expected-source"); PublishMessage("message", source: "let-me-thru"); }); - ExpectMsg(err => err.LogSource == "let-me-thru"); + await ExpectMsgAsync(err => err.LogSource == "let-me-thru"); TestSuccessful = true; } [Fact] - public void Specified_numbers_of_messagesan_be_intercepted() + public async Task Specified_numbers_of_messages_can_be_intercepted() { - _testingEventFilter.ForLogLevel(LogLevel).Expect(2, () => + await _testingEventFilter.ForLogLevel(LogLevel).ExpectAsync(2, () => { LogMessage("whatever"); LogMessage("whatever"); @@ -141,35 +143,28 @@ public void Specified_numbers_of_messagesan_be_intercepted() } [Fact] - public void Expect_0_events_Should_work() + public async Task Expect_0_events_Should_work() { - this.Invoking(_ => + await Awaiting(async () => { - EventFilter.Error().Expect(0, () => + await EventFilter.Error().ExpectAsync(0, () => { Log.Error("something"); }); - }).Should().Throw("Expected 0 events"); + }).Should().ThrowAsync("Expected 0 events"); } [Fact] public async Task ExpectAsync_0_events_Should_work() { - Exception ex = null; - try + await Awaiting(async () => { await EventFilter.Error().ExpectAsync(0, async () => { await Task.Delay(100); // bug only happens when error is not logged instantly Log.Error("something"); }); - } - catch (Exception e) - { - ex = e; - } - - ex.Should().NotBeNull("Expected 0 errors logged, but there are error logs"); + }).Should().ThrowAsync("Expected 0 errors logged, but there are error logs"); } /// issue: InternalExpectAsync does not await actionAsync() - causing actionAsync to run as a detached task #5537 @@ -201,9 +196,9 @@ await _testingEventFilter.ForLogLevel(LogLevel).ExpectAsync(0, async () => } [Fact] - public void Messages_can_be_muted() + public async Task Messages_can_be_muted() { - _testingEventFilter.ForLogLevel(LogLevel).Mute(() => + await _testingEventFilter.ForLogLevel(LogLevel).MuteAsync(() => { LogMessage("whatever"); LogMessage("whatever"); @@ -235,54 +230,50 @@ public void Messages_can_be_muted_from_now_on_with_using() [Fact] - public void Make_sure_async_works() + public async Task Make_sure_async_works() { - _testingEventFilter.ForLogLevel(LogLevel).Expect(1, TimeSpan.FromSeconds(2), () => + await _testingEventFilter.ForLogLevel(LogLevel).ExpectAsync(1, TimeSpan.FromSeconds(2), () => { Task.Delay(TimeSpan.FromMilliseconds(10)).ContinueWith(t => { LogMessage("whatever"); }); }); } [Fact] - public void Chain_many_filters() + public async Task Chain_many_filters() { - _testingEventFilter + await _testingEventFilter .ForLogLevel(LogLevel,message:"Message 1").And .ForLogLevel(LogLevel,message:"Message 3") - .Expect(2,() => - { - LogMessage("Message 1"); - LogMessage("Message 2"); - LogMessage("Message 3"); - - }); - ExpectMsg(m => (string) m.Message == "Message 2"); + .ExpectAsync(2,() => + { + LogMessage("Message 1"); + LogMessage("Message 2"); + LogMessage("Message 3"); + + }); + await ExpectMsgAsync(m => (string) m.Message == "Message 2"); } [Fact] - public void Should_timeout_if_too_few_messages() + public async Task Should_timeout_if_too_few_messages() { - var exception = XAssert.Throws(() => + await Awaiting(async () => { - _testingEventFilter.ForLogLevel(LogLevel).Expect(2, TimeSpan.FromMilliseconds(50), () => + await _testingEventFilter.ForLogLevel(LogLevel).ExpectAsync(2, TimeSpan.FromMilliseconds(50), () => { LogMessage("whatever"); }); - }); - Assert.Contains("timeout", exception.Message, StringComparison.OrdinalIgnoreCase); + }).Should().ThrowAsync().WithMessage("timeout"); } [Fact] - public void Should_log_when_not_muting() + public async Task Should_log_when_not_muting() { const string message = "This should end up in the log since it's not filtered"; LogMessage(message); - ExpectMsg( msg => (string)msg.Message == message); + await ExpectMsgAsync( msg => (string)msg.Message == message); } - - // ReSharper restore ConvertToLambdaExpression - } } diff --git a/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase_Instances.cs b/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase_Instances.cs index f903b5fec11..cf72afc95f4 100644 --- a/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase_Instances.cs +++ b/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase_Instances.cs @@ -6,6 +6,7 @@ //----------------------------------------------------------------------- using Akka.Event; +using Akka.TestKit.Tests.TestEventListenerTests; namespace Akka.TestKit.Tests.Xunit2.TestEventListenerTests { diff --git a/src/core/Akka.TestKit.Tests/TestEventListenerTests/ConfigTests.cs b/src/core/Akka.TestKit.Tests/TestEventListenerTests/ConfigTests.cs index 1323943235a..d200cca86e0 100644 --- a/src/core/Akka.TestKit.Tests/TestEventListenerTests/ConfigTests.cs +++ b/src/core/Akka.TestKit.Tests/TestEventListenerTests/ConfigTests.cs @@ -8,7 +8,7 @@ using System.Linq; using Xunit; -namespace Akka.TestKit.Tests.Xunit2.TestEventListenerTests +namespace Akka.TestKit.Tests.TestEventListenerTests { public class ConfigTests : TestKit.Xunit2.TestKit { diff --git a/src/core/Akka.TestKit.Tests/TestEventListenerTests/CustomEventFilterTests.cs b/src/core/Akka.TestKit.Tests/TestEventListenerTests/CustomEventFilterTests.cs index 20d69a67b31..fe3fc8d4c06 100644 --- a/src/core/Akka.TestKit.Tests/TestEventListenerTests/CustomEventFilterTests.cs +++ b/src/core/Akka.TestKit.Tests/TestEventListenerTests/CustomEventFilterTests.cs @@ -5,15 +5,15 @@ // //----------------------------------------------------------------------- +using System.Threading.Tasks; using Akka.Event; using Xunit; -namespace Akka.TestKit.Tests.Xunit2.TestEventListenerTests +namespace Akka.TestKit.Tests.TestEventListenerTests { public abstract class CustomEventFilterTestsBase : EventFilterTestBase { - // ReSharper disable ConvertToLambdaExpression - public CustomEventFilterTestsBase() : base("akka.loglevel=ERROR") { } + protected CustomEventFilterTestsBase() : base("akka.loglevel=ERROR") { } protected override void SendRawLogEventMessage(object message) { @@ -23,25 +23,26 @@ protected override void SendRawLogEventMessage(object message) protected abstract EventFilterFactory CreateTestingEventFilter(); [Fact] - public void Custom_filter_should_match() + public async Task Custom_filter_should_match() { var eventFilter = CreateTestingEventFilter(); - eventFilter.Custom(logEvent => logEvent is Error && (string) logEvent.Message == "whatever").ExpectOne(() => - { - Log.Error("whatever"); - }); + await eventFilter.Custom(logEvent => logEvent is Error && (string) logEvent.Message == "whatever") + .ExpectOneAsync(() => + { + Log.Error("whatever"); + }); } [Fact] - public void Custom_filter_should_match2() + public async Task Custom_filter_should_match2() { var eventFilter = CreateTestingEventFilter(); - eventFilter.Custom(logEvent => (string)logEvent.Message == "whatever").ExpectOne(() => - { - Log.Error("whatever"); - }); + await eventFilter.Custom(logEvent => (string)logEvent.Message == "whatever") + .ExpectOneAsync(() => + { + Log.Error("whatever"); + }); } - // ReSharper restore ConvertToLambdaExpression } public class CustomEventFilterTests : CustomEventFilterTestsBase diff --git a/src/core/Akka.TestKit.Tests/TestEventListenerTests/DeadLettersEventFilterTests.cs b/src/core/Akka.TestKit.Tests/TestEventListenerTests/DeadLettersEventFilterTests.cs index 8f9bce744a5..3c9a15d2db7 100644 --- a/src/core/Akka.TestKit.Tests/TestEventListenerTests/DeadLettersEventFilterTests.cs +++ b/src/core/Akka.TestKit.Tests/TestEventListenerTests/DeadLettersEventFilterTests.cs @@ -5,24 +5,30 @@ // //----------------------------------------------------------------------- +using System.Threading.Tasks; using Akka.Actor; using Akka.Event; using Akka.TestKit.TestActors; using Xunit; -namespace Akka.TestKit.Tests.Xunit2.TestEventListenerTests +namespace Akka.TestKit.Tests.TestEventListenerTests { public abstract class DeadLettersEventFilterTestsBase : EventFilterTestBase { private readonly IActorRef _deadActor; - // ReSharper disable ConvertToLambdaExpression protected DeadLettersEventFilterTestsBase() : base("akka.loglevel=ERROR") { _deadActor = Sys.ActorOf(BlackHoleActor.Props, "dead-actor"); + } + + public override async Task InitializeAsync() + { + await base.InitializeAsync(); + Watch(_deadActor); Sys.Stop(_deadActor); - ExpectTerminated(_deadActor); + await ExpectTerminatedAsync(_deadActor); } protected override void SendRawLogEventMessage(object message) @@ -33,17 +39,14 @@ protected override void SendRawLogEventMessage(object message) protected abstract EventFilterFactory CreateTestingEventFilter(); [Fact] - public void Should_be_able_to_filter_dead_letters() + public async Task Should_be_able_to_filter_dead_letters() { var eventFilter = CreateTestingEventFilter(); - eventFilter.DeadLetter().ExpectOne(() => + await eventFilter.DeadLetter().ExpectOneAsync(() => { _deadActor.Tell("whatever"); }); } - - - // ReSharper restore ConvertToLambdaExpression } public class DeadLettersEventFilterTests : DeadLettersEventFilterTestsBase diff --git a/src/core/Akka.TestKit.Tests/TestEventListenerTests/EventFilterTestBase.cs b/src/core/Akka.TestKit.Tests/TestEventListenerTests/EventFilterTestBase.cs index 7700a5c9d21..c90205ec5ea 100644 --- a/src/core/Akka.TestKit.Tests/TestEventListenerTests/EventFilterTestBase.cs +++ b/src/core/Akka.TestKit.Tests/TestEventListenerTests/EventFilterTestBase.cs @@ -7,9 +7,8 @@ using System.Threading.Tasks; using Akka.Event; -using Akka.Testkit.Tests.TestEventListenerTests; -namespace Akka.TestKit.Tests.Xunit2.TestEventListenerTests +namespace Akka.TestKit.Tests.TestEventListenerTests { public abstract class EventFilterTestBase : TestKit.Xunit2.TestKit { @@ -20,35 +19,39 @@ public abstract class EventFilterTestBase : TestKit.Xunit2.TestKit protected EventFilterTestBase(string config) : base(@"akka.loggers = [""" + typeof(ForwardAllEventsTestEventListener).AssemblyQualifiedName + @"""], " + (string.IsNullOrEmpty(config) ? "" : config)) + { + } + + public override async Task InitializeAsync() { //We send a ForwardAllEventsTo containing message to the TestEventListenerToForwarder logger (configured as a logger above). //It should respond with an "OK" message when it has received the message. var initLoggerMessage = new ForwardAllEventsTestEventListener.ForwardAllEventsTo(TestActor); - // ReSharper disable once DoNotCallOverridableMethodsInConstructor + SendRawLogEventMessage(initLoggerMessage); - ExpectMsg("OK"); + await ExpectMsgAsync("OK"); //From now on we know that all messages will be forwarded to TestActor } protected abstract void SendRawLogEventMessage(object message); - protected override Task AfterAllAsync() + protected override async Task AfterAllAsync() { //After every test we make sure no uncatched messages have been logged if(TestSuccessful) { - EnsureNoMoreLoggedMessages(); + await EnsureNoMoreLoggedMessages(); } - return base.AfterAllAsync(); + await base.AfterAllAsync(); } - private void EnsureNoMoreLoggedMessages() + private async Task EnsureNoMoreLoggedMessages() { //We log a Finished message. When it arrives to TestActor we know no other message has been logged. //If we receive something else it means another message was logged, and ExpectMsg will fail const string message = "<>"; SendRawLogEventMessage(message); - ExpectMsg(err => (string) err.Message == message,hint: "message to be \"" + message + "\""); + await ExpectMsgAsync(err => (string) err.Message == message,hint: "message to be \"" + message + "\""); } } diff --git a/src/core/Akka.TestKit.Tests/TestEventListenerTests/ExceptionEventFilterTests.cs b/src/core/Akka.TestKit.Tests/TestEventListenerTests/ExceptionEventFilterTests.cs index 13544350e5a..ff0a372d2bc 100644 --- a/src/core/Akka.TestKit.Tests/TestEventListenerTests/ExceptionEventFilterTests.cs +++ b/src/core/Akka.TestKit.Tests/TestEventListenerTests/ExceptionEventFilterTests.cs @@ -6,14 +6,15 @@ //----------------------------------------------------------------------- using System; +using System.Threading.Tasks; using Akka.Actor; using Akka.Event; -using Akka.TestKit; -using Akka.TestKit.Tests.Xunit2.TestEventListenerTests; +using FluentAssertions; using Xunit; using Xunit.Sdk; +using static FluentAssertions.FluentActions; -namespace Akka.Testkit.Tests.TestEventListenerTests +namespace Akka.TestKit.Tests.TestEventListenerTests { public class ExceptionEventFilterTests : EventFilterTestBase { @@ -29,117 +30,116 @@ protected override void SendRawLogEventMessage(object message) } [Fact] - public void SingleExceptionIsIntercepted() + public async Task SingleExceptionIsIntercepted() { - EventFilter.Exception() - .ExpectOne(() => Log.Error(new SomeException(), "whatever")); - ExpectNoMsg(TimeSpan.FromMilliseconds(100)); + await EventFilter.Exception() + .ExpectOneAsync(() => Log.Error(new SomeException(), "whatever")); + await ExpectNoMsgAsync(TimeSpan.FromMilliseconds(100)); } [Fact] - public void CanInterceptMessagesWhenStartIsSpecified() + public async Task CanInterceptMessagesWhenStartIsSpecified() { - EventFilter.Exception(start: "what") - .ExpectOne(() => Log.Error(new SomeException(), "whatever")); - ExpectNoMsg(TimeSpan.FromMilliseconds(100)); + await EventFilter.Exception(start: "what") + .ExpectOneAsync(() => Log.Error(new SomeException(), "whatever")); + await ExpectNoMsgAsync(TimeSpan.FromMilliseconds(100)); } [Fact] - public void DoNotInterceptMessagesWhenStartDoesNotMatch() + public async Task DoNotInterceptMessagesWhenStartDoesNotMatch() { EventFilter.Exception(start: "this is clearly not in message"); Log.Error(new SomeException(), "whatever"); - ExpectMsg(err => (string)err.Message == "whatever"); + await ExpectMsgAsync(err => (string)err.Message == "whatever"); } [Fact] - public void CanInterceptMessagesWhenMessageIsSpecified() + public async Task CanInterceptMessagesWhenMessageIsSpecified() { - EventFilter.Exception(message: "whatever") - .ExpectOne(() => Log.Error(new SomeException(), "whatever")); - ExpectNoMsg(TimeSpan.FromMilliseconds(100)); + await EventFilter.Exception(message: "whatever") + .ExpectOneAsync(() => Log.Error(new SomeException(), "whatever")); + await ExpectNoMsgAsync(TimeSpan.FromMilliseconds(100)); } [Fact] - public void DoNotInterceptMessagesWhenMessageDoesNotMatch() + public async Task DoNotInterceptMessagesWhenMessageDoesNotMatch() { EventFilter.Exception(message: "this is clearly not the message"); Log.Error(new SomeException(), "whatever"); - ExpectMsg(err => (string)err.Message == "whatever"); + await ExpectMsgAsync(err => (string)err.Message == "whatever"); } [Fact] - public void CanInterceptMessagesWhenContainsIsSpecified() + public async Task CanInterceptMessagesWhenContainsIsSpecified() { - EventFilter.Exception(contains: "ate") - .ExpectOne(() => Log.Error(new SomeException(), "whatever")); - ExpectNoMsg(TimeSpan.FromMilliseconds(100)); + await EventFilter.Exception(contains: "ate") + .ExpectOneAsync(() => Log.Error(new SomeException(), "whatever")); + await ExpectNoMsgAsync(TimeSpan.FromMilliseconds(100)); } [Fact] - public void DoNotInterceptMessagesWhenContainsDoesNotMatch() + public async Task DoNotInterceptMessagesWhenContainsDoesNotMatch() { EventFilter.Exception(contains: "this is clearly not in the message"); Log.Error(new SomeException(), "whatever"); - ExpectMsg(err => (string)err.Message == "whatever"); + await ExpectMsgAsync(err => (string)err.Message == "whatever"); } [Fact] - public void CanInterceptMessagesWhenSourceIsSpecified() + public async Task CanInterceptMessagesWhenSourceIsSpecified() { - EventFilter.Exception(source: LogSource.Create(this, Sys).Source) - .ExpectOne(() => - { - Log.Error(new SomeException(), "whatever"); - }); - ExpectNoMsg(TimeSpan.FromMilliseconds(100)); + await EventFilter.Exception(source: LogSource.Create(this, Sys).Source) + .ExpectOneAsync(() => Log.Error(new SomeException(), "whatever")); + await ExpectNoMsgAsync(TimeSpan.FromMilliseconds(100)); } [Fact] - public void DoNotInterceptMessagesWhenSourceDoesNotMatch() + public async Task DoNotInterceptMessagesWhenSourceDoesNotMatch() { EventFilter.Exception(source: "this is clearly not the source"); Log.Error(new SomeException(), "whatever"); - ExpectMsg(err => (string)err.Message == "whatever"); + await ExpectMsgAsync(err => (string)err.Message == "whatever"); } [Fact] - public void SpecifiedNumbersOfExceptionsCanBeIntercepted() + public async Task SpecifiedNumbersOfExceptionsCanBeIntercepted() { - EventFilter.Exception() - .Expect(2, () => + await EventFilter.Exception() + .ExpectAsync(2, () => { Log.Error(new SomeException(), "whatever"); Log.Error(new SomeException(), "whatever"); }); - ExpectNoMsg(TimeSpan.FromMilliseconds(100)); + await ExpectNoMsgAsync(TimeSpan.FromMilliseconds(100)); } [Fact] - public void ShouldFailIfMoreExceptionsThenSpecifiedAreLogged() + public async Task ShouldFailIfMoreExceptionsThenSpecifiedAreLogged() { - var exception = XAssert.Throws(() => - EventFilter.Exception().Expect(2, () => + await Awaiting(async () => { - Log.Error(new SomeException(), "whatever"); - Log.Error(new SomeException(), "whatever"); - Log.Error(new SomeException(), "whatever"); - })); - Assert.Contains("1 message too many", exception.Message, StringComparison.OrdinalIgnoreCase); + await EventFilter.Exception().ExpectAsync(2, () => + { + Log.Error(new SomeException(), "whatever"); + Log.Error(new SomeException(), "whatever"); + Log.Error(new SomeException(), "whatever"); + }); + }) + .Should().ThrowAsync().WithMessage("Received 1 message too many.*"); } [Fact] - public void ShouldReportCorrectMessageCount() + public async Task ShouldReportCorrectMessageCount() { var toSend = "Eric Cartman"; var actor = ActorOf( ExceptionTestActor.Props() ); - EventFilter + await EventFilter .Exception(source: actor.Path.ToString()) // expecting 2 because the same exception is logged in PostRestart - .Expect(2, () => actor.Tell( toSend )); + .ExpectAsync(2, () => actor.Tell( toSend )); } internal sealed class ExceptionTestActor : UntypedActor @@ -156,7 +156,7 @@ protected override void OnReceive( object message ) { switch (message) { - case string msg: + case string _: throw new InvalidOperationException( "I'm sailing away. Set an open course" ); default: diff --git a/src/core/Akka.TestKit.Tests/TestEventListenerTests/ForwardAllEventsTestEventListener.cs b/src/core/Akka.TestKit.Tests/TestEventListenerTests/ForwardAllEventsTestEventListener.cs index c0cb6d0006f..562bec50268 100644 --- a/src/core/Akka.TestKit.Tests/TestEventListenerTests/ForwardAllEventsTestEventListener.cs +++ b/src/core/Akka.TestKit.Tests/TestEventListenerTests/ForwardAllEventsTestEventListener.cs @@ -7,9 +7,8 @@ using Akka.Actor; using Akka.Event; -using Akka.TestKit; -namespace Akka.Testkit.Tests.TestEventListenerTests +namespace Akka.TestKit.Tests.TestEventListenerTests { public class ForwardAllEventsTestEventListener : TestEventListener { @@ -17,9 +16,9 @@ public class ForwardAllEventsTestEventListener : TestEventListener protected override void Print(LogEvent m) { - if(m.Message is ForwardAllEventsTo) + if(m.Message is ForwardAllEventsTo to) { - _forwarder = ((ForwardAllEventsTo)m.Message).Forwarder; + _forwarder = to.Forwarder; _forwarder.Tell("OK"); } else if(_forwarder != null) @@ -34,14 +33,12 @@ protected override void Print(LogEvent m) public class ForwardAllEventsTo { - private readonly IActorRef _forwarder; - public ForwardAllEventsTo(IActorRef forwarder) { - _forwarder = forwarder; + Forwarder = forwarder; } - public IActorRef Forwarder { get { return _forwarder; } } + public IActorRef Forwarder { get; } } } From aeb4a0cc7312e7451e2dd73e72e14aa3070e64ba Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Sat, 23 Apr 2022 03:06:39 +0700 Subject: [PATCH 2/2] Fix namespace and exception message pattern matching --- .../TestEventListenerTests/AllTestForEventFilterBase.cs | 2 +- .../AllTestForEventFilterBase_Instances.cs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase.cs b/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase.cs index e97ec586e56..77a7ce86b63 100644 --- a/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase.cs +++ b/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase.cs @@ -264,7 +264,7 @@ await _testingEventFilter.ForLogLevel(LogLevel).ExpectAsync(2, TimeSpan.FromMill { LogMessage("whatever"); }); - }).Should().ThrowAsync().WithMessage("timeout"); + }).Should().ThrowAsync().WithMessage("Timeout (*"); } [Fact] diff --git a/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase_Instances.cs b/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase_Instances.cs index cf72afc95f4..7a2f04040d7 100644 --- a/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase_Instances.cs +++ b/src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase_Instances.cs @@ -6,9 +6,8 @@ //----------------------------------------------------------------------- using Akka.Event; -using Akka.TestKit.Tests.TestEventListenerTests; -namespace Akka.TestKit.Tests.Xunit2.TestEventListenerTests +namespace Akka.TestKit.Tests.TestEventListenerTests { public class EventFilterDebugTests : AllTestForEventFilterBase {