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

Creating xUnit version of BaseTest, as well as xUnit versions of startup logic #505

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
32 changes: 32 additions & 0 deletions source/Halibut.Tests/BaseTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Threading;
using Halibut.Tests.Support;
using NUnit.Framework;
using Serilog;
using Xunit;

namespace Halibut.Tests
{
Expand Down Expand Up @@ -31,4 +33,34 @@ public void TearDown()
Logger.Information("Finished Test Tearing Down");
}
}

// TODO: @server-at-scale - Merge with above when tests are completely cut over to xUnit.
public class BaseTestXUnit :
IClassFixture<BindCertificatesForAllTests>,
IClassFixture<BumpThreadPoolForAllTests>,
IClassFixture<LowerHalibutLimitsForAllTests>,
IDisposable
{
readonly CancellationTokenSource? cancellationTokenSource;
public CancellationToken CancellationToken { get; private set; }
public ILogger Logger { get; }

public BaseTestXUnit()
{
Logger = new SerilogLoggerBuilder().Build().ForContext(GetType());
Logger.Information("Test started");
cancellationTokenSource = new CancellationTokenSource();
CancellationToken = cancellationTokenSource.Token;
}

public void Dispose()
{
Logger.Information("Staring Test Tearing Down");
Logger.Information("Cancelling CancellationTokenSource");
cancellationTokenSource?.Cancel();
Logger.Information("Disposing CancellationTokenSource");
cancellationTokenSource?.Dispose();
Logger.Information("Finished Test Tearing Down");
}
}
}
10 changes: 10 additions & 0 deletions source/Halibut.Tests/BindCertificatesForAllTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
using System;
using Halibut.Tests.Support;
using NUnit.Framework;

namespace Halibut.Tests
{
public class BindCertificatesForAllTests
{
public BindCertificatesForAllTests()
{
#if SUPPORTS_WEB_SOCKET_CLIENT
WebSocketSslCertificateHelper.AddSslCertToLocalStore();
#endif
}

[SetUpFixture]
[Obsolete("Remove when NUnit is fully replaced with xUnit")]
public class TestsSetupClass
{
[OneTimeSetUp]
[Obsolete("Remove when NUnit is fully replaced with xUnit")]
public void GlobalSetup()
{
#if SUPPORTS_WEB_SOCKET_CLIENT
Expand Down
11 changes: 11 additions & 0 deletions source/Halibut.Tests/BumpThreadPoolForAllTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ namespace Halibut.Tests
{
public class BumpThreadPoolForAllTests
{
public BumpThreadPoolForAllTests()
{
var minWorkerPoolThreads = 400;
ThreadPool.GetMinThreads(out _, out var minCompletionPortThreads);
ThreadPool.GetMaxThreads(out var maxWorkerThreads, out var maxCompletionPortThreads);
ThreadPool.SetMaxThreads(Math.Max(minWorkerPoolThreads, maxWorkerThreads), Math.Max(minCompletionPortThreads, maxCompletionPortThreads));
ThreadPool.SetMinThreads(minWorkerPoolThreads, minCompletionPortThreads);
}

[SetUpFixture]
[Obsolete("Remove when NUnit is fully replaced with xUnit")]
public class TestsSetupClass
{
[OneTimeSetUp]
[Obsolete("Remove when NUnit is fully replaced with xUnit")]
public void GlobalSetup()
{
var minWorkerPoolThreads = 400;
Expand Down
18 changes: 9 additions & 9 deletions source/Halibut.Tests/Diagnostics/CachingLogFactoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
using Halibut.Diagnostics;
using Halibut.Diagnostics.LogCreators;
using Halibut.Tests.Support.Logging;
using NUnit.Framework;
using Xunit;

namespace Halibut.Tests.Diagnostics
{
public class CachingLogFactoryFixture
{
[Test]
[Fact]
public void TheSameILogIsReturnedForTheSamePrefix()
{
var cachingLogFactory = new InMemoryConnectionLogCreator().ToCachingLogFactory();
Expand All @@ -20,7 +20,7 @@ public void TheSameILogIsReturnedForTheSamePrefix()
ReferenceEquals(first, second).Should().BeTrue();
}

[Test]
[Fact]
public void TheSameILogIsReturnedForTheSameEndPoint()
{
var cachingLogFactory = new InMemoryConnectionLogCreator().ToCachingLogFactory();
Expand All @@ -30,7 +30,7 @@ public void TheSameILogIsReturnedForTheSameEndPoint()
ReferenceEquals(first, second).Should().BeTrue();
}

[Test]
[Fact]
public void TheSameILogIsReturnedForTheSameEndPointAndPrefix()
{
var cachingLogFactory = new InMemoryConnectionLogCreator().ToCachingLogFactory();
Expand All @@ -40,7 +40,7 @@ public void TheSameILogIsReturnedForTheSameEndPointAndPrefix()
ReferenceEquals(first, second).Should().BeTrue();
}

[Test]
[Fact]
public void ADifferentILogIsReturnedForTheDifferentPrefixes()
{
var cachingLogFactory = new InMemoryConnectionLogCreator().ToCachingLogFactory();
Expand All @@ -50,7 +50,7 @@ public void ADifferentILogIsReturnedForTheDifferentPrefixes()
ReferenceEquals(first, second).Should().BeFalse();
}

[Test]
[Fact]
public void ADifferentILogIsReturnedForTheDifferentEndPoints()
{
var cachingLogFactory = new InMemoryConnectionLogCreator().ToCachingLogFactory();
Expand All @@ -60,7 +60,7 @@ public void ADifferentILogIsReturnedForTheDifferentEndPoints()
ReferenceEquals(first, second).Should().BeFalse();
}

[Test]
[Fact]
public void CachingAInMemoryConnectionLogMeansTheLogsAreRetained()
{
var cachingLogFactory = new InMemoryConnectionLogCreator().ToCachingLogFactory();
Expand All @@ -83,7 +83,7 @@ public void CachingAInMemoryConnectionLogMeansTheLogsAreRetained()
/// <summary>
/// Something similar to what we actually want to do, so lets test it here.
/// </summary>
[Test]
[Fact]
public void CachingWithAggregateLogWriterLogCreatorAndInMemoryConnectionLogCreatorWorksAsExpected()
{
var logWriter = new InMemoryLogWriter();
Expand Down Expand Up @@ -113,4 +113,4 @@ public void CachingWithAggregateLogWriterLogCreatorAndInMemoryConnectionLogCreat
logsFromWriter[1].FormattedMessage.Should().Be("Hello from endpoint");
}
}
}
}
1 change: 1 addition & 0 deletions source/Halibut.Tests/Halibut.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.36" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" />
<PackageReference Include="xunit" Version="2.5.1" />
</ItemGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net48' ">
Expand Down
23 changes: 23 additions & 0 deletions source/Halibut.Tests/LowerHalibutLimitsForAllTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,35 @@

namespace Halibut.Tests
{
//TODO: @server-at-scale - Remove attribute when NUnit is fully replaced with xUnit
[SetUpFixture]
public class LowerHalibutLimitsForAllTests
{
public static readonly TimeSpan HalfTheTcpReceiveTimeout = TimeSpan.FromSeconds(22.5);

public LowerHalibutLimitsForAllTests()
{
var halibutLimitType = typeof(HalibutLimits);

// The following 4 can be overriden, so set them high and let the test author drop the values as needed.
// Also set to a "weird value" to make it more obvious which timeout is at play in tests.
halibutLimitType.ReflectionSetFieldValue(nameof(HalibutLimits.PollingRequestQueueTimeout), TimeSpan.FromSeconds(66));
halibutLimitType.ReflectionSetFieldValue(nameof(HalibutLimits.PollingRequestMaximumMessageProcessingTimeout), TimeSpan.FromSeconds(66));
halibutLimitType.ReflectionSetFieldValue(nameof(HalibutLimits.RetryListeningSleepInterval), TimeSpan.FromSeconds(1));
halibutLimitType.ReflectionSetFieldValue(nameof(HalibutLimits.ConnectionErrorRetryTimeout), TimeSpan.FromSeconds(66)); // Must always be greater than the heartbeat timeout.

// Intentionally set higher than the heart beat, since some tests need to determine that the hart beat timeout applies.
halibutLimitType.ReflectionSetFieldValue(nameof(HalibutLimits.TcpClientSendTimeout), HalfTheTcpReceiveTimeout + HalfTheTcpReceiveTimeout);
halibutLimitType.ReflectionSetFieldValue(nameof(HalibutLimits.TcpClientReceiveTimeout), HalfTheTcpReceiveTimeout + HalfTheTcpReceiveTimeout);

halibutLimitType.ReflectionSetFieldValue(nameof(HalibutLimits.TcpClientHeartbeatSendTimeout), TimeSpan.FromSeconds(15));
halibutLimitType.ReflectionSetFieldValue(nameof(HalibutLimits.TcpClientHeartbeatReceiveTimeout), TimeSpan.FromSeconds(15));
halibutLimitType.ReflectionSetFieldValue(nameof(HalibutLimits.TcpClientConnectTimeout), TimeSpan.FromSeconds(20));
halibutLimitType.ReflectionSetFieldValue(nameof(HalibutLimits.PollingQueueWaitTimeout), TimeSpan.FromSeconds(20));
}

[OneTimeSetUp]
[Obsolete("Remove when NUnit is fully replaced with xUnit")]
public static void LowerHalibutLimits()
{
var halibutLimitType = typeof(HalibutLimits);
Expand Down
28 changes: 14 additions & 14 deletions source/Halibut.Tests/ServiceModel/ServiceInvokerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
using Halibut.Tests.Util;
using Halibut.TestUtils.Contracts;
using Halibut.Transport.Protocol;
using NUnit.Framework;
using Xunit;

namespace Halibut.Tests.ServiceModel
{

public class ServiceInvokerFixture : BaseTest
public class ServiceInvokerFixture : BaseTestXUnit
{
[Test]
[Fact]
public void InvokeWithParams()
{
var serviceFactory = new ServiceFactoryBuilder()
Expand All @@ -33,8 +33,8 @@ public void InvokeWithParams()
var response = sut.Invoke(request);
response.Result.Should().Be($"{value}...");
}
[Test]

[Fact]
public void InvokeWithNoParams()
{
var serviceFactory = new ServiceFactoryBuilder()
Expand All @@ -51,8 +51,8 @@ public void InvokeWithNoParams()
var response = sut.Invoke(request);
response.Result.Should().Be(1);
}
[Test]

[Fact]
public async Task AsyncInvokeWithParamsOnAsyncService()
{
var serviceFactory = new ServiceFactoryBuilder()
Expand All @@ -72,7 +72,7 @@ public async Task AsyncInvokeWithParamsOnAsyncService()
response.Result.Should().Be($"{value}Async...");
}

[Test]
[Fact]
public async Task AsyncInvokeWithNoParamsOnAsyncService()
{
var serviceFactory = new ServiceFactoryBuilder()
Expand All @@ -90,7 +90,7 @@ public async Task AsyncInvokeWithNoParamsOnAsyncService()
response.Result.Should().Be(1);
}

[Test]
[Fact]
public async Task AsyncInvokeWithParamsOnSyncService()
{
var serviceFactory = new ServiceFactoryBuilder()
Expand All @@ -110,7 +110,7 @@ public async Task AsyncInvokeWithParamsOnSyncService()
response.Result.Should().Be($"{value}...");
}

[Test]
[Fact]
public async Task AsyncInvokeWithNoParamsOnSyncService()
{
var serviceFactory = new ServiceFactoryBuilder()
Expand All @@ -128,7 +128,7 @@ public async Task AsyncInvokeWithNoParamsOnSyncService()
response.Result.Should().Be(1);
}

[Test]
[Fact]
public async Task AsyncInvokeWithNoParams_AsyncServiceMissingSuffix()
{
var serviceFactory = new ServiceFactoryBuilder()
Expand All @@ -146,7 +146,7 @@ public async Task AsyncInvokeWithNoParams_AsyncServiceMissingSuffix()
await AssertAsync.Throws<Exception>(() => sut.InvokeAsync(request));
}

[Test]
[Fact]
public async Task AsyncInvokeWithNoParams_AsyncServiceMissingCancellationToken()
{
var serviceFactory = new ServiceFactoryBuilder()
Expand All @@ -164,7 +164,7 @@ public async Task AsyncInvokeWithNoParams_AsyncServiceMissingCancellationToken()
await AssertAsync.Throws<Exception>(() => sut.InvokeAsync(request));
}

[Test]
[Fact]
public async Task AsyncInvokeWithParams_AsyncServiceMissingSuffix()
{
var serviceFactory = new ServiceFactoryBuilder()
Expand All @@ -184,7 +184,7 @@ public async Task AsyncInvokeWithParams_AsyncServiceMissingSuffix()
await AssertAsync.Throws<Exception>(() => sut.InvokeAsync(request));
}

[Test]
[Fact]
public async Task AsyncInvokeWithParams_AsyncServiceMissingCancellationToken()
{
var serviceFactory = new ServiceFactoryBuilder()
Expand Down