Skip to content

Commit

Permalink
Update to xunit v3
Browse files Browse the repository at this point in the history
- Update to xunit v3.
- Fix some IDE0055 warnings.
- Invert some pre-processor directives.
- Use generic assertions where relevant.
- Use collection expressions where relevant.
- Remove redundant using statements.
- Improve some assertions.

Resolves #2439.
  • Loading branch information
martincostello committed Jan 10, 2025
1 parent 8548e9a commit 000727c
Show file tree
Hide file tree
Showing 157 changed files with 1,050 additions and 724 deletions.
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageVersion Include="System.Text.Json" Version="9.0.0" />
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.0" />
<PackageVersion Include="xunit.v3" Version="1.0.1" />
<PackageVersion Include="xunit.v3.assert" Version="1.0.1" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var configuration = Argument<string>("configuration", "Release");
// EXTERNAL NUGET TOOLS
//////////////////////////////////////////////////////////////////////

#Tool "xunit.runner.console&version=2.9.2"
#Tool "xunit.v3.runner.console&version=1.0.1"
#Tool "dotnet-stryker&version=4.4.1"

//////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion eng/Test.targets
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="ReportGenerator" PrivateAssets="all" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" PrivateAssets="all" />
<PackageReference Include="xunit.v3" />
</ItemGroup>

<PropertyGroup Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) != '.NETFramework'">
Expand Down
2 changes: 2 additions & 0 deletions src/Snippets/Docs/Testing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public static void PipelineProviderProviderMocking()
}
}

#pragma warning disable IDE0290
#region testing-resilience-pipeline-provider-usage

// Represents an arbitrary API that needs resilience support
Expand Down Expand Up @@ -124,3 +125,4 @@ public static IServiceCollection AddMyApi(this IServiceCollection services)
}

#endregion
#pragma warning restore IDE0290
2 changes: 1 addition & 1 deletion src/Snippets/Snippets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.v3.assert" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void Ctor_Message_RetryAfter_InnerException_Ok()
exception.RetryAfter.Should().Be(TestRetryAfter);
}

#if !NETCOREAPP
#if NETFRAMEWORK
[Fact]
public void BinarySerialization_NonNullRetryAfter_Ok()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public void Ctor_Isolated(bool isolated)
[Theory]
public async Task IsolateAsync_NotInitialized_Ok(bool closedAfter)
{
var cancellationToken = TestContext.Current.CancellationToken;
var control = new CircuitBreakerManualControl();
await control.IsolateAsync();
await control.IsolateAsync(cancellationToken);
if (closedAfter)
{
await control.CloseAsync();
await control.CloseAsync(cancellationToken);
}

var isolated = false;
Expand All @@ -56,7 +57,7 @@ public async Task ResetAsync_NotInitialized_Ok()
var control = new CircuitBreakerManualControl();

await control
.Invoking(c => c.CloseAsync(CancellationToken.None))
.Invoking(c => c.CloseAsync(TestContext.Current.CancellationToken))
.Should()
.NotThrowAsync();
}
Expand All @@ -65,12 +66,13 @@ await control
public async Task Initialize_Twice_Ok()
{
int called = 0;
var cancellationToken = TestContext.Current.CancellationToken;
var control = new CircuitBreakerManualControl();
control.Initialize(_ => Task.CompletedTask, _ => Task.CompletedTask);
control.Initialize(_ => { called++; return Task.CompletedTask; }, _ => { called++; return Task.CompletedTask; });

await control.IsolateAsync();
await control.CloseAsync();
await control.IsolateAsync(cancellationToken);
await control.CloseAsync(cancellationToken);

called.Should().Be(2);
}
Expand All @@ -79,23 +81,25 @@ public async Task Initialize_Twice_Ok()
public async Task Initialize_DisposeRegistration_ShuldBeCancelled()
{
int called = 0;
var cancellationToken = TestContext.Current.CancellationToken;
var control = new CircuitBreakerManualControl();
var reg = control.Initialize(_ => { called++; return Task.CompletedTask; }, _ => { called++; return Task.CompletedTask; });

await control.IsolateAsync();
await control.CloseAsync();
await control.IsolateAsync(cancellationToken);
await control.CloseAsync(cancellationToken);

reg.Dispose();

await control.IsolateAsync();
await control.CloseAsync();
await control.IsolateAsync(cancellationToken);
await control.CloseAsync(cancellationToken);

called.Should().Be(2);
}

[Fact]
public async Task Initialize_Ok()
{
var cancellationToken = TestContext.Current.CancellationToken;
var control = new CircuitBreakerManualControl();
var isolateCalled = false;
var resetCalled = false;
Expand All @@ -116,8 +120,8 @@ public async Task Initialize_Ok()
return Task.CompletedTask;
});

await control.IsolateAsync(CancellationToken.None);
await control.CloseAsync(CancellationToken.None);
await control.IsolateAsync(cancellationToken);
await control.CloseAsync(cancellationToken);

isolateCalled.Should().BeTrue();
resetCalled.Should().BeTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CircuitBreakerOptionsTests
public async Task ShouldHandle_EnsureDefaults()
{
var options = new CircuitBreakerStrategyOptions();
var context = ResilienceContextPool.Shared.Get();
var context = ResilienceContextPool.Shared.Get(TestContext.Current.CancellationToken);

(await options.ShouldHandle(new CircuitBreakerPredicateArguments<object>(context, Outcome.FromResult<object>("dummy")))).Should().Be(false);
(await options.ShouldHandle(new CircuitBreakerPredicateArguments<object>(context, Outcome.FromException<object>(new OperationCanceledException())))).Should().Be(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Polly.Core.Tests.CircuitBreaker;

public class CircuitBreakerPredicateArgumentsTests
public static class CircuitBreakerPredicateArgumentsTests
{
[Fact]
public void Ctor_Ok()
public static void Ctor_Ok()
{
var args = new CircuitBreakerPredicateArguments<int>(ResilienceContextPool.Shared.Get(), Outcome.FromResult(1));
var args = new CircuitBreakerPredicateArguments<int>(
ResilienceContextPool.Shared.Get(TestContext.Current.CancellationToken),
Outcome.FromResult(1));

args.Context.Should().NotBeNull();
args.Outcome.Result.Should().Be(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void AddCircuitBreaker_Validation()
[Fact]
public void AddCircuitBreaker_IntegrationTest()
{
var cancellationToken = TestContext.Current.CancellationToken;
int opened = 0;
int closed = 0;
int halfOpened = 0;
Expand All @@ -88,36 +89,36 @@ public void AddCircuitBreaker_IntegrationTest()

for (int i = 0; i < 10; i++)
{
strategy.Execute(_ => -1);
strategy.Execute(_ => -1, cancellationToken);
}

// Circuit opened
opened.Should().Be(1);
halfOpened.Should().Be(0);
closed.Should().Be(0);
BrokenCircuitException exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
BrokenCircuitException exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0, cancellationToken));
exception.RetryAfter.Should().Be(breakDuration);

// Circuit still open after some time
timeProvider.Advance(halfBreakDuration);
opened.Should().Be(1);
halfOpened.Should().Be(0);
closed.Should().Be(0);
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0, cancellationToken));
exception.RetryAfter.Should().Be(halfBreakDuration);

// Circuit Half Opened
timeProvider.Advance(halfBreakDuration);
strategy.Execute(_ => -1);
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
strategy.Execute(_ => -1, cancellationToken);
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0, cancellationToken));
opened.Should().Be(2);
halfOpened.Should().Be(1);
closed.Should().Be(0);
exception.RetryAfter.Should().Be(breakDuration);

// Now close it
timeProvider.Advance(breakDuration);
strategy.Execute(_ => 0);
strategy.Execute(_ => 0, cancellationToken);
opened.Should().Be(2);
halfOpened.Should().Be(2);
closed.Should().Be(1);
Expand All @@ -126,6 +127,7 @@ public void AddCircuitBreaker_IntegrationTest()
[Fact]
public void AddCircuitBreaker_IntegrationTest_WithBreakDurationGenerator()
{
var cancellationToken = TestContext.Current.CancellationToken;
int opened = 0;
int closed = 0;
int halfOpened = 0;
Expand All @@ -151,36 +153,36 @@ public void AddCircuitBreaker_IntegrationTest_WithBreakDurationGenerator()

for (int i = 0; i < 10; i++)
{
strategy.Execute(_ => -1);
strategy.Execute(_ => -1, cancellationToken);
}

// Circuit opened
opened.Should().Be(1);
halfOpened.Should().Be(0);
closed.Should().Be(0);
BrokenCircuitException exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
BrokenCircuitException exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0, cancellationToken));
exception.RetryAfter.Should().Be(breakDuration);

// Circuit still open after some time
timeProvider.Advance(halfBreakDuration);
opened.Should().Be(1);
halfOpened.Should().Be(0);
closed.Should().Be(0);
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0, cancellationToken));
exception.RetryAfter.Should().Be(halfBreakDuration);

// Circuit Half Opened
timeProvider.Advance(halfBreakDuration);
strategy.Execute(_ => -1);
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
strategy.Execute(_ => -1, cancellationToken);
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0, cancellationToken));
opened.Should().Be(2);
halfOpened.Should().Be(1);
closed.Should().Be(0);
exception.RetryAfter.Should().Be(breakDuration);

// Now close it
timeProvider.Advance(breakDuration);
strategy.Execute(_ => 0);
strategy.Execute(_ => 0, cancellationToken);
opened.Should().Be(2);
halfOpened.Should().Be(2);
closed.Should().Be(1);
Expand All @@ -189,8 +191,9 @@ public void AddCircuitBreaker_IntegrationTest_WithBreakDurationGenerator()
[Fact]
public async Task AddCircuitBreakers_WithIsolatedManualControl_ShouldBeIsolated()
{
var cancellationToken = TestContext.Current.CancellationToken;
var manualControl = new CircuitBreakerManualControl();
await manualControl.IsolateAsync();
await manualControl.IsolateAsync(cancellationToken);

var strategy1 = new ResiliencePipelineBuilder()
.AddCircuitBreaker(new() { ManualControl = manualControl })
Expand All @@ -203,7 +206,7 @@ public async Task AddCircuitBreakers_WithIsolatedManualControl_ShouldBeIsolated(
strategy1.Invoking(s => s.Execute(() => { })).Should().Throw<IsolatedCircuitException>().Where(e => e.RetryAfter == null);
strategy2.Invoking(s => s.Execute(() => { })).Should().Throw<IsolatedCircuitException>().Where(e => e.RetryAfter == null);

await manualControl.CloseAsync();
await manualControl.CloseAsync(cancellationToken);

strategy1.Execute(() => { });
strategy2.Execute(() => { });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public CircuitBreakerResilienceStrategyTests()
null);
}

private static CancellationToken CancellationToken => TestContext.Current.CancellationToken;

[Fact]
public void Ctor_Ok() =>
this.Invoking(_ => Create()).Should().NotThrow();
Expand All @@ -52,10 +54,10 @@ public async Task Ctor_ManualControl_EnsureAttached()
_options.ManualControl = new CircuitBreakerManualControl();
var strategy = Create();

await _options.ManualControl.IsolateAsync(CancellationToken.None);
await _options.ManualControl.IsolateAsync(CancellationToken);
strategy.Invoking(s => s.Execute(_ => 0)).Should().Throw<IsolatedCircuitException>().Where(e => e.RetryAfter == null);

await _options.ManualControl.CloseAsync(CancellationToken.None);
await _options.ManualControl.CloseAsync(CancellationToken);

strategy.Invoking(s => s.Execute(_ => 0)).Should().NotThrow();

Expand All @@ -73,7 +75,7 @@ public void Execute_HandledResult_OnFailureCalled()
_behavior.When(v => v.OnActionFailure(CircuitState.Closed, out Arg.Any<bool>()))
.Do(x => x[1] = shouldBreak);

strategy.Execute(_ => -1).Should().Be(-1);
strategy.Execute(_ => -1, CancellationToken).Should().Be(-1);

_behavior.Received().OnActionFailure(CircuitState.Closed, out Arg.Any<bool>());
}
Expand All @@ -84,7 +86,7 @@ public void Execute_UnhandledResult_OnActionSuccess()
_options.ShouldHandle = args => new ValueTask<bool>(args.Outcome.Result is -1);
var strategy = Create();

strategy.Execute(_ => 0).Should().Be(0);
strategy.Execute(_ => 0, CancellationToken).Should().Be(0);

_behavior.Received(1).OnActionSuccess(CircuitState.Closed);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task ResetAsync_NotInitialized_Throws()
var control = new CircuitBreakerManualControl();

await control
.Invoking(c => c.CloseAsync(CancellationToken.None))
.Invoking(c => c.CloseAsync(TestContext.Current.CancellationToken))
.Should()
.NotThrowAsync<InvalidOperationException>();
}
Expand Down
Loading

0 comments on commit 000727c

Please sign in to comment.