Skip to content

Commit

Permalink
[TestKit.Tests] Fix nullable warning code and modernize codes (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkatufus authored Dec 23, 2022
1 parent 57f1e23 commit 0754619
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 42 deletions.
5 changes: 2 additions & 3 deletions src/Akka.Hosting.TestKit.Tests/TestActorRefTests/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
// </copyright>
//-----------------------------------------------------------------------

using System;
using Akka.Actor;
using Akka.TestKit;

namespace Akka.Hosting.TestKit.Tests.TestActorRefTests;

public class ReplyActor : TActorBase
{
private IActorRef _replyTo;
private IActorRef? _replyTo;

protected override bool ReceiveMessage(object message)
{
Expand All @@ -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":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<WorkerActor>(Sys, Props.Create<WorkerActor>(), 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<WrappedTerminated>(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<WrappedTerminated>(w => w.Terminated.ActorRef == a, TimeSpan.FromSeconds(10), $"that the terminated actor was the one killed, i.e. {a.Path}");
probe.ExpectTerminated(actorRef);
AssertThread();
}

Expand Down Expand Up @@ -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<WorkerActor>(Sys, Props.Create<WorkerActor>());
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<WorkerActor>(Sys, Props.Create<WorkerActor>());
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]
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace Akka.Hosting.TestKit.Tests.TestEventListenerTests
public abstract class AllTestForEventFilterBase<TLogEvent> : 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)
{
}
Expand All @@ -31,7 +31,7 @@ protected override async Task BeforeTestStart()
{
await base.BeforeTestStart();
LogLevel = Event.Logging.LogLevelFor<TLogEvent>();
// ReSharper disable once VirtualMemberCallInContructor
// ReSharper disable once VirtualMemberCallInConstructor
_testingEventFilter = CreateTestingEventFilter();
}

Expand All @@ -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;
}
Expand All @@ -61,13 +64,19 @@ 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;
}

[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");
Expand All @@ -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;
}
Expand All @@ -99,13 +111,19 @@ 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;
}

[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");
Expand All @@ -119,13 +137,19 @@ 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;
}

[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");
Expand All @@ -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");
Expand All @@ -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 () =>
Expand All @@ -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<FalseException>(async () =>
{
await _testingEventFilter.ForLogLevel(LogLevel).ExpectAsync(0, actionAsync: async () =>
Expand All @@ -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<FalseException>(async () =>
{
await _testingEventFilter.ForLogLevel(LogLevel).ExpectAsync(0, async () =>
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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")
Expand All @@ -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), () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class EventFilterTestBase : TestKit
/// </summary>
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" })
);
}

Expand Down Expand Up @@ -84,7 +84,7 @@ private class TimerTestFsm : FSM<int, object>
{
public TimerTestFsm()
{
StartWith(1, null);
StartWith(1, "");
When(1, e => Stay());
}
}
Expand Down
Loading

0 comments on commit 0754619

Please sign in to comment.