diff --git a/src/Akka.Hosting.TestKit.Tests/TestActorRefTests/Logger.cs b/src/Akka.Hosting.TestKit.Tests/TestActorRefTests/Logger.cs index 33eb78e..b86eaab 100644 --- a/src/Akka.Hosting.TestKit.Tests/TestActorRefTests/Logger.cs +++ b/src/Akka.Hosting.TestKit.Tests/TestActorRefTests/Logger.cs @@ -13,11 +13,10 @@ namespace Akka.Hosting.TestKit.Tests.TestActorRefTests; public class Logger : ActorBase { private int _count; - private string _msg; + private string? _msg; protected override bool Receive(object message) { - var warning = message as Warning; - if(warning != null && warning.Message is string) + if(message is Warning { Message: string } warning) { _count++; _msg = (string)warning.Message; diff --git a/src/Akka.Hosting.TestKit.Tests/TestActorRefTests/ReplyActor.cs b/src/Akka.Hosting.TestKit.Tests/TestActorRefTests/ReplyActor.cs index e2c111f..57d8e1d 100644 --- a/src/Akka.Hosting.TestKit.Tests/TestActorRefTests/ReplyActor.cs +++ b/src/Akka.Hosting.TestKit.Tests/TestActorRefTests/ReplyActor.cs @@ -5,6 +5,7 @@ // //----------------------------------------------------------------------- +using System; using Akka.Actor; using Akka.TestKit; @@ -12,7 +13,7 @@ namespace Akka.Hosting.TestKit.Tests.TestActorRefTests; public class ReplyActor : TActorBase { - private IActorRef _replyTo; + private IActorRef? _replyTo; protected override bool ReceiveMessage(object message) { @@ -29,6 +30,9 @@ protected override bool ReceiveMessage(object message) worker2.Tell(Sender, Self); return true; case "workDone": + if (_replyTo is null) + throw new NullReferenceException("_replyTo is null, make sure that \"complexRequest\" is sent first"); + _replyTo.Tell("complexReply", Self); return true; case "simpleRequest": diff --git a/src/Akka.Hosting.TestKit.Tests/TestActorRefTests/TestActorRefSpec.cs b/src/Akka.Hosting.TestKit.Tests/TestActorRefTests/TestActorRefSpec.cs index 4df61d5..1b6cd5f 100644 --- a/src/Akka.Hosting.TestKit.Tests/TestActorRefTests/TestActorRefSpec.cs +++ b/src/Akka.Hosting.TestKit.Tests/TestActorRefTests/TestActorRefSpec.cs @@ -22,8 +22,7 @@ public class TestActorRefSpec : TestKit { public static int Counter = 4; public static readonly Thread Thread = Thread.CurrentThread; - public static Thread OtherThread; - + public static Thread? OtherThread; public TestActorRefSpec() { @@ -110,13 +109,16 @@ public void TestActorRef_must_support_reply_via_sender() [Fact] public void TestActorRef_must_stop_when_sent_a_PoisonPill() { - //TODO: Should have this surrounding all code EventFilter[ActorKilledException]() intercept { + //TODO: Should have this surrounding all code EventFilter[ActorKilledException]() intercept { + var probe = CreateTestProbe(); var a = new TestActorRef(Sys, Props.Create(), null, "will-be-killed"); + var actorRef = (InternalTestActorRef)a.Ref; + probe.Watch(actorRef); Sys.ActorOf(Props.Create(() => new WatchAndForwardActor(a, TestActor)), "forwarder"); + a.Tell(PoisonPill.Instance); - ExpectMsg(w => w.Terminated.ActorRef == a, TimeSpan.FromSeconds(10), string.Format("that the terminated actor was the one killed, i.e. {0}", a.Path)); - var actorRef = (InternalTestActorRef)a.Ref; - actorRef.IsTerminated.Should().Be(true); + ExpectMsg(w => w.Terminated.ActorRef == a, TimeSpan.FromSeconds(10), $"that the terminated actor was the one killed, i.e. {a.Path}"); + probe.ExpectTerminated(actorRef); AssertThread(); } @@ -178,19 +180,22 @@ public void TestActorRef_must_allow_override_of_dispatcher() public void TestActorRef_must_proxy_receive_for_the_underlying_actor_without_sender() { var a = new TestActorRef(Sys, Props.Create()); - a.Receive("work"); var actorRef = (InternalTestActorRef)a.Ref; - Assert.True(actorRef.IsTerminated); + Watch(actorRef); + a.Receive("work"); + ExpectTerminated(actorRef); } [Fact] public void TestActorRef_must_proxy_receive_for_the_underlying_actor_with_sender() { var a = new TestActorRef(Sys, Props.Create()); - a.Receive("work", TestActor); //This will stop the actor + var probe = CreateTestProbe(); var actorRef = (InternalTestActorRef)a.Ref; - Assert.True(actorRef.IsTerminated); + probe.Watch(actorRef); + a.Receive("work", TestActor); //This will stop the actor ExpectMsg("workDone"); + probe.ExpectTerminated(actorRef); } [Fact] @@ -221,7 +226,7 @@ public void BugFix1709_TestFsmActorRef_must_work_with_Fsms_with_constructor_argu private class SaveStringActor : TActorBase { - public string ReceivedString { get; private set; } + public string? ReceivedString { get; private set; } protected override bool ReceiveMessage(object message) { diff --git a/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase.cs b/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase.cs index dd4878c..843c918 100644 --- a/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase.cs +++ b/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase.cs @@ -20,9 +20,9 @@ namespace Akka.Hosting.TestKit.Tests.TestEventListenerTests public abstract class AllTestForEventFilterBase : EventFilterTestBase where TLogEvent : LogEvent { // ReSharper disable ConvertToLambdaExpression - private EventFilterFactory _testingEventFilter; + private EventFilterFactory? _testingEventFilter; - protected AllTestForEventFilterBase(LogLevel logLevel, ITestOutputHelper output = null) + protected AllTestForEventFilterBase(LogLevel logLevel, ITestOutputHelper? output = null) : base(logLevel, output) { } @@ -31,7 +31,7 @@ protected override async Task BeforeTestStart() { await base.BeforeTestStart(); LogLevel = Event.Logging.LogLevelFor(); - // ReSharper disable once VirtualMemberCallInContructor + // ReSharper disable once VirtualMemberCallInConstructor _testingEventFilter = CreateTestingEventFilter(); } @@ -53,6 +53,9 @@ protected override void SendRawLogEventMessage(object message) [Fact] public void Single_message_is_intercepted() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + _testingEventFilter.ForLogLevel(LogLevel).ExpectOne(() => LogMessage("whatever")); TestSuccessful = true; } @@ -61,6 +64,9 @@ public void Single_message_is_intercepted() [Fact] public void Can_intercept_messages_when_start_is_specified() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + _testingEventFilter.ForLogLevel(LogLevel, start: "what").ExpectOne(() => LogMessage("whatever")); TestSuccessful = true; } @@ -68,6 +74,9 @@ public void Can_intercept_messages_when_start_is_specified() [Fact] public void Do_not_intercept_messages_when_start_does_not_match() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + _testingEventFilter.ForLogLevel(LogLevel, start: "what").ExpectOne(() => { LogMessage("let-me-thru"); @@ -80,6 +89,9 @@ public void Do_not_intercept_messages_when_start_does_not_match() [Fact] public void Can_intercept_messages_when_message_is_specified() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + _testingEventFilter.ForLogLevel(LogLevel, message: "whatever").ExpectOne(() => LogMessage("whatever")); TestSuccessful = true; } @@ -99,6 +111,9 @@ public void Do_not_intercept_messages_when_message_does_not_match() [Fact] public void Can_intercept_messages_when_contains_is_specified() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + _testingEventFilter.ForLogLevel(LogLevel, contains: "ate").ExpectOne(() => LogMessage("whatever")); TestSuccessful = true; } @@ -106,6 +121,9 @@ public void Can_intercept_messages_when_contains_is_specified() [Fact] public void Do_not_intercept_messages_when_contains_does_not_match() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + _testingEventFilter.ForLogLevel(LogLevel, contains: "eve").ExpectOne(() => { LogMessage("let-me-thru"); @@ -119,6 +137,9 @@ public void Do_not_intercept_messages_when_contains_does_not_match() [Fact] public void Can_intercept_messages_when_source_is_specified() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + _testingEventFilter.ForLogLevel(LogLevel, source: LogSource.FromType(GetType(), Sys)).ExpectOne(() => LogMessage("whatever")); TestSuccessful = true; } @@ -126,6 +147,9 @@ public void Can_intercept_messages_when_source_is_specified() [Fact] public void Do_not_intercept_messages_when_source_does_not_match() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + _testingEventFilter.ForLogLevel(LogLevel, source: "expected-source").ExpectOne(() => { PublishMessage("message", source: "expected-source"); @@ -136,8 +160,11 @@ public void Do_not_intercept_messages_when_source_does_not_match() } [Fact] - public void Specified_numbers_of_messagesan_be_intercepted() + public void Specified_numbers_of_messages_and_be_intercepted() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + _testingEventFilter.ForLogLevel(LogLevel).Expect(2, () => { LogMessage("whatever"); @@ -161,7 +188,7 @@ public void Expect_0_events_Should_work() [Fact] public async Task ExpectAsync_0_events_Should_work() { - Exception ex = null; + Exception? ex = null; try { await EventFilter.Error().ExpectAsync(0, async () => @@ -182,6 +209,9 @@ await EventFilter.Error().ExpectAsync(0, async () => [Fact] public async Task ExpectAsync_should_await_actionAsync() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + await Assert.ThrowsAnyAsync(async () => { await _testingEventFilter.ForLogLevel(LogLevel).ExpectAsync(0, actionAsync: async () => @@ -196,6 +226,9 @@ await _testingEventFilter.ForLogLevel(LogLevel).ExpectAsync(0, actionAsync: asyn [Fact] public async Task InterceptAsync_should_await_func() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + await Assert.ThrowsAnyAsync(async () => { await _testingEventFilter.ForLogLevel(LogLevel).ExpectAsync(0, async () => @@ -209,6 +242,9 @@ await _testingEventFilter.ForLogLevel(LogLevel).ExpectAsync(0, async () => [Fact] public void Messages_can_be_muted() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + _testingEventFilter.ForLogLevel(LogLevel).Mute(() => { LogMessage("whatever"); @@ -221,6 +257,9 @@ public void Messages_can_be_muted() [Fact] public void Messages_can_be_muted_from_now_on() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + var unmutableFilter = _testingEventFilter.ForLogLevel(LogLevel).Mute(); LogMessage("whatever"); LogMessage("whatever"); @@ -231,6 +270,9 @@ public void Messages_can_be_muted_from_now_on() [Fact] public void Messages_can_be_muted_from_now_on_with_using() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + using(_testingEventFilter.ForLogLevel(LogLevel).Mute()) { LogMessage("whatever"); @@ -243,15 +285,21 @@ public void Messages_can_be_muted_from_now_on_with_using() [Fact] public void Make_sure_async_works() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + _testingEventFilter.ForLogLevel(LogLevel).Expect(1, TimeSpan.FromSeconds(2), () => { - Task.Delay(TimeSpan.FromMilliseconds(10)).ContinueWith(t => { LogMessage("whatever"); }); + Task.Delay(TimeSpan.FromMilliseconds(10)).ContinueWith(_ => { LogMessage("whatever"); }); }); } [Fact] public void Chain_many_filters() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + _testingEventFilter .ForLogLevel(LogLevel,message:"Message 1").And .ForLogLevel(LogLevel,message:"Message 3") @@ -269,6 +317,9 @@ public void Chain_many_filters() [Fact] public void Should_timeout_if_too_few_messages() { + if (_testingEventFilter is null) + throw new NullReferenceException("_testingEventFilter should not be null, check CreateTestingEventFilter implementation."); + Invoking(() => { _testingEventFilter.ForLogLevel(LogLevel).Expect(2, TimeSpan.FromMilliseconds(50), () => diff --git a/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/DeadLettersEventFilterTests.cs b/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/DeadLettersEventFilterTests.cs index 86f0416..2acc375 100644 --- a/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/DeadLettersEventFilterTests.cs +++ b/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/DeadLettersEventFilterTests.cs @@ -16,7 +16,7 @@ namespace Akka.Hosting.TestKit.Tests.TestEventListenerTests; public abstract class DeadLettersEventFilterTestsBase : EventFilterTestBase { - private IActorRef _deadActor; + private IActorRef? _deadActor; // ReSharper disable ConvertToLambdaExpression protected DeadLettersEventFilterTestsBase() : base(Event.LogLevel.ErrorLevel) diff --git a/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/EventFilterTestBase.cs b/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/EventFilterTestBase.cs index 4d1b1c7..75d2e48 100644 --- a/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/EventFilterTestBase.cs +++ b/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/EventFilterTestBase.cs @@ -21,7 +21,7 @@ public abstract class EventFilterTestBase : TestKit /// protected bool TestSuccessful; - protected EventFilterTestBase(LogLevel logLevel, ITestOutputHelper output = null) : base(output: output) + protected EventFilterTestBase(LogLevel logLevel, ITestOutputHelper? output = null) : base(output: output) { _logLevel = logLevel; } diff --git a/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/ForwardAllEventsTestEventListener.cs b/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/ForwardAllEventsTestEventListener.cs index 30644a6..d28e1e7 100644 --- a/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/ForwardAllEventsTestEventListener.cs +++ b/src/Akka.Hosting.TestKit.Tests/TestEventListenerTests/ForwardAllEventsTestEventListener.cs @@ -13,13 +13,13 @@ namespace Akka.Hosting.TestKit.Tests.TestEventListenerTests; public class ForwardAllEventsTestEventListener : TestEventListener { - private IActorRef _forwarder; + private IActorRef? _forwarder; 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,13 +34,11 @@ 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; } } } \ No newline at end of file diff --git a/src/Akka.Hosting.TestKit.Tests/TestFSMRefTests/TestFSMRefSpec.cs b/src/Akka.Hosting.TestKit.Tests/TestFSMRefTests/TestFSMRefSpec.cs index 65f9e86..30bab56 100644 --- a/src/Akka.Hosting.TestKit.Tests/TestFSMRefTests/TestFSMRefSpec.cs +++ b/src/Akka.Hosting.TestKit.Tests/TestFSMRefTests/TestFSMRefSpec.cs @@ -42,7 +42,7 @@ public void A_TestFSMRef_must_allow_access_to_internal_state() fsm.SetStateTimeout(TimeSpan.FromMilliseconds(100)); Within(TimeSpan.FromMilliseconds(80), TimeSpan.FromMilliseconds(500), () => - AwaitCondition(() => fsm.StateName == 2 && fsm.StateData == "timeout") + AwaitCondition(() => fsm is { StateName: 2, StateData: "timeout" }) ); } @@ -84,7 +84,7 @@ private class TimerTestFsm : FSM { public TimerTestFsm() { - StartWith(1, null); + StartWith(1, ""); When(1, e => Stay()); } } diff --git a/src/Akka.Hosting.TestKit.Tests/TestKitBaseTests/DilatedTests.cs b/src/Akka.Hosting.TestKit.Tests/TestKitBaseTests/DilatedTests.cs index fc9b4e5..b7012ad 100644 --- a/src/Akka.Hosting.TestKit.Tests/TestKitBaseTests/DilatedTests.cs +++ b/src/Akka.Hosting.TestKit.Tests/TestKitBaseTests/DilatedTests.cs @@ -7,7 +7,6 @@ using System; using System.Diagnostics; -using System.Threading.Tasks; using Akka.Configuration; using Xunit; using Xunit.Sdk; @@ -76,7 +75,7 @@ public void FishForMessage_should_dilate_timeout() AssertDilated(stopwatch.ElapsedMilliseconds, $"Expected the timeout to be {ExpectedTimeout} but in fact it was {stopwatch.ElapsedMilliseconds}."); } - private static void AssertDilated(double diff, string message = null) + private static void AssertDilated(double diff, string? message = null) { Assert.True(diff >= ExpectedTimeout - DiffDelta, message); Assert.True(diff < ExpectedTimeout + Margin, message); // margin for GC diff --git a/src/Akka.Hosting.TestKit.Tests/TestKitBaseTests/IgnoreMessagesTests.cs b/src/Akka.Hosting.TestKit.Tests/TestKitBaseTests/IgnoreMessagesTests.cs index 0a9d583..e4200aa 100644 --- a/src/Akka.Hosting.TestKit.Tests/TestKitBaseTests/IgnoreMessagesTests.cs +++ b/src/Akka.Hosting.TestKit.Tests/TestKitBaseTests/IgnoreMessagesTests.cs @@ -6,7 +6,6 @@ //----------------------------------------------------------------------- using System; -using System.Threading.Tasks; using Akka.Actor; using FluentAssertions; using Xunit; @@ -17,12 +16,12 @@ public class IgnoreMessagesTests : TestKit { public class IgnoredMessage { - public IgnoredMessage(string ignoreMe = null) + public IgnoredMessage(string? ignoreMe = null) { IgnoreMe = ignoreMe; } - public string IgnoreMe { get; } + public string? IgnoreMe { get; } } protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider) @@ -32,7 +31,7 @@ protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IService [Fact] public void IgnoreMessages_should_ignore_messages() { - IgnoreMessages(o => o is int && (int)o == 1); + IgnoreMessages(o => o is 1); TestActor.Tell(1); TestActor.Tell("1"); string.Equals((string)ReceiveOne(), "1").Should().BeTrue(); diff --git a/src/Akka.Hosting.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs b/src/Akka.Hosting.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs index 6a8612c..0ce3c6d 100644 --- a/src/Akka.Hosting.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs +++ b/src/Akka.Hosting.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs @@ -198,7 +198,7 @@ public void ReceiveWhile_Filter_should_break_on_function_returning_null_and_retu TestActor.Tell("3"); TestActor.Tell(99999.0); TestActor.Tell(4); - ReceiveWhile(_ => _ is double ? null : _.ToString()) + ReceiveWhile(_ => (_ is double ? null : _.ToString())!) .Should().BeEquivalentTo(new[] { "1", "2", "3" }, opt => opt.WithStrictOrdering()); } @@ -208,7 +208,7 @@ public void ReceiveWhile_Filter_should_not_consume_last_message_that_didnt_match TestActor.Tell("1"); TestActor.Tell("2"); TestActor.Tell(4711); - ReceiveWhile(_ => _ is string ? _ : null); + ReceiveWhile(_ => (_ is string ? _ : null)!); ExpectMsg(4711); } diff --git a/src/Akka.Hosting.TestKit.Tests/TestSchedulerTests.cs b/src/Akka.Hosting.TestKit.Tests/TestSchedulerTests.cs index 8a2df7a..ea71433 100644 --- a/src/Akka.Hosting.TestKit.Tests/TestSchedulerTests.cs +++ b/src/Akka.Hosting.TestKit.Tests/TestSchedulerTests.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using Akka.Actor; using Akka.Configuration; +using Akka.Event; using Akka.TestKit; using Akka.TestKit.Configs; using FluentAssertions; @@ -18,7 +19,7 @@ namespace Akka.Hosting.TestKit.Tests; public class TestSchedulerTests : TestKit { - private IActorRef _testReceiveActor; + private IActorRef? _testReceiveActor; protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider) { @@ -130,7 +131,7 @@ public void Advance_to_takes_us_to_correct_time() private class TestReceiveActor : ReceiveActor { - private Cancelable _cancelable; + private Cancelable? _cancelable; public TestReceiveActor() { @@ -152,6 +153,9 @@ public TestReceiveActor() Receive(_ => { + if (_cancelable is null) + throw new NullReferenceException("_cancelable is null, actor has not received any CancelableMessage message"); + _cancelable.Cancel(); });