Skip to content

Commit

Permalink
Merge pull request #16 from s2quake/test/logging
Browse files Browse the repository at this point in the history
Add logging code to test project
  • Loading branch information
s2quake authored Jun 15, 2024
2 parents ff35196 + fa221bf commit 8ca2ee0
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 51 deletions.
8 changes: 4 additions & 4 deletions test/JSSoft.Communication.Tests/CallbackTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions test/JSSoft.Communication.Tests/ClientTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma warning disable SA1402

using JSSoft.Communication.Tests.Extensions;
using Xunit.Abstractions;

namespace JSSoft.Communication.Tests;

Expand All @@ -22,28 +23,36 @@ public abstract class ClientTestBase<TService> : IAsyncLifetime
private Guid _clientToken;
private Guid _serverToken;

protected ClientTestBase(ServerService<TService> serverService)
protected ClientTestBase(ITestOutputHelper logger, ServerService<TService> 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<TService> ServerService { get; }

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();
}
}
Expand All @@ -61,13 +70,16 @@ public abstract class ClientTestBase<TService, TServerSevice> : 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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using Xunit.Abstractions;

namespace JSSoft.Communication.Tests.Exceptions;

public sealed class ArgumentExceptionTest : ExceptionTestBase<ArgumentException>
public sealed class ArgumentExceptionTest(ITestOutputHelper logger)
: ExceptionTestBase<ArgumentException>(logger)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using Xunit.Abstractions;

namespace JSSoft.Communication.Tests.Exceptions;

public sealed class ArgumentNullExceptionTest : ExceptionTestBase<ArgumentNullException>
public sealed class ArgumentNullExceptionTest(ITestOutputHelper logger)
: ExceptionTestBase<ArgumentNullException>(logger)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using Xunit.Abstractions;

namespace JSSoft.Communication.Tests.Exceptions;

public sealed class ArgumentOutOfRangeExceptionTest : ExceptionTestBase<ArgumentOutOfRangeException>
public sealed class ArgumentOutOfRangeExceptionTest(ITestOutputHelper logger)
: ExceptionTestBase<ArgumentOutOfRangeException>(logger)
{
}
11 changes: 4 additions & 7 deletions test/JSSoft.Communication.Tests/Exceptions/ExceptionTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using Xunit.Abstractions;

namespace JSSoft.Communication.Tests.Exceptions;

public abstract class ExceptionTestBase<TException>
: ClientTestBase<ExceptionTestBase<TException>.ITestService>
public abstract class ExceptionTestBase<TException>(ITestOutputHelper logger)
: ClientTestBase<ExceptionTestBase<TException>.ITestService>(logger, new TestServer())
where TException : Exception
{
protected ExceptionTestBase()
: base(new TestServer())
{
}

public interface ITestService
{
void Invoke()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using Xunit.Abstractions;

namespace JSSoft.Communication.Tests.Exceptions;

public sealed class IndexOutOfRangeExceptionTest : ExceptionTestBase<IndexOutOfRangeException>
public sealed class IndexOutOfRangeExceptionTest(ITestOutputHelper logger)
: ExceptionTestBase<IndexOutOfRangeException>(logger)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using Xunit.Abstractions;

namespace JSSoft.Communication.Tests.Exceptions;

public sealed class InvalidOperationExceptionTest : ExceptionTestBase<ArgumentException>
public sealed class InvalidOperationExceptionTest(ITestOutputHelper logger)
: ExceptionTestBase<ArgumentException>(logger)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using Xunit.Abstractions;

namespace JSSoft.Communication.Tests.Exceptions;

public sealed class NotSupportedExceptionTest : ExceptionTestBase<NotSupportedException>
public sealed class NotSupportedExceptionTest(ITestOutputHelper logger)
: ExceptionTestBase<NotSupportedException>(logger)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using Xunit.Abstractions;

namespace JSSoft.Communication.Tests.Exceptions;

public sealed class NullReferenceExceptionTest : ExceptionTestBase<NullReferenceException>
public sealed class NullReferenceExceptionTest(ITestOutputHelper logger)
: ExceptionTestBase<NullReferenceException>(logger)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using Xunit.Abstractions;

namespace JSSoft.Communication.Tests.Exceptions;

public sealed class ObjectDisposedExceptionTest : ExceptionTestBase<ObjectDisposedException>
public sealed class ObjectDisposedExceptionTest(ITestOutputHelper logger)
: ExceptionTestBase<ObjectDisposedException>(logger)
{
}
10 changes: 4 additions & 6 deletions test/JSSoft.Communication.Tests/InvokeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using Xunit.Abstractions;

namespace JSSoft.Communication.Tests;

public class InvokeTest : ClientTestBase<InvokeTest.ITestService, InvokeTest.TestServer>
public class InvokeTest(ITestOutputHelper logger)
: ClientTestBase<InvokeTest.ITestService, InvokeTest.TestServer>(logger, new TestServer())
{
public InvokeTest()
: base(new TestServer())
{
}

public interface ITestService
{
void Invoke();
Expand Down
24 changes: 0 additions & 24 deletions test/JSSoft.Communication.Tests/TestLogger.cs

This file was deleted.

0 comments on commit 8ca2ee0

Please sign in to comment.