Skip to content

Commit

Permalink
Introduce Outcome.EnsureSuccess (#1243)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Jun 1, 2023
1 parent 4ec50a9 commit b6f8d2b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
16 changes: 16 additions & 0 deletions src/Polly.Core.Tests/Strategy/OutcomeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,20 @@ public void ToString_NullResult_ShouldBeEmpty()
var outcome = new Outcome<object>((object)null!);
outcome.ToString().Should().BeEmpty();
}

[Fact]
public void EnsureSuccess_Result()
{
var outcome = new Outcome<string>("dummy");

outcome.Invoking(o => o.EnsureSuccess()).Should().NotThrow();
}

[Fact]
public void EnsureSuccess_Exception()
{
var outcome = new Outcome<string>(new InvalidOperationException());

outcome.Invoking(o => o.EnsureSuccess()).Should().Throw<InvalidOperationException>();
}
}
8 changes: 1 addition & 7 deletions src/Polly.Core/Registry/ConfigureBuilderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ namespace Polly.Registry;
public class ConfigureBuilderContext<TKey>
where TKey : notnull
{
/// <summary>
/// Initializes a new instance of the <see cref="ConfigureBuilderContext{TKey}"/> class.
/// </summary>
/// <param name="strategyKey">The strategy key.</param>
/// <param name="builderName">The builder name.</param>
/// <param name="strategyKeyString">The strategy key as string.</param>
public ConfigureBuilderContext(TKey strategyKey, string builderName, string strategyKeyString)
internal ConfigureBuilderContext(TKey strategyKey, string builderName, string strategyKeyString)
{
StrategyKey = strategyKey;
BuilderName = builderName;
Expand Down
8 changes: 8 additions & 0 deletions src/Polly.Core/Strategy/Outcome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public Outcome(TResult result)
/// </summary>
public bool IsVoidResult => Result is VoidResult;

/// <summary>
/// Throws an exception if the operation produced an exception.
/// </summary>
/// <remarks>
/// If the operation produced a result, this method does nothing. The thrown exception maintains its original stack trace.
/// </remarks>
public void EnsureSuccess() => ExceptionDispatchInfo?.Throw();

/// <summary>
/// Tries to get the result, if available.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public void AddResilienceStrategy_EnsureContextFilled(bool generic)
{
_services.AddResilienceStrategy<string, string>(Key, (builder, context) =>
{
context.RegistryContext.Should().NotBeNull();
context.StrategyKey.Should().Be(Key);
builder.Should().NotBeNull();
context.ServiceProvider.Should().NotBeNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@ namespace Polly.Extensions.DependencyInjection;
/// Represents the context for adding a resilience strategy with the specified key.
/// </summary>
/// <typeparam name="TKey">The type of the key used to identify the resilience strategy.</typeparam>
public sealed class AddResilienceStrategyContext<TKey> : ConfigureBuilderContext<TKey>
public sealed class AddResilienceStrategyContext<TKey>
where TKey : notnull
{
internal AddResilienceStrategyContext(ConfigureBuilderContext<TKey> context, IServiceProvider serviceProvider)
: base(context.StrategyKey, context.BuilderName, context.StrategyKeyString) => ServiceProvider = serviceProvider;
internal AddResilienceStrategyContext(ConfigureBuilderContext<TKey> registryContext, IServiceProvider serviceProvider)
{
RegistryContext = registryContext;
ServiceProvider = serviceProvider;
StrategyKey = registryContext.StrategyKey;
}

/// <summary>
/// Gets the strategy key for the strategy being created.
/// </summary>
public TKey StrategyKey { get; }

/// <summary>
/// Gets the <see cref="IServiceProvider"/> that provides access to the dependency injection container.
/// </summary>
public IServiceProvider ServiceProvider { get; }

/// <summary>
/// Gets the context that is used by the registry.
/// </summary>
public ConfigureBuilderContext<TKey> RegistryContext { get; }
}

0 comments on commit b6f8d2b

Please sign in to comment.