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

switch to File Scope Namespace, and more #27

Merged
merged 4 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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>
Copy link
Contributor

Choose a reason for hiding this comment

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

After reverting multiple frameworks, this can be switched back to TargetFramework, but it's not a big deal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I originally wanted to add a test for current LTS .NET6 but the test failed so I removed that test for now
and someone would change TargetFramework to TargetFrameworks anyway in the future, so I left it there.

<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
Expand Down
Loading