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

Drop custom validation attributes #1351

Merged
merged 1 commit into from
Jun 26, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ public class AdvancedCircuitBreakerStrategyOptions<TResult> : CircuitBreakerStra
/// <remarks>
/// Value must be greater than 0.5 seconds. Defaults to 30 seconds.
/// </remarks>
[TimeSpan("00:00:00.500")]
[Range(typeof(TimeSpan), "00:00:00.500", "1.00:00:00")]
public TimeSpan SamplingDuration { get; set; } = CircuitBreakerConstants.DefaultSamplingDuration;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class CircuitBreakerStrategyOptions<TResult> : ResilienceStrateg
/// Value must be greater than 0.5 seconds.
/// Defaults to 5 seconds.
/// </remarks>
[TimeSpan("00:00:00.500")]
[Range(typeof(TimeSpan), "00:00:00.500", "1.00:00:00")]
public TimeSpan BreakDuration { get; set; } = CircuitBreakerConstants.DefaultBreakDuration;

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Core/Retry/RetryStrategyOptions.TResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class RetryStrategyOptions<TResult> : ResilienceStrategyOptions
/// Defaults to 2 seconds.
/// </para>
/// </remarks>
[TimeSpan("00:00:00", "1.00:00:00")]
[Range(typeof(TimeSpan), "00:00:00", "1.00:00:00")]
public TimeSpan BaseDelay { get; set; } = RetryConstants.DefaultBaseDelay;

/// <summary>
Expand Down
17 changes: 0 additions & 17 deletions src/Polly.Core/Timeout/TimeoutAttribute.cs

This file was deleted.

13 changes: 1 addition & 12 deletions src/Polly.Core/Timeout/TimeoutResilienceStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected internal override async ValueTask<Outcome<TResult>> ExecuteCoreAsync<T
var timeout = DefaultTimeout;
if (TimeoutGenerator is not null)
{
timeout = await GenerateTimeoutAsync(context).ConfigureAwait(context.ContinueOnCapturedContext);
timeout = await TimeoutGenerator!(new TimeoutGeneratorArguments(context)).ConfigureAwait(context.ContinueOnCapturedContext);
}

if (!TimeoutUtil.ShouldApplyTimeout(timeout))
Expand Down Expand Up @@ -80,17 +80,6 @@ protected internal override async ValueTask<Outcome<TResult>> ExecuteCoreAsync<T
return outcome;
}

internal async ValueTask<TimeSpan> GenerateTimeoutAsync(ResilienceContext context)
{
var timeout = await TimeoutGenerator!(new TimeoutGeneratorArguments(context)).ConfigureAwait(context.ContinueOnCapturedContext);
if (!TimeoutUtil.IsTimeoutValid(timeout))
{
return DefaultTimeout;
}

return timeout;
}

private static CancellationTokenRegistration CreateRegistration(CancellationTokenSource cancellationSource, CancellationToken previousToken)
{
if (previousToken.CanBeCanceled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class TimeoutResilienceStrategyBuilderExtensions
/// </summary>
/// <typeparam name="TBuilder">The builder type.</typeparam>
/// <param name="builder">The builder instance.</param>
/// <param name="timeout">The timeout value. This value should be greater than <see cref="TimeSpan.Zero"/> or <see cref="System.Threading.Timeout.InfiniteTimeSpan"/>.</param>
/// <param name="timeout">The timeout value. This value should be greater than <see cref="TimeSpan.Zero"/>.</param>
/// <returns>The same builder instance.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> is <see langword="null"/>.</exception>
/// <exception cref="ValidationException">Thrown when the options produced from the arguments are invalid.</exception>
Expand Down
8 changes: 5 additions & 3 deletions src/Polly.Core/Timeout/TimeoutStrategyOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.ComponentModel.DataAnnotations;

namespace Polly.Timeout;

/// <summary>
Expand All @@ -15,10 +17,10 @@ public class TimeoutStrategyOptions : ResilienceStrategyOptions
/// Gets or sets the default timeout.
/// </summary>
/// <remarks>
/// By default, the value is set to <see cref="System.Threading.Timeout.InfiniteTimeSpan"/> thus making the timeout strategy disabled.
/// Defaults to 30 seconds. This value must be greater than 1 second and less than 24 hours.
/// </remarks>
[Timeout]
public TimeSpan Timeout { get; set; } = System.Threading.Timeout.InfiniteTimeSpan;
[Range(typeof(TimeSpan), "00:00:01", "1.00:00:00")]
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30);

/// <summary>
/// Gets or sets the timeout generator that generates the timeout for a given execution.
Expand Down
10 changes: 0 additions & 10 deletions src/Polly.Core/Timeout/TimeoutUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,4 @@ public static bool ShouldApplyTimeout(TimeSpan timeout)
{
return timeout > TimeSpan.Zero;
}

public static bool IsTimeoutValid(TimeSpan timeout)
{
if (timeout > TimeSpan.Zero)
{
return true;
}

return timeout == System.Threading.Timeout.InfiniteTimeSpan || timeout > TimeSpan.Zero;
}
}
51 changes: 0 additions & 51 deletions src/Polly.Core/Utils/TimeSpanAttribute.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public void InvalidOptions_Validate()

Validation Errors:
The field MinimumThroughput must be between 2 and 2147483647.
The field SamplingDuration must be >= to 00:00:00.5000000.
The field BreakDuration must be >= to 00:00:00.5000000.
The field SamplingDuration must be between 00:00:00.5000000 and 1.00:00:00.
The field BreakDuration must be between 00:00:00.5000000 and 1.00:00:00.
The ShouldHandle field is required.
""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void InvalidOptions_Validate()

Validation Errors:
The field FailureThreshold must be between 1 and 2147483647.
The field BreakDuration must be >= to 00:00:00.5000000.
The field BreakDuration must be between 00:00:00.5000000 and 1.00:00:00.
The ShouldHandle field is required.
""");
}
Expand Down
2 changes: 1 addition & 1 deletion test/Polly.Core.Tests/Retry/RetryStrategyOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Invalid Options

Validation Errors:
The field RetryCount must be between -1 and 100.
The field BaseDelay must be >= to 00:00:00.
The field BaseDelay must be between 00:00:00 and 1.00:00:00.
The ShouldHandle field is required.
""");
}
Expand Down
12 changes: 0 additions & 12 deletions test/Polly.Core.Tests/Timeout/TimeoutAttributeTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public void AddTimeout_InvalidOptions_Throws()

private static TimeSpan GetTimeout(TimeoutResilienceStrategy strategy)
{
if (strategy.TimeoutGenerator == null)
if (strategy.TimeoutGenerator is null)
{
return strategy.DefaultTimeout;
}

return strategy.GenerateTimeoutAsync(ResilienceContext.Get()).Preserve().GetAwaiter().GetResult();
return strategy.TimeoutGenerator(new TimeoutGeneratorArguments(ResilienceContext.Get())).Preserve().GetAwaiter().GetResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public async Task Execute_EnsureOnTimeoutCalled()
public void Execute_NoTimeout(TimeSpan timeout)
{
var called = false;
var sut = CreateSut();
SetTimeout(timeout);
var sut = CreateSut();
sut.Execute(_ => { });

called.Should().BeFalse();
Expand Down
7 changes: 4 additions & 3 deletions test/Polly.Core.Tests/Timeout/TimeoutTestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ public static class TimeoutTestUtils
{
TimeSpan.MinValue,
TimeSpan.Zero,
TimeSpan.FromSeconds(-1)
TimeSpan.FromSeconds(-1),
TimeSpan.FromHours(25),
};

public static readonly TheoryData<TimeSpan> ValidTimeouts = new()
{
System.Threading.Timeout.InfiniteTimeSpan,
TimeSpan.FromSeconds(1)
TimeSpan.FromSeconds(1),
TimeSpan.FromHours(1),
};
}
45 changes: 0 additions & 45 deletions test/Polly.Core.Tests/Utils/TimeSpanAttributeTests.cs

This file was deleted.