Skip to content

Commit

Permalink
Merge pull request #27 from AoshiW/modern-.NET
Browse files Browse the repository at this point in the history
switch to File Scope Namespace, and more
  • Loading branch information
swiftyspiffy authored Aug 5, 2023
2 parents d9bdf98 + 96ec32e commit 6e98a4d
Show file tree
Hide file tree
Showing 26 changed files with 1,781 additions and 1,808 deletions.
1,173 changes: 581 additions & 592 deletions src/TwitchLib.Communication.Tests/Clients/ClientTestsBase.cs

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions src/TwitchLib.Communication.Tests/Clients/TcpClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using TwitchLib.Communication.Clients;
using TwitchLib.Communication.Models;

namespace TwitchLib.Communication.Tests.Clients
namespace TwitchLib.Communication.Tests.Clients;

public class TcpClientTests : ClientTestsBase<TcpClient>
{
public class TcpClientTests : ClientTestsBase<TcpClient>
{
public TcpClientTests() : base(new ClientOptions(useSsl: false)) { }
}
public TcpClientTests() : base(new ClientOptions(useSsl: false)) { }
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using TwitchLib.Communication.Clients;

namespace TwitchLib.Communication.Tests.Clients
namespace TwitchLib.Communication.Tests.Clients;

public class WebSocketClientTests : ClientTestsBase<WebSocketClient>
{
public class WebSocketClientTests : ClientTestsBase<WebSocketClient>
{
}
}
}
109 changes: 54 additions & 55 deletions src/TwitchLib.Communication.Tests/Helpers/TestLogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,64 @@
using Serilog.Events;
using Serilog.Exceptions;

namespace TwitchLib.Communication.Tests.Helpers
namespace TwitchLib.Communication.Tests.Helpers;

internal static class TestLogHelper
{
internal static class TestLogHelper
{
private static readonly string OUTPUT_TEMPLATE =
"[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u}] {Message:lj}{NewLine}{Exception}{NewLine}";
private static readonly string OUTPUT_TEMPLATE =
"[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u}] {Message:lj}{NewLine}{Exception}{NewLine}";

private static readonly string NEW_TEST_RUN_INDICATOR;
private static readonly string NEW_TEST_RUN_INDICATOR;

static TestLogHelper()
{
StringBuilder builder = new StringBuilder();
builder.AppendLine();
builder.AppendLine(new string('-', 80));
builder.Append(new string(' ', 34));
builder.AppendLine("new Test-Run");
builder.AppendLine(new string('-', 80));
NEW_TEST_RUN_INDICATOR = builder.ToString();
}
static TestLogHelper()
{
var builder = new StringBuilder();
builder.AppendLine();
builder.Append('-', 80).AppendLine();
builder.Append(' ', 34);
builder.AppendLine("new Test-Run");
builder.Append('-', 80).AppendLine();
NEW_TEST_RUN_INDICATOR = builder.ToString();
}

internal static Microsoft.Extensions.Logging.ILogger<T> GetLogger<T>(
LogEventLevel logEventLevel = LogEventLevel.Verbose,
[CallerMemberName] string callerMemberName = "TestMethod")
{
Serilog.ILogger logger = GetSerilogLogger<T>(typeof(T).Name,
callerMemberName,
logEventLevel);
Microsoft.Extensions.Logging.ILoggerFactory loggerFactory =
new Serilog.Extensions.Logging.SerilogLoggerFactory(logger);
return loggerFactory.CreateLogger<T>();
}
internal static Microsoft.Extensions.Logging.ILogger<T> GetLogger<T>(
LogEventLevel logEventLevel = LogEventLevel.Verbose,
[CallerMemberName] string callerMemberName = "TestMethod")
{
Serilog.ILogger logger = GetSerilogLogger<T>(typeof(T).Name,
callerMemberName,
logEventLevel);
Microsoft.Extensions.Logging.ILoggerFactory loggerFactory =
new Serilog.Extensions.Logging.SerilogLoggerFactory(logger);
return loggerFactory.CreateLogger<T>();
}

private static Serilog.ILogger GetSerilogLogger<T>(string typeName,
string callerMemberName,
LogEventLevel logEventLevel)
{
Serilog.LoggerConfiguration loggerConfiguration = GetConfiguration(typeName,
callerMemberName,
logEventLevel);
Serilog.ILogger logger = loggerConfiguration.CreateLogger().ForContext<T>();
logger.Information(NEW_TEST_RUN_INDICATOR);
return logger;
}
private static Serilog.ILogger GetSerilogLogger<T>(string typeName,
string callerMemberName,
LogEventLevel logEventLevel)
{
Serilog.LoggerConfiguration loggerConfiguration = GetConfiguration(typeName,
callerMemberName,
logEventLevel);
Serilog.ILogger logger = loggerConfiguration.CreateLogger().ForContext<T>();
logger.Information(NEW_TEST_RUN_INDICATOR);
return logger;
}

private static Serilog.LoggerConfiguration GetConfiguration(string typeName,
string callerMemberName,
LogEventLevel logEventLevel)
{
Serilog.LoggerConfiguration loggerConfiguration = new Serilog.LoggerConfiguration();
loggerConfiguration.MinimumLevel.Verbose();
string path = $"../../../Logs/{typeName}/{callerMemberName}.log";
loggerConfiguration.WriteTo.File(
path: path,
restrictedToMinimumLevel: logEventLevel,
outputTemplate: OUTPUT_TEMPLATE
);
loggerConfiguration.Enrich.WithExceptionDetails();
loggerConfiguration.Enrich.FromLogContext();
return loggerConfiguration;
}
private static Serilog.LoggerConfiguration GetConfiguration(string typeName,
string callerMemberName,
LogEventLevel logEventLevel)
{
var loggerConfiguration = new Serilog.LoggerConfiguration();
loggerConfiguration.MinimumLevel.Verbose();
string path = $"../../../Logs/{typeName}/{callerMemberName}.log";
loggerConfiguration.WriteTo.File(
path: path,
restrictedToMinimumLevel: logEventLevel,
outputTemplate: OUTPUT_TEMPLATE
);
loggerConfiguration.Enrich.WithExceptionDetails();
loggerConfiguration.Enrich.FromLogContext();
return loggerConfiguration;
}
}
}
61 changes: 30 additions & 31 deletions src/TwitchLib.Communication.Tests/Models/ReconnectionPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,38 @@
using TwitchLib.Communication.Models;
using Xunit;

namespace TwitchLib.Communication.Tests.Models
namespace TwitchLib.Communication.Tests.Models;

public class ReconnectionPolicyTests
{
public class ReconnectionPolicyTests
/// <summary>
/// Checks <see cref="ClientOptions.ReconnectionPolicy"/>
/// <br></br>
/// <see cref="ReconnectionPolicy.AreAttemptsComplete"/>
/// <br></br>
/// <see cref="ReconnectionPolicy.Reset(Boolean)"/>
/// </summary>
[Fact]
public void ReconnectionPolicy_OmitReconnect()
{
/// <summary>
/// Checks <see cref="ClientOptions.ReconnectionPolicy"/>
/// <br></br>
/// <see cref="ReconnectionPolicy.AreAttemptsComplete"/>
/// <br></br>
/// <see cref="ReconnectionPolicy.Reset(Boolean)"/>
/// </summary>
[Fact]
public void ReconnectionPolicy_OmitReconnect()
try
{
ReconnectionPolicy reconnectionPolicy = new NoReconnectionPolicy();
Assert.False(reconnectionPolicy.AreAttemptsComplete());
reconnectionPolicy.ProcessValues();
Assert.True(reconnectionPolicy.AreAttemptsComplete());
// in case of a normal connect, we expect the ReconnectionPolicy to be reset
reconnectionPolicy.Reset(false);
Assert.False(reconnectionPolicy.AreAttemptsComplete());
reconnectionPolicy.ProcessValues();
Assert.True(reconnectionPolicy.AreAttemptsComplete());
// in case of a reconnect, we expect the ReconnectionPolicy not to be reset
reconnectionPolicy.Reset(true);
Assert.True(reconnectionPolicy.AreAttemptsComplete());
}
catch (Exception e)
{
try
{
ReconnectionPolicy reconnectionPolicy = new NoReconnectionPolicy();
Assert.False(reconnectionPolicy.AreAttemptsComplete());
reconnectionPolicy.ProcessValues();
Assert.True(reconnectionPolicy.AreAttemptsComplete());
// in case of a normal connect, we expect the ReconnectionPolicy to be reset
reconnectionPolicy.Reset(false);
Assert.False(reconnectionPolicy.AreAttemptsComplete());
reconnectionPolicy.ProcessValues();
Assert.True(reconnectionPolicy.AreAttemptsComplete());
// in case of a reconnect, we expect the ReconnectionPolicy not to be reset
reconnectionPolicy.Reset(true);
Assert.True(reconnectionPolicy.AreAttemptsComplete());
}
catch (Exception e)
{
Assert.Fail(e.ToString());
}
Assert.Fail(e.ToString());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFrameworks>net7.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
Expand Down
Loading

0 comments on commit 6e98a4d

Please sign in to comment.