Skip to content

Commit

Permalink
test: Add logging code.
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Jun 15, 2024
1 parent cb5ba52 commit 3c7ad30
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 23 deletions.
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

0 comments on commit 3c7ad30

Please sign in to comment.