Skip to content

Commit

Permalink
fix: fixed issues with test suite instability
Browse files Browse the repository at this point in the history
  • Loading branch information
philasmar committed Dec 14, 2024
1 parent d26cf94 commit 7eff662
Show file tree
Hide file tree
Showing 21 changed files with 242 additions and 207 deletions.
18 changes: 9 additions & 9 deletions test/AWS.Logger.AspNetCore.Tests/TestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ private void LoggingSetup(string configFileName, string configSectionInfoBlockNa
/// 10 debug messages to CloudWatchLogs. The results are then verified.
/// </summary>
[Fact]
public void ILogger()
public async Task ILogger()
{
LoggingSetup("appsettings.json",null);
SimpleLoggingTest(ConfigSection.Config.LogGroup);
await SimpleLoggingTest(ConfigSection.Config.LogGroup);
}

[Fact]
Expand Down Expand Up @@ -129,10 +129,10 @@ public async Task MultiThreadTestMock()
/// threads that log 200 debug messages each to CloudWatchLogs. The results are then verified.
/// </summary>
[Fact]
public void MultiThreadTest()
public async Task MultiThreadTest()
{
LoggingSetup("multiThreadTest.json",null);
MultiThreadTestGroup(ConfigSection.Config.LogGroup);
await MultiThreadTestGroup(ConfigSection.Config.LogGroup);
}

/// <summary>
Expand All @@ -143,17 +143,17 @@ public void MultiThreadTest()
/// inorder to force a buffer full scenario. The results are then verified.
/// </summary>
[Fact]
public void MultiThreadBufferFullTest()
public async Task MultiThreadBufferFullTest()
{
LoggingSetup("multiThreadBufferFullTest.json",null);
MultiThreadBufferFullTestGroup(ConfigSection.Config.LogGroup);
await MultiThreadBufferFullTestGroup(ConfigSection.Config.LogGroup);
}

[Fact]
public void OverrideLogStreamNameTest()
public async Task OverrideLogStreamNameTest()
{
LoggingSetup("overrideLogStreamName.json", null);
MultiThreadTestGroup(ConfigSection.Config.LogGroup, ConfigSection.Config.LogStreamName);
await MultiThreadTestGroup(ConfigSection.Config.LogGroup, ConfigSection.Config.LogStreamName);
}

/// <summary>
Expand All @@ -165,7 +165,7 @@ protected override void LogMessages(int count)
Logger.LogError(0, new Exception("Exception message."), "Error message");
for (int i = 0; i < count-2; i++)
{
Logger.LogDebug(string.Format("Test logging message {0} Ilogger, Thread Id:{1}", i, Thread.CurrentThread.ManagedThreadId));
Logger.LogDebug(string.Format("Test logging message {0} Ilogger, Thread Id:{1}", i, Environment.CurrentManagedThreadId));
}
Logger.LogDebug(LASTMESSAGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- A1 is set to be a ConsoleAppender -->
<appender name="MultiThreadBufferFullTest" type="AWS.Logger.Log4net.AWSAppender,AWS.Logger.Log4net" >

<LogGroup>AWSLog4NetGroupMultiThreadBufferFullTest</LogGroup>
<LogGroup>{LOG_GROUP_NAME}</LogGroup>
<Region>us-west-2</Region>
<MaxQueuedMessages>10</MaxQueuedMessages>

Expand Down
2 changes: 1 addition & 1 deletion test/AWS.Logger.Log4Net.Tests/MultiThreadTest.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- A1 is set to be a ConsoleAppender -->
<appender name="MultiThreadTest" type="AWS.Logger.Log4net.AWSAppender,AWS.Logger.Log4net" >

<LogGroup>AWSLog4NetGroupLog4NetMultiThreadTest</LogGroup>
<LogGroup>{LOG_GROUP_NAME}</LogGroup>
<Region>us-west-2</Region>

<layout type="log4net.Layout.PatternLayout">
Expand Down
2 changes: 1 addition & 1 deletion test/AWS.Logger.Log4Net.Tests/OverrideLogStreamName.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!-- A1 is set to be a ConsoleAppender -->
<appender name="OverrideLogStreamName" type="AWS.Logger.Log4net.AWSAppender,AWS.Logger.Log4net" >

<LogGroup>AWSLog4NetGroupOverrideLogStreamName</LogGroup>
<LogGroup>{LOG_GROUP_NAME}</LogGroup>
<LogStreamName>CustomStreamName</LogStreamName>
<Region>us-west-2</Region>
<LogStreamNameSuffix>Custom</LogStreamNameSuffix> <!-- should be ignored since LogStreamName is set -->
Expand Down
2 changes: 2 additions & 0 deletions test/AWS.Logger.Log4Net.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: Xunit.CollectionBehavior(DisableTestParallelization = true)]
46 changes: 30 additions & 16 deletions test/AWS.Logger.Log4Net.Tests/TestClass.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AWS.Logger.Log4net;
using AWS.Logger.TestUtils;
using log4net;
using log4net.Config;
Expand All @@ -12,13 +17,18 @@ public class Log4NetTestClass : BaseTestClass
{
public ILog Logger;

private void GetLog4NetLogger(string fileName, string logName)
private void GetLog4NetLogger(string fileName, string logName, string logGroupName)
{
// Create logger
var repositoryAssembly = typeof(Log4NetTestClass).GetTypeInfo().Assembly;
var loggerRepository = LogManager.GetRepository(repositoryAssembly);
XmlConfigurator.Configure(loggerRepository, new System.IO.FileInfo(fileName));
Logger = LogManager.GetLogger(repositoryAssembly, logName);
var fileInfo = new FileInfo(fileName);
var fileContent = File.ReadAllText(fileInfo.FullName);
using (Stream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(fileContent.Replace("{LOG_GROUP_NAME}", logGroupName))))
{
XmlConfigurator.Configure(loggerRepository, memoryStream);
Logger = LogManager.GetLogger(repositoryAssembly, logName);
}
}

public Log4NetTestClass(TestFixture testFixture) : base(testFixture)
Expand All @@ -27,42 +37,46 @@ public Log4NetTestClass(TestFixture testFixture) : base(testFixture)

#region Test Cases
[Fact]
public void Log4Net()
public async Task Log4Net()
{
GetLog4NetLogger("log4net.config","Log4Net");
SimpleLoggingTest("AWSLog4NetGroupLog4Net");
var logGroupName = $"AWSLog4NetGroupLog4Net{Guid.NewGuid().ToString().Split('-').Last()}";
GetLog4NetLogger("log4net.config","Log4Net", logGroupName);
await SimpleLoggingTest(logGroupName);
}

[Fact]
public void MultiThreadTest()
public async Task MultiThreadTest()
{
GetLog4NetLogger("MultiThreadTest.config", "MultiThreadTest");
MultiThreadTestGroup("AWSLog4NetGroupLog4NetMultiThreadTest");
var logGroupName = $"AWSLog4NetGroupLog4NetMultiThreadTest{Guid.NewGuid().ToString().Split('-').Last()}";
GetLog4NetLogger("MultiThreadTest.config", "MultiThreadTest", logGroupName);
await MultiThreadTestGroup(logGroupName);
}

[Fact]
public void MultiThreadBufferFullTest()
public async Task MultiThreadBufferFullTest()
{
GetLog4NetLogger("MultiThreadBufferFullTest.config", "MultiThreadBufferFullTest");
MultiThreadBufferFullTestGroup("AWSLog4NetGroupMultiThreadBufferFullTest");
var logGroupName = $"AWSLog4NetGroupMultiThreadBufferFullTest{Guid.NewGuid().ToString().Split('-').Last()}";
GetLog4NetLogger("MultiThreadBufferFullTest.config", "MultiThreadBufferFullTest", logGroupName);
await MultiThreadBufferFullTestGroup(logGroupName);
}

/// <summary>
/// Verifies that multiple producers can log to the same log stream
/// when an override log stream name is provided
/// </summary>
[Fact]
public void CustomLogStreamNameTest()
public async Task CustomLogStreamNameTest()
{
GetLog4NetLogger("OverrideLogStreamName.config", "OverrideLogStreamName");
MultiThreadTestGroup("AWSLog4NetGroupOverrideLogStreamName");
var logGroupName = $"AWSLog4NetGroupMultiThreadBufferFullTest{Guid.NewGuid().ToString().Split('-').Last()}";
GetLog4NetLogger("OverrideLogStreamName.config", "OverrideLogStreamName", logGroupName);
await MultiThreadTestGroup(logGroupName);
}

protected override void LogMessages(int count)
{
for (int i = 0; i < count-1; i++)
{
Logger.Debug(string.Format("Test logging message {0} Log4Net, Thread Id:{1}", i, Thread.CurrentThread.ManagedThreadId));
Logger.Debug(string.Format("Test logging message {0} Log4Net, Thread Id:{1}", i, Environment.CurrentManagedThreadId));
}
Logger.Debug(LASTMESSAGE);
}
Expand Down
2 changes: 1 addition & 1 deletion test/AWS.Logger.Log4Net.Tests/log4net.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!-- A1 is set to be a ConsoleAppender -->
<appender name="AWS" type="AWS.Logger.Log4net.AWSAppender,AWS.Logger.Log4net" >

<LogGroup>AWSLog4NetGroupLog4Net</LogGroup>
<LogGroup>{LOG_GROUP_NAME}</LogGroup>
<Region>us-west-2</Region>
<LogStreamNameSuffix>Custom</LogStreamNameSuffix>
<LogStreamNamePrefix>CustomPrefix</LogStreamNamePrefix>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<add assembly ="NLog.AWS.Logger"/>
</extensions>
<targets>
<target name="AWSNLogGroupEventSizeExceededTest" type="AWSTarget" logGroup="AWSNLogGroupEventSizeExceededTest" region="us-west-2" logStreamNameSuffix="Custom"/>
<target name="{LOG_GROUP_NAME}" type="AWSTarget" logGroup="{LOG_GROUP_NAME}" region="us-west-2" logStreamNameSuffix="Custom"/>
<target name="loggerRegular" xsi:type="Console" layout="${callsite} ${message}" />
</targets>
<rules>
<logger name="loggerRegularEventSizeExceeded" minlevel="Debug" writeTo="loggerRegular,AWSNLogGroupEventSizeExceededTest"/>
<logger name="loggerRegularEventSizeExceeded" minlevel="Debug" writeTo="loggerRegular,{LOG_GROUP_NAME}"/>
</rules>
</nlog>
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<add assembly ="NLog.AWS.Logger"/>
</extensions>
<targets>
<target name="AWSNLogGroupMultiThreadBufferFullTest" type="AWSTarget" logGroup="AWSNLogGroupMultiThreadBufferFullTest" region="us-west-2" MaxQueuedMessages="10"/>
<target name="{LOG_GROUP_NAME}" type="AWSTarget" logGroup="{LOG_GROUP_NAME}" region="us-west-2" MaxQueuedMessages="10"/>
<target name="loggerRegular" xsi:type="Console" layout="${callsite} ${message}" />
</targets>
<rules>
<logger name="loggerMultiThreadBufferFull" minlevel="Debug" writeTo="loggerRegular,AWSNLogGroupMultiThreadBufferFullTest" />
<logger name="loggerMultiThreadBufferFull" minlevel="Debug" writeTo="loggerRegular,{LOG_GROUP_NAME}" />
</rules>
</nlog>
4 changes: 2 additions & 2 deletions test/AWS.Logger.NLog.Tests/AWSNLogGroupMultiThreadTest.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<add assembly ="NLog.AWS.Logger"/>
</extensions>
<targets>
<target name="AWSNLogGroupMultiThreadTest" type="AWSTarget" logGroup="AWSNLogGroupMultiThreadTest" region="us-west-2"/>
<target name="{LOG_GROUP_NAME}" type="AWSTarget" logGroup="{LOG_GROUP_NAME}" region="us-west-2"/>
<target name="loggerRegular" xsi:type="Console" layout="${callsite} ${message}" />
</targets>
<rules>
<logger name="loggerMultiThread" minlevel="Debug" writeTo="loggerRegular,AWSNLogGroupMultiThreadTest" />
<logger name="loggerMultiThread" minlevel="Debug" writeTo="loggerRegular,{LOG_GROUP_NAME}" />
</rules>
</nlog>
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<add assembly ="NLog.AWS.Logger"/>
</extensions>
<targets>
<target name="AWSNLogOverrideLogStreamName" type="AWSTarget" logGroup="AWSNLogOverrideLogStreamName" region="us-west-2" logStreamNameSuffix="Custom" logStreamNamePrefix="CustomPrefix" logStreamName="CustomStreamName"/>
<target name="{LOG_GROUP_NAME}" type="AWSTarget" logGroup="{LOG_GROUP_NAME}" region="us-west-2" logStreamNameSuffix="Custom" logStreamNamePrefix="CustomPrefix" logStreamName="CustomStreamName"/>
<target name="overrideLogStreamName" xsi:type="Console" layout="${callsite} ${message}" />
</targets>
<rules>
<logger name="overrideLogStreamName" minlevel="Debug" writeTo="overrideLogStreamName,AWSNLogOverrideLogStreamName"/>
<logger name="overrideLogStreamName" minlevel="Debug" writeTo="overrideLogStreamName,{LOG_GROUP_NAME}"/>
</rules>
</nlog>
4 changes: 2 additions & 2 deletions test/AWS.Logger.NLog.Tests/Regular.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<add assembly ="NLog.AWS.Logger"/>
</extensions>
<targets>
<target name="AWSNLogGroup" type="AWSTarget" logGroup="AWSNLogGroup" region="us-west-2" logStreamNameSuffix="Custom" logStreamNamePrefix="CustomPrefix"/>
<target name="{LOG_GROUP_NAME}" type="AWSTarget" logGroup="{LOG_GROUP_NAME}" region="us-west-2" logStreamNameSuffix="Custom" logStreamNamePrefix="CustomPrefix"/>
<target name="loggerRegular" xsi:type="Console" layout="${callsite} ${message}" />
</targets>
<rules>
<logger name="loggerRegular" minlevel="Debug" writeTo="loggerRegular,AWSNLogGroup"/>
<logger name="loggerRegular" minlevel="Debug" writeTo="loggerRegular,{LOG_GROUP_NAME}"/>
</rules>
</nlog>
54 changes: 34 additions & 20 deletions test/AWS.Logger.NLog.Tests/TestClass.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Amazon.CloudWatchLogs.Model;
using AWS.Logger.TestUtils;
using NLog;
Expand All @@ -15,9 +19,15 @@ public class NLogTestClass: BaseTestClass
{
public NLog.Logger Logger;

private void CreateLoggerFromConfiguration(string configFileName)
private void CreateLoggerFromConfiguration(string configFileName, string logGroupName)
{
LogManager.Configuration = new XmlLoggingConfiguration(configFileName);
var fileInfo = new FileInfo(configFileName);
var fileContent = File.ReadAllText(fileInfo.FullName);
using (Stream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(fileContent.Replace("{LOG_GROUP_NAME}", logGroupName))))
using (XmlReader reader = XmlReader.Create(memoryStream))
{
LogManager.Configuration = new XmlLoggingConfiguration(reader, configFileName);
}
}

public NLogTestClass(TestFixture testFixture) : base(testFixture)
Expand All @@ -26,58 +36,62 @@ public NLogTestClass(TestFixture testFixture) : base(testFixture)

#region Test Cases
[Fact]
public void Nlog()
public async Task Nlog()
{
CreateLoggerFromConfiguration("Regular.config");
var logGroupName = $"AWSNLogGroup{Guid.NewGuid().ToString().Split('-').Last()}";
CreateLoggerFromConfiguration("Regular.config", logGroupName);
Logger = LogManager.GetLogger("loggerRegular");
SimpleLoggingTest("AWSNLogGroup");
await SimpleLoggingTest(logGroupName);
}

[Fact]
public void MultiThreadTest()
public async Task MultiThreadTest()
{
CreateLoggerFromConfiguration("AWSNLogGroupMultiThreadTest.config");
var logGroupName = $"AWSNLogGroupMultiThreadTest{Guid.NewGuid().ToString().Split('-').Last()}";
CreateLoggerFromConfiguration("AWSNLogGroupMultiThreadTest.config", logGroupName);
Logger = LogManager.GetLogger("loggerMultiThread");
MultiThreadTestGroup("AWSNLogGroupMultiThreadTest");
await MultiThreadTestGroup(logGroupName);
}

[Fact]
public void MultiThreadBufferFullTest()
public async Task MultiThreadBufferFullTest()
{
CreateLoggerFromConfiguration("AWSNLogGroupMultiThreadBufferFullTest.config");
var logGroupName = $"AWSNLogGroupMultiThreadBufferFullTest{Guid.NewGuid().ToString().Split('-').Last()}";
CreateLoggerFromConfiguration("AWSNLogGroupMultiThreadBufferFullTest.config", logGroupName);
Logger = LogManager.GetLogger("loggerMultiThreadBufferFull");
MultiThreadBufferFullTestGroup("AWSNLogGroupMultiThreadBufferFullTest");
await MultiThreadBufferFullTestGroup(logGroupName);
}

/// <summary>
/// Verifies that multiple producers can log to the same log stream
/// when an override log stream name is provided
/// </summary>
[Fact]
public void CustomLogStreamNameTest()
public async Task CustomLogStreamNameTest()
{
CreateLoggerFromConfiguration("AWSNLogOverrideLogStreamName.config");
var logGroupName = $"AWSNLogOverrideLogStreamName{Guid.NewGuid().ToString().Split('-').Last()}";
CreateLoggerFromConfiguration("AWSNLogOverrideLogStreamName.config", logGroupName);
Logger = LogManager.GetLogger("overrideLogStreamName");
MultiThreadTestGroup("AWSNLogOverrideLogStreamName", "CustomStreamName");
await MultiThreadTestGroup(logGroupName, "CustomStreamName");
}

[Fact]
public async Task MessageHasToBeBrokenUp()
{
string logGroupName = "AWSNLogGroupEventSizeExceededTest";
var logGroupName = $"AWSNLogGroupEventSizeExceededTest{Guid.NewGuid().ToString().Split('-').Last()}";

CreateLoggerFromConfiguration("AWSNLogGroupEventSizeExceededTest.config");
CreateLoggerFromConfiguration("AWSNLogGroupEventSizeExceededTest.config", logGroupName);
Logger = LogManager.GetLogger("loggerRegularEventSizeExceeded");

// This will get broken up into 3 CloudWatch Log messages
Logger.Debug(new string('a', 600000));
Logger.Debug(LASTMESSAGE);

GetLogEventsResponse getLogEventsResponse = new GetLogEventsResponse();
if (NotifyLoggingCompleted(logGroupName, "LASTMESSAGE"))
if (await NotifyLoggingCompleted(logGroupName, "LASTMESSAGE"))
{
DescribeLogStreamsResponse describeLogstreamsResponse =
await Client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest
await _testFixture.Client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest
{
Descending = true,
LogGroupName = logGroupName,
Expand All @@ -86,7 +100,7 @@ await Client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest

// Wait for the large messages to propagate
Thread.Sleep(5000);
getLogEventsResponse = await Client.GetLogEventsAsync(new GetLogEventsRequest
getLogEventsResponse = await _testFixture.Client.GetLogEventsAsync(new GetLogEventsRequest
{
LogGroupName = logGroupName,
LogStreamName = describeLogstreamsResponse.LogStreams[0].LogStreamName
Expand All @@ -100,7 +114,7 @@ protected override void LogMessages(int count)
{
for (int i = 0; i < count-1; i++)
{
Logger.Debug(string.Format("Test logging message {0} NLog, Thread Id:{1}", i, Thread.CurrentThread.ManagedThreadId));
Logger.Debug(string.Format("Test logging message {0} NLog, Thread Id:{1}", i, Environment.CurrentManagedThreadId));
}
Logger.Debug(LASTMESSAGE);
}
Expand Down
2 changes: 1 addition & 1 deletion test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroup.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Serilog": {
"Using": [ "AWS.Logger.SeriLog" ],
"MinimumLevel": "Debug",
"LogGroup": "AWSSeriLogGroup",
"LogGroup": "{LOG_GROUP_NAME}",
"Region": "us-west-2",
"LogStreamNameSuffix": "Custom",
"LogStreamNamePrefix": "CustomPrefix",
Expand Down
Loading

0 comments on commit 7eff662

Please sign in to comment.