-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to dispose linked resources on pipeline disposal (#1511)
- Loading branch information
Showing
13 changed files
with
253 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/Polly.Core/Utils/Pipeline/ComponentWithDisposeCallbacks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace Polly.Utils.Pipeline; | ||
|
||
internal class ComponentWithDisposeCallbacks : PipelineComponent | ||
{ | ||
private readonly List<Action> _callbacks; | ||
|
||
public ComponentWithDisposeCallbacks(PipelineComponent component, List<Action> callbacks) | ||
{ | ||
Component = component; | ||
_callbacks = callbacks; | ||
} | ||
|
||
internal PipelineComponent Component { get; } | ||
|
||
public override void Dispose() | ||
{ | ||
ExecuteCallbacks(); | ||
|
||
Component.Dispose(); | ||
} | ||
|
||
public override ValueTask DisposeAsync() | ||
{ | ||
ExecuteCallbacks(); | ||
|
||
return Component.DisposeAsync(); | ||
} | ||
|
||
internal override ValueTask<Outcome<TResult>> ExecuteCore<TResult, TState>( | ||
Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> callback, | ||
ResilienceContext context, | ||
TState state) => Component.ExecuteCore(callback, context, state); | ||
|
||
private void ExecuteCallbacks() | ||
{ | ||
foreach (var callback in _callbacks) | ||
{ | ||
callback(); | ||
} | ||
|
||
_callbacks.Clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
test/Polly.Core.Tests/Utils/Pipeline/ComponentWithDisposeCallbacksTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using NSubstitute; | ||
using Polly.Utils.Pipeline; | ||
|
||
namespace Polly.Core.Tests.Utils.Pipeline; | ||
|
||
public class ComponentWithDisposeCallbacksTests | ||
{ | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
[Theory] | ||
public async Task Dispose_Ok(bool isAsync) | ||
{ | ||
// Arrange | ||
var called1 = 0; | ||
var called2 = 0; | ||
|
||
var callbacks = new List<Action> | ||
{ | ||
() => called1++, | ||
() => called2++ | ||
}; | ||
var component = Substitute.For<PipelineComponent>(); | ||
var sut = new ComponentWithDisposeCallbacks(component, callbacks); | ||
|
||
// Act | ||
if (isAsync) | ||
{ | ||
await sut.DisposeAsync(); | ||
await sut.DisposeAsync(); | ||
await component.Received(2).DisposeAsync(); | ||
} | ||
else | ||
{ | ||
sut.Dispose(); | ||
#pragma warning disable S3966 // Objects should not be disposed more than once | ||
sut.Dispose(); | ||
#pragma warning restore S3966 // Objects should not be disposed more than once | ||
component.Received(2).Dispose(); | ||
} | ||
|
||
// Assert | ||
callbacks.Should().BeEmpty(); | ||
called1.Should().Be(1); | ||
called2.Should().Be(1); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
test/Polly.Core.Tests/Utils/Pipeline/PipelineComponentFactoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using NSubstitute; | ||
using Polly.Utils.Pipeline; | ||
|
||
namespace Polly.Core.Tests.Utils.Pipeline; | ||
|
||
public class PipelineComponentFactoryTests | ||
{ | ||
[Fact] | ||
public void WithDisposableCallbacks_NoCallbacks_ReturnsOriginalComponent() | ||
{ | ||
var component = Substitute.For<PipelineComponent>(); | ||
var result = PipelineComponentFactory.WithDisposableCallbacks(component, new List<Action>()); | ||
result.Should().BeSameAs(component); | ||
} | ||
|
||
[Fact] | ||
public void PipelineComponentFactory_Should_Return_WrapperComponent_With_Callbacks() | ||
{ | ||
var component = Substitute.For<PipelineComponent>(); | ||
var callbacks = new List<Action> { () => { } }; | ||
|
||
var result = PipelineComponentFactory.WithDisposableCallbacks(component, callbacks); | ||
|
||
result.Should().BeOfType<ComponentWithDisposeCallbacks>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Threading.RateLimiting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Polly.RateLimiting; | ||
using Polly.Registry; | ||
|
||
namespace Polly.Extensions.Tests; | ||
|
||
public class DisposablePipelineTests | ||
{ | ||
[Fact] | ||
public void DisposePipeline_EnsureLinkedResourcesDisposedToo() | ||
{ | ||
var limiters = new List<RateLimiter>(); | ||
|
||
var provider = new ServiceCollection() | ||
.AddResiliencePipeline("my-pipeline", (builder, context) => | ||
{ | ||
var limiter = new ConcurrencyLimiter(new ConcurrencyLimiterOptions | ||
{ | ||
PermitLimit = 1, | ||
QueueLimit = 1 | ||
}); | ||
limiters.Add(limiter); | ||
builder.AddRateLimiter(new RateLimiterStrategyOptions | ||
{ | ||
RateLimiter = args => limiter.AcquireAsync(1, args.Context.CancellationToken) | ||
}); | ||
// when the pipeline instance is disposed, limiter is disposed too | ||
context.OnPipelineDisposed(() => limiter.Dispose()); | ||
}) | ||
.BuildServiceProvider(); | ||
|
||
limiters.Should().HaveCount(0); | ||
provider.GetRequiredService<ResiliencePipelineProvider<string>>().GetPipeline("my-pipeline"); | ||
provider.GetRequiredService<ResiliencePipelineProvider<string>>().GetPipeline("my-pipeline"); | ||
limiters.Should().HaveCount(1); | ||
IsDisposed(limiters[0]).Should().BeFalse(); | ||
|
||
provider.Dispose(); | ||
limiters.Should().HaveCount(1); | ||
IsDisposed(limiters[0]).Should().BeTrue(); | ||
} | ||
|
||
private static bool IsDisposed(RateLimiter limiter) | ||
{ | ||
try | ||
{ | ||
limiter.AcquireAsync(1).AsTask().GetAwaiter().GetResult(); | ||
return false; | ||
} | ||
catch (ObjectDisposedException) | ||
{ | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters