Skip to content

Commit

Permalink
Allow adding generic strategies to generic builder (#1386)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Jul 4, 2023
1 parent a5007c1 commit 2de8fc0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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);
}
}

0 comments on commit 2de8fc0

Please sign in to comment.