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

xunit v3 preparation #2442

Merged
merged 1 commit into from
Jan 10, 2025
Merged
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
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
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 = CancellationToken.None;
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())
.Should()
.NotThrowAsync();
}
Expand All @@ -65,12 +66,13 @@ await control
public async Task Initialize_Twice_Ok()
{
int called = 0;
var cancellationToken = CancellationToken.None;
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 = CancellationToken.None;
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 = CancellationToken.None;
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 @@ -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(CancellationToken.None),
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 = CancellationToken.None;
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 = CancellationToken.None;
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 = CancellationToken.None;
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 => CancellationToken.None;

[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
Loading
Loading