From 3dc91606e3149b2a6287aab293b909945b171bec Mon Sep 17 00:00:00 2001 From: Martin Tomka Date: Wed, 26 Jul 2023 11:04:21 +0200 Subject: [PATCH] Allow isolate CircuitBreakerManualControl using constructor --- .../CircuitBreakerManualControl.cs | 13 ++++++++++++ src/Polly.Core/PublicAPI.Unshipped.txt | 1 + .../CircuitBreakerManualControlTests.cs | 21 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/Polly.Core/CircuitBreaker/CircuitBreakerManualControl.cs b/src/Polly.Core/CircuitBreaker/CircuitBreakerManualControl.cs index 8ff312d71b6..a2637a808be 100644 --- a/src/Polly.Core/CircuitBreaker/CircuitBreakerManualControl.cs +++ b/src/Polly.Core/CircuitBreaker/CircuitBreakerManualControl.cs @@ -15,6 +15,19 @@ public sealed class CircuitBreakerManualControl : IDisposable private readonly HashSet> _onReset = new(); private bool _isolated; + /// + /// Initializes a new instance of the class. + /// + public CircuitBreakerManualControl() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Determines whether the circit breaker is isolated immediately after construction. + public CircuitBreakerManualControl(bool isIsolated) => _isolated = isIsolated; + internal void Initialize(Func onIsolate, Func onReset, Action onDispose) { _onDispose.Add(onDispose); diff --git a/src/Polly.Core/PublicAPI.Unshipped.txt b/src/Polly.Core/PublicAPI.Unshipped.txt index 29884cc11e7..4b1c5591242 100644 --- a/src/Polly.Core/PublicAPI.Unshipped.txt +++ b/src/Polly.Core/PublicAPI.Unshipped.txt @@ -32,6 +32,7 @@ Polly.CircuitBreaker.BrokenCircuitException.BrokenCircuitException(TRes Polly.CircuitBreaker.BrokenCircuitException.Result.get -> TResult Polly.CircuitBreaker.CircuitBreakerManualControl Polly.CircuitBreaker.CircuitBreakerManualControl.CircuitBreakerManualControl() -> void +Polly.CircuitBreaker.CircuitBreakerManualControl.CircuitBreakerManualControl(bool isIsolated) -> void Polly.CircuitBreaker.CircuitBreakerManualControl.CloseAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Polly.CircuitBreaker.CircuitBreakerManualControl.Dispose() -> void Polly.CircuitBreaker.CircuitBreakerManualControl.IsolateAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! diff --git a/test/Polly.Core.Tests/CircuitBreaker/CircuitBreakerManualControlTests.cs b/test/Polly.Core.Tests/CircuitBreaker/CircuitBreakerManualControlTests.cs index 5cfe45d1867..8af153f1369 100644 --- a/test/Polly.Core.Tests/CircuitBreaker/CircuitBreakerManualControlTests.cs +++ b/test/Polly.Core.Tests/CircuitBreaker/CircuitBreakerManualControlTests.cs @@ -4,6 +4,27 @@ namespace Polly.Core.Tests.CircuitBreaker; public class CircuitBreakerManualControlTests { + [InlineData(true)] + [InlineData(false)] + [Theory] + public void Ctor_Isolated(bool isolated) + { + using var control = new CircuitBreakerManualControl(isolated); + var isolateCalled = false; + + control.Initialize( + c => + { + c.IsSynchronous.Should().BeTrue(); + isolateCalled = true; + return Task.CompletedTask; + }, + _ => Task.CompletedTask, + () => { }); + + isolateCalled.Should().Be(isolated); + } + [InlineData(true)] [InlineData(false)] [Theory]