Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Akka.TestKit.Tests.TestEventListenerTests to async #5872

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

namespace Akka.TestKit.Tests.TestEventListenerTests
{
public abstract class AllTestForEventFilterBase<TLogEvent> : EventFilterTestBase where TLogEvent : LogEvent
{
// ReSharper disable ConvertToLambdaExpression
private readonly EventFilterFactory _testingEventFilter;

protected AllTestForEventFilterBase(string config)
: base(config)
{
LogLevel = Logging.LogLevelFor<TLogEvent>();
// 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)
Expand All @@ -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<TLogEvent>(err => (string)err.Message == "let-me-thru");
await ExpectMsgAsync<TLogEvent>(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<TLogEvent>(err => (string)err.Message == "let-me-thru");
await ExpectMsgAsync<TLogEvent>(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");
Expand All @@ -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<TLogEvent>(err => err.LogSource == "let-me-thru");
await ExpectMsgAsync<TLogEvent>(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");
Expand All @@ -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<Exception>("Expected 0 events");
}).Should().ThrowAsync<Exception>("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<Exception>("Expected 0 errors logged, but there are error logs");
}

/// issue: InternalExpectAsync does not await actionAsync() - causing actionAsync to run as a detached task #5537
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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<TLogEvent>(m => (string) m.Message == "Message 2");
.ExpectAsync(2,() =>
{
LogMessage("Message 1");
LogMessage("Message 2");
LogMessage("Message 3");

});
await ExpectMsgAsync<TLogEvent>(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<TrueException>(() =>
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<TrueException>().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<TLogEvent>( msg => (string)msg.Message == message);
await ExpectMsgAsync<TLogEvent>( msg => (string)msg.Message == message);
}

// ReSharper restore ConvertToLambdaExpression

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

using Akka.Event;

namespace Akka.TestKit.Tests.Xunit2.TestEventListenerTests
namespace Akka.TestKit.Tests.TestEventListenerTests
{
public class EventFilterDebugTests : AllTestForEventFilterBase<Debug>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
// </copyright>
//-----------------------------------------------------------------------

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)
{
Expand All @@ -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<Error>(logEvent => (string)logEvent.Message == "whatever").ExpectOne(() =>
{
Log.Error("whatever");
});
await eventFilter.Custom<Error>(logEvent => (string)logEvent.Message == "whatever")
.ExpectOneAsync(() =>
{
Log.Error("whatever");
});
}
// ReSharper restore ConvertToLambdaExpression
}

public class CustomEventFilterTests : CustomEventFilterTestsBase
Expand Down
Loading