Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xakep139 authored and github-actions committed May 9, 2024
1 parent 6d3eb4c commit 80d518b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.Http.Resilience.Test.Hedging;
using Polly;
using Polly.Retry;
using Xunit;
Expand All @@ -19,16 +21,15 @@ public class HttpRetryStrategyOptionsTests
#pragma warning disable S2330
public static readonly IEnumerable<object[]> HandledExceptionsClassified = new[]
{
new object[] { new InvalidCastException(), false },
new object[] { new HttpRequestException(), true }
new object[] { new InvalidCastException(), null!, false },
[new HttpRequestException(), null!, true],
[new OperationCanceledExceptionMock(new TimeoutException()), null!, true],
[new OperationCanceledExceptionMock(new TimeoutException()), default(CancellationToken), true],
[new OperationCanceledExceptionMock(new InvalidOperationException()), default(CancellationToken), false],
[new OperationCanceledExceptionMock(new TimeoutException()), new CancellationToken(canceled: true), false],
};

private readonly HttpRetryStrategyOptions _testClass;

public HttpRetryStrategyOptionsTests()
{
_testClass = new HttpRetryStrategyOptions();
}
private readonly HttpRetryStrategyOptions _testClass = new();

[Fact]
public void Ctor_Defaults()
Expand Down Expand Up @@ -63,9 +64,10 @@ public async Task ShouldHandleResultAsError_DefaultValue_ShouldClassify(HttpStat

[Theory]
[MemberData(nameof(HandledExceptionsClassified))]
public async Task ShouldHandleException_DefaultValue_ShouldClassify(Exception exception, bool expectedToHandle)
public async Task ShouldHandleException_DefaultValue_ShouldClassify(Exception exception, CancellationToken? token, bool expectedToHandle)
{
var shouldHandle = await _testClass.ShouldHandle(CreateArgs(Outcome.FromException<HttpResponseMessage>(exception)));
var args = CreateArgs(Outcome.FromException<HttpResponseMessage>(exception), token ?? default);
var shouldHandle = await _testClass.ShouldHandle(args);
Assert.Equal(expectedToHandle, shouldHandle);
}

Expand All @@ -86,17 +88,18 @@ public async Task ShouldHandleResultAsError_DefaultInstance_ShouldClassify(HttpS

[Theory]
[MemberData(nameof(HandledExceptionsClassified))]
public async Task ShouldHandleException_DefaultInstance_ShouldClassify(Exception exception, bool expectedToHandle)
public async Task ShouldHandleException_DefaultInstance_ShouldClassify(Exception exception, CancellationToken? token, bool expectedToHandle)
{
var shouldHandle = await new HttpRetryStrategyOptions().ShouldHandle(CreateArgs(Outcome.FromException<HttpResponseMessage>(exception)));
var args = CreateArgs(Outcome.FromException<HttpResponseMessage>(exception), token ?? default);
var shouldHandle = await new HttpRetryStrategyOptions().ShouldHandle(args);
Assert.Equal(expectedToHandle, shouldHandle);
}

[Fact]
public async Task ShouldRetryAfterHeader_InvalidOutcomes_ShouldReturnNull()
{
var options = new HttpRetryStrategyOptions { ShouldRetryAfterHeader = true };
using var responseMessage = new HttpResponseMessage { };
using var responseMessage = new HttpResponseMessage();

Assert.NotNull(options.DelayGenerator);

Expand Down Expand Up @@ -153,7 +156,8 @@ public void GetDelayGenerator_ShouldGetBasedOnShouldRetryAfterHeader(bool should
Assert.Equal(shouldRetryAfterHeader, options.DelayGenerator != null);
}

private static RetryPredicateArguments<HttpResponseMessage> CreateArgs(Outcome<HttpResponseMessage> outcome)
=> new(ResilienceContextPool.Shared.Get(), outcome, 0);

private static RetryPredicateArguments<HttpResponseMessage> CreateArgs(
Outcome<HttpResponseMessage> outcome,
CancellationToken cancellationToken = default)
=> new(ResilienceContextPool.Shared.Get(cancellationToken), outcome, 0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void AddStandardResilienceHandler_ConfigurationPropertyWithTypo_Throws(Me

AddStandardResilienceHandler(mode, builder, _invalidConfigurationSection, options => { });

Assert.Throws<InvalidOperationException>(() => HttpClientBuilderExtensionsTests.GetPipeline(builder.Services, $"test-standard"));
Assert.Throws<InvalidOperationException>(() => HttpClientBuilderExtensionsTests.GetPipeline(builder.Services, "test-standard"));
}

[Fact]
Expand Down Expand Up @@ -175,7 +175,7 @@ public void AddStandardResilienceHandler_EnsureValidated(bool wholePipeline)
}
});

Assert.Throws<OptionsValidationException>(() => GetPipeline(builder.Services, $"test-standard"));
Assert.Throws<OptionsValidationException>(() => GetPipeline(builder.Services, "test-standard"));
}

[InlineData(MethodArgs.None)]
Expand All @@ -193,7 +193,7 @@ public void AddStandardResilienceHandler_EnsureConfigured(MethodArgs mode)

AddStandardResilienceHandler(mode, builder, _validConfigurationSection, options => { });

var pipeline = GetPipeline(builder.Services, $"test-standard");
var pipeline = GetPipeline(builder.Services, "test-standard");
Assert.NotNull(pipeline);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ namespace Microsoft.Extensions.Http.Resilience.Test.Resilience;

public class HttpStandardResilienceOptionsTests
{
private readonly HttpStandardResilienceOptions _options;

public HttpStandardResilienceOptionsTests()
{
_options = new HttpStandardResilienceOptions();
}
private readonly HttpStandardResilienceOptions _options = new();

[Fact]
public void Ctor_EnsureDefaults()
Expand Down

0 comments on commit 80d518b

Please sign in to comment.