From e9c6fffbcc2e95e0118fa23926a92b4751f53c30 Mon Sep 17 00:00:00 2001 From: s2quake Date: Sat, 15 Jun 2024 19:41:30 +0900 Subject: [PATCH 1/2] test: Add logging code. --- .../JSSoft.Communication.Tests/ClientTestBase.cs | 16 ++++++++++++++-- .../Exceptions/ArgumentExceptionTest.cs | 5 ++++- .../Exceptions/ArgumentNullExceptionTest.cs | 5 ++++- .../ArgumentOutOfRangeExceptionTest.cs | 5 ++++- .../Exceptions/ExceptionTestBase.cs | 11 ++++------- .../Exceptions/IndexOutOfRangeExceptionTest.cs | 5 ++++- .../Exceptions/InvalidOperationExceptionTest.cs | 5 ++++- .../Exceptions/NotSupportedExceptionTest.cs | 5 ++++- .../Exceptions/NullReferenceExceptionTest.cs | 5 ++++- .../Exceptions/ObjectDisposedExceptionTest.cs | 5 ++++- test/JSSoft.Communication.Tests/InvokeTest.cs | 10 ++++------ 11 files changed, 54 insertions(+), 23 deletions(-) diff --git a/test/JSSoft.Communication.Tests/ClientTestBase.cs b/test/JSSoft.Communication.Tests/ClientTestBase.cs index e2c1529..27cdd37 100644 --- a/test/JSSoft.Communication.Tests/ClientTestBase.cs +++ b/test/JSSoft.Communication.Tests/ClientTestBase.cs @@ -7,6 +7,7 @@ #pragma warning disable SA1402 using JSSoft.Communication.Tests.Extensions; +using Xunit.Abstractions; namespace JSSoft.Communication.Tests; @@ -22,13 +23,17 @@ public abstract class ClientTestBase : IAsyncLifetime private Guid _clientToken; private Guid _serverToken; - protected ClientTestBase(ServerService serverService) + protected ClientTestBase(ITestOutputHelper logger, ServerService serverService) { + Logger = logger; ServerService = serverService; _serverContext = new(ServerService) { EndPoint = _endPoint }; _clientContext = new(_clientService) { EndPoint = _endPoint }; + Logger.WriteLine($"EndPoint: {_endPoint}"); } + protected ITestOutputHelper Logger { get; } + protected TService Client => _client!; protected ServerService ServerService { get; } @@ -36,14 +41,18 @@ protected ClientTestBase(ServerService serverService) public async Task InitializeAsync() { _serverToken = await _serverContext.OpenAsync(cancellationToken: default); + Logger.WriteLine($"Server is opened: {_serverToken}"); _clientToken = await _clientContext.OpenAsync(cancellationToken: default); + Logger.WriteLine($"Client is opened: {_clientToken}"); _client = _clientService.Server; } public async Task DisposeAsync() { await _serverContext.ReleaseAsync(_serverToken); + Logger.WriteLine($"Server is released: {_serverToken}"); await _clientContext.ReleaseAsync(_clientToken); + Logger.WriteLine($"Client is released: {_clientToken}"); _endPoint.Dispose(); } } @@ -61,13 +70,16 @@ public abstract class ClientTestBase : IAsyncLifetime private Guid _clientToken; private Guid _serverToken; - protected ClientTestBase(TServerSevice serverService) + protected ClientTestBase(ITestOutputHelper logger, TServerSevice serverService) { + Logger = logger; ServerService = serverService; _serverContext = new(ServerService) { EndPoint = _endPoint }; _clientContext = new(_clientService) { EndPoint = _endPoint }; } + protected ITestOutputHelper Logger { get; } + protected TService Client => _client!; protected TServerSevice ServerService { get; } diff --git a/test/JSSoft.Communication.Tests/Exceptions/ArgumentExceptionTest.cs b/test/JSSoft.Communication.Tests/Exceptions/ArgumentExceptionTest.cs index a06a5db..a1fbe1f 100644 --- a/test/JSSoft.Communication.Tests/Exceptions/ArgumentExceptionTest.cs +++ b/test/JSSoft.Communication.Tests/Exceptions/ArgumentExceptionTest.cs @@ -3,8 +3,11 @@ // Licensed under the MIT License. See LICENSE.md in the project root for license information. // +using Xunit.Abstractions; + namespace JSSoft.Communication.Tests.Exceptions; -public sealed class ArgumentExceptionTest : ExceptionTestBase +public sealed class ArgumentExceptionTest(ITestOutputHelper logger) + : ExceptionTestBase(logger) { } diff --git a/test/JSSoft.Communication.Tests/Exceptions/ArgumentNullExceptionTest.cs b/test/JSSoft.Communication.Tests/Exceptions/ArgumentNullExceptionTest.cs index b782a70..31708ec 100644 --- a/test/JSSoft.Communication.Tests/Exceptions/ArgumentNullExceptionTest.cs +++ b/test/JSSoft.Communication.Tests/Exceptions/ArgumentNullExceptionTest.cs @@ -3,8 +3,11 @@ // Licensed under the MIT License. See LICENSE.md in the project root for license information. // +using Xunit.Abstractions; + namespace JSSoft.Communication.Tests.Exceptions; -public sealed class ArgumentNullExceptionTest : ExceptionTestBase +public sealed class ArgumentNullExceptionTest(ITestOutputHelper logger) + : ExceptionTestBase(logger) { } diff --git a/test/JSSoft.Communication.Tests/Exceptions/ArgumentOutOfRangeExceptionTest.cs b/test/JSSoft.Communication.Tests/Exceptions/ArgumentOutOfRangeExceptionTest.cs index 07ff1ff..a5ed931 100644 --- a/test/JSSoft.Communication.Tests/Exceptions/ArgumentOutOfRangeExceptionTest.cs +++ b/test/JSSoft.Communication.Tests/Exceptions/ArgumentOutOfRangeExceptionTest.cs @@ -3,8 +3,11 @@ // Licensed under the MIT License. See LICENSE.md in the project root for license information. // +using Xunit.Abstractions; + namespace JSSoft.Communication.Tests.Exceptions; -public sealed class ArgumentOutOfRangeExceptionTest : ExceptionTestBase +public sealed class ArgumentOutOfRangeExceptionTest(ITestOutputHelper logger) + : ExceptionTestBase(logger) { } diff --git a/test/JSSoft.Communication.Tests/Exceptions/ExceptionTestBase.cs b/test/JSSoft.Communication.Tests/Exceptions/ExceptionTestBase.cs index 2186dbe..faedd2e 100644 --- a/test/JSSoft.Communication.Tests/Exceptions/ExceptionTestBase.cs +++ b/test/JSSoft.Communication.Tests/Exceptions/ExceptionTestBase.cs @@ -3,17 +3,14 @@ // Licensed under the MIT License. See LICENSE.md in the project root for license information. // +using Xunit.Abstractions; + namespace JSSoft.Communication.Tests.Exceptions; -public abstract class ExceptionTestBase - : ClientTestBase.ITestService> +public abstract class ExceptionTestBase(ITestOutputHelper logger) + : ClientTestBase.ITestService>(logger, new TestServer()) where TException : Exception { - protected ExceptionTestBase() - : base(new TestServer()) - { - } - public interface ITestService { void Invoke() diff --git a/test/JSSoft.Communication.Tests/Exceptions/IndexOutOfRangeExceptionTest.cs b/test/JSSoft.Communication.Tests/Exceptions/IndexOutOfRangeExceptionTest.cs index b669af4..8ce1de1 100644 --- a/test/JSSoft.Communication.Tests/Exceptions/IndexOutOfRangeExceptionTest.cs +++ b/test/JSSoft.Communication.Tests/Exceptions/IndexOutOfRangeExceptionTest.cs @@ -3,8 +3,11 @@ // Licensed under the MIT License. See LICENSE.md in the project root for license information. // +using Xunit.Abstractions; + namespace JSSoft.Communication.Tests.Exceptions; -public sealed class IndexOutOfRangeExceptionTest : ExceptionTestBase +public sealed class IndexOutOfRangeExceptionTest(ITestOutputHelper logger) + : ExceptionTestBase(logger) { } diff --git a/test/JSSoft.Communication.Tests/Exceptions/InvalidOperationExceptionTest.cs b/test/JSSoft.Communication.Tests/Exceptions/InvalidOperationExceptionTest.cs index bfcb5bd..d477907 100644 --- a/test/JSSoft.Communication.Tests/Exceptions/InvalidOperationExceptionTest.cs +++ b/test/JSSoft.Communication.Tests/Exceptions/InvalidOperationExceptionTest.cs @@ -3,8 +3,11 @@ // Licensed under the MIT License. See LICENSE.md in the project root for license information. // +using Xunit.Abstractions; + namespace JSSoft.Communication.Tests.Exceptions; -public sealed class InvalidOperationExceptionTest : ExceptionTestBase +public sealed class InvalidOperationExceptionTest(ITestOutputHelper logger) + : ExceptionTestBase(logger) { } diff --git a/test/JSSoft.Communication.Tests/Exceptions/NotSupportedExceptionTest.cs b/test/JSSoft.Communication.Tests/Exceptions/NotSupportedExceptionTest.cs index 5a21e16..50edf18 100644 --- a/test/JSSoft.Communication.Tests/Exceptions/NotSupportedExceptionTest.cs +++ b/test/JSSoft.Communication.Tests/Exceptions/NotSupportedExceptionTest.cs @@ -3,8 +3,11 @@ // Licensed under the MIT License. See LICENSE.md in the project root for license information. // +using Xunit.Abstractions; + namespace JSSoft.Communication.Tests.Exceptions; -public sealed class NotSupportedExceptionTest : ExceptionTestBase +public sealed class NotSupportedExceptionTest(ITestOutputHelper logger) + : ExceptionTestBase(logger) { } diff --git a/test/JSSoft.Communication.Tests/Exceptions/NullReferenceExceptionTest.cs b/test/JSSoft.Communication.Tests/Exceptions/NullReferenceExceptionTest.cs index fd67257..030527b 100644 --- a/test/JSSoft.Communication.Tests/Exceptions/NullReferenceExceptionTest.cs +++ b/test/JSSoft.Communication.Tests/Exceptions/NullReferenceExceptionTest.cs @@ -3,8 +3,11 @@ // Licensed under the MIT License. See LICENSE.md in the project root for license information. // +using Xunit.Abstractions; + namespace JSSoft.Communication.Tests.Exceptions; -public sealed class NullReferenceExceptionTest : ExceptionTestBase +public sealed class NullReferenceExceptionTest(ITestOutputHelper logger) + : ExceptionTestBase(logger) { } diff --git a/test/JSSoft.Communication.Tests/Exceptions/ObjectDisposedExceptionTest.cs b/test/JSSoft.Communication.Tests/Exceptions/ObjectDisposedExceptionTest.cs index 2438b41..cca7d59 100644 --- a/test/JSSoft.Communication.Tests/Exceptions/ObjectDisposedExceptionTest.cs +++ b/test/JSSoft.Communication.Tests/Exceptions/ObjectDisposedExceptionTest.cs @@ -3,8 +3,11 @@ // Licensed under the MIT License. See LICENSE.md in the project root for license information. // +using Xunit.Abstractions; + namespace JSSoft.Communication.Tests.Exceptions; -public sealed class ObjectDisposedExceptionTest : ExceptionTestBase +public sealed class ObjectDisposedExceptionTest(ITestOutputHelper logger) + : ExceptionTestBase(logger) { } diff --git a/test/JSSoft.Communication.Tests/InvokeTest.cs b/test/JSSoft.Communication.Tests/InvokeTest.cs index 70a96ce..67414ab 100644 --- a/test/JSSoft.Communication.Tests/InvokeTest.cs +++ b/test/JSSoft.Communication.Tests/InvokeTest.cs @@ -3,15 +3,13 @@ // Licensed under the MIT License. See LICENSE.md in the project root for license information. // +using Xunit.Abstractions; + namespace JSSoft.Communication.Tests; -public class InvokeTest : ClientTestBase +public class InvokeTest(ITestOutputHelper logger) + : ClientTestBase(logger, new TestServer()) { - public InvokeTest() - : base(new TestServer()) - { - } - public interface ITestService { void Invoke(); From fa221bfab765eebd41d0b5122c62d879fccde50c Mon Sep 17 00:00:00 2001 From: s2quake Date: Sat, 15 Jun 2024 19:49:45 +0900 Subject: [PATCH 2/2] test: Add logging and remove unnecessary codes. --- .../CallbackTest.cs | 8 +++---- test/JSSoft.Communication.Tests/TestLogger.cs | 24 ------------------- 2 files changed, 4 insertions(+), 28 deletions(-) delete mode 100644 test/JSSoft.Communication.Tests/TestLogger.cs diff --git a/test/JSSoft.Communication.Tests/CallbackTest.cs b/test/JSSoft.Communication.Tests/CallbackTest.cs index eeb9e66..02670a5 100644 --- a/test/JSSoft.Communication.Tests/CallbackTest.cs +++ b/test/JSSoft.Communication.Tests/CallbackTest.cs @@ -94,20 +94,20 @@ public void Callback3_Test() public async Task InitializeAsync() { - _logger.WriteLine($"InitializeAsync 1"); _serverToken = await _serverContext.OpenAsync(CancellationToken.None); + _logger.WriteLine($"Server is opened: {_serverToken}"); _clientToken = await _clientContext.OpenAsync(CancellationToken.None); + _logger.WriteLine($"Client is opened: {_clientToken}"); _server = _testServer; - _logger.WriteLine($"InitializeAsync 2"); } public async Task DisposeAsync() { - _logger.WriteLine($"DisposeAsync 1"); await _serverContext.ReleaseAsync(_serverToken); + _logger.WriteLine($"Server is released: {_serverToken}"); await _clientContext.ReleaseAsync(_clientToken); + _logger.WriteLine($"Client is released: {_clientToken}"); _endPoint.Dispose(); - _logger.WriteLine($"DisposeAsync 2"); } public class ValueEventArgs(object? value) : EventArgs diff --git a/test/JSSoft.Communication.Tests/TestLogger.cs b/test/JSSoft.Communication.Tests/TestLogger.cs deleted file mode 100644 index ff2e341..0000000 --- a/test/JSSoft.Communication.Tests/TestLogger.cs +++ /dev/null @@ -1,24 +0,0 @@ -// -// Copyright (c) 2024 Jeesu Choi. All Rights Reserved. -// Licensed under the MIT License. See LICENSE.md in the project root for license information. -// - -using JSSoft.Communication.Logging; -using Xunit.Abstractions; - -namespace JSSoft.Communication.Tests; - -public sealed class TestLogger(ITestOutputHelper logger) : ILogger -{ - private readonly ITestOutputHelper _logger = logger; - - public void Debug(object message) => _logger.WriteLine($"{message}\n"); - - public void Info(object message) => _logger.WriteLine($"{message}\n"); - - public void Error(object message) => _logger.WriteLine($"{message}\n"); - - public void Warn(object message) => _logger.WriteLine($"{message}\n"); - - public void Fatal(object message) => _logger.WriteLine($"{message}\n"); -}