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

Allow adding generic strategies to generic builder #1386

Merged
merged 1 commit into from
Jul 4, 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
17 changes: 17 additions & 0 deletions src/Polly.Core/ResilienceStrategyBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ public static TBuilder AddStrategy<TBuilder>(this TBuilder builder, ResilienceSt
return builder.AddStrategy(_ => strategy, EmptyOptions.Instance);
}

/// <summary>
/// Adds an already created strategy instance to the builder.
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="builder">The builder instance.</param>
/// <param name="strategy">The strategy instance.</param>
/// <returns>The same builder instance.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="strategy"/> is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when this builder was already used to create a strategy. The builder cannot be modified after it has been used.</exception>
public static ResilienceStrategyBuilder<TResult> AddStrategy<TResult>(this ResilienceStrategyBuilder<TResult> builder, ResilienceStrategy<TResult> strategy)
{
Guard.NotNull(builder);
Guard.NotNull(strategy);

return builder.AddStrategy(strategy.Strategy);
}

/// <summary>
/// Adds a strategy to the builder.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions test/Polly.Core.Tests/GenericResilienceStrategyBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,19 @@ public void Build_Ok()
strategy.Should().NotBeNull();
strategy.Strategy.Should().BeOfType<ResilienceStrategyPipeline>().Subject.Strategies.Should().HaveCount(2);
}

[Fact]
public void AddGenericStrategy_Ok()
{
// arrange
var testStrategy = new ResilienceStrategy<string>(new TestResilienceStrategy());
_builder.AddStrategy(testStrategy);

// act
var strategy = _builder.Build();

// assert
strategy.Should().NotBeNull();
strategy.Strategy.Should().Be(testStrategy.Strategy);
}
}