diff --git a/src/rm.DelegatingHandlers/FaultWindowSignalingHandler.cs b/src/rm.DelegatingHandlers/FaultWindowSignalingHandler.cs index 9bc4dc8..cc23d44 100644 --- a/src/rm.DelegatingHandlers/FaultWindowSignalingHandler.cs +++ b/src/rm.DelegatingHandlers/FaultWindowSignalingHandler.cs @@ -3,7 +3,6 @@ using System.Threading; using System.Threading.Tasks; using rm.FeatureToggle; -using rm.Random2; namespace rm.DelegatingHandlers; @@ -14,18 +13,22 @@ public class FaultWindowSignalingHandler : DelegatingHandler { private readonly IFaultWindowSignalingHandlerSettings faultWindowSignalingHandlerSettings; - private readonly IProbability probability = new Probability(rng); - private static readonly Random rng = RandomFactory.GetThreadStaticRandom(); + private readonly IProbability probability; private readonly object locker = new object(); private DateTime? faultWindowEndTime = null; /// public FaultWindowSignalingHandler( - IFaultWindowSignalingHandlerSettings faultWindowSignalingHandlerSettings) + IFaultWindowSignalingHandlerSettings faultWindowSignalingHandlerSettings, + Random rng) { this.faultWindowSignalingHandlerSettings = faultWindowSignalingHandlerSettings ?? throw new ArgumentNullException(nameof(faultWindowSignalingHandlerSettings)); + _ = rng + ?? throw new ArgumentNullException(nameof(rng)); + + probability = new Probability(rng); } protected override Task SendAsync( diff --git a/src/rm.DelegatingHandlers/FaultWindowSignalingOnConditionHandler.cs b/src/rm.DelegatingHandlers/FaultWindowSignalingOnConditionHandler.cs index 6391910..3c84634 100644 --- a/src/rm.DelegatingHandlers/FaultWindowSignalingOnConditionHandler.cs +++ b/src/rm.DelegatingHandlers/FaultWindowSignalingOnConditionHandler.cs @@ -3,7 +3,6 @@ using System.Threading; using System.Threading.Tasks; using rm.FeatureToggle; -using rm.Random2; namespace rm.DelegatingHandlers; @@ -14,18 +13,22 @@ public class FaultWindowSignalingOnConditionHandler : DelegatingHandler { private readonly IFaultWindowSignalingOnConditionHandlerSettings faultWindowSignalingOnConditionHandlerSettings; - private readonly IProbability probability = new Probability(rng); - private static readonly Random rng = RandomFactory.GetThreadStaticRandom(); + private readonly IProbability probability; private readonly object locker = new object(); private DateTime? faultWindowEndTime = null; /// public FaultWindowSignalingOnConditionHandler( - IFaultWindowSignalingOnConditionHandlerSettings faultWindowSignalingOnConditionHandlerSettings) + IFaultWindowSignalingOnConditionHandlerSettings faultWindowSignalingOnConditionHandlerSettings, + Random rng) { this.faultWindowSignalingOnConditionHandlerSettings = faultWindowSignalingOnConditionHandlerSettings ?? throw new ArgumentNullException(nameof(faultWindowSignalingOnConditionHandlerSettings)); + _ = rng + ?? throw new ArgumentNullException(nameof(rng)); + + probability = new Probability(rng); } protected override Task SendAsync( diff --git a/src/rm.DelegatingHandlers/ProcrastinatingGaussianHandler.cs b/src/rm.DelegatingHandlers/ProcrastinatingGaussianHandler.cs index 2595236..2f93ad0 100644 --- a/src/rm.DelegatingHandlers/ProcrastinatingGaussianHandler.cs +++ b/src/rm.DelegatingHandlers/ProcrastinatingGaussianHandler.cs @@ -3,7 +3,6 @@ using System.Threading; using System.Threading.Tasks; using rm.Extensions; -using rm.Random2; namespace rm.DelegatingHandlers; @@ -17,14 +16,17 @@ public class ProcrastinatingGaussianHandler : DelegatingHandler { private readonly double mu; private readonly double sigma; - private static readonly Random rng = RandomFactory.GetThreadStaticRandom(); + private readonly Random rng; /// public ProcrastinatingGaussianHandler( - IProcrastinatingGaussianHandlerSettings procrastinatingGaussianHandlerSettings) + IProcrastinatingGaussianHandlerSettings procrastinatingGaussianHandlerSettings, + Random rng) { _ = procrastinatingGaussianHandlerSettings ?? throw new ArgumentNullException(nameof(procrastinatingGaussianHandlerSettings)); + this.rng = rng + ?? throw new ArgumentNullException(nameof(rng)); mu = procrastinatingGaussianHandlerSettings.Mu; sigma = procrastinatingGaussianHandlerSettings.Sigma; diff --git a/src/rm.DelegatingHandlers/ProcrastinatingWithProbabilityHandler.cs b/src/rm.DelegatingHandlers/ProcrastinatingWithProbabilityHandler.cs index 2939bae..f833520 100644 --- a/src/rm.DelegatingHandlers/ProcrastinatingWithProbabilityHandler.cs +++ b/src/rm.DelegatingHandlers/ProcrastinatingWithProbabilityHandler.cs @@ -3,7 +3,6 @@ using System.Threading; using System.Threading.Tasks; using rm.FeatureToggle; -using rm.Random2; namespace rm.DelegatingHandlers; @@ -14,15 +13,19 @@ public class ProcrastinatingWithProbabilityHandler : DelegatingHandler { private readonly IProcrastinatingWithProbabilityHandlerSettings procrastinatingWithProbabilityHandlerSettings; - private readonly IProbability probability = new Probability(rng); - private static readonly Random rng = RandomFactory.GetThreadStaticRandom(); + private readonly IProbability probability; /// public ProcrastinatingWithProbabilityHandler( - IProcrastinatingWithProbabilityHandlerSettings procrastinatingWithProbabilityHandlerSettings) + IProcrastinatingWithProbabilityHandlerSettings procrastinatingWithProbabilityHandlerSettings, + Random rng) { this.procrastinatingWithProbabilityHandlerSettings = procrastinatingWithProbabilityHandlerSettings ?? throw new ArgumentNullException(nameof(procrastinatingWithProbabilityHandlerSettings)); + _ = rng + ?? throw new ArgumentNullException(nameof(rng)); + + probability = new Probability(rng); } protected override async Task SendAsync( diff --git a/src/rm.DelegatingHandlers/ShortCircuitingResponseWithProbabilityHandler.cs b/src/rm.DelegatingHandlers/ShortCircuitingResponseWithProbabilityHandler.cs index 40f1fed..2330843 100644 --- a/src/rm.DelegatingHandlers/ShortCircuitingResponseWithProbabilityHandler.cs +++ b/src/rm.DelegatingHandlers/ShortCircuitingResponseWithProbabilityHandler.cs @@ -5,7 +5,6 @@ using System.Threading; using System.Threading.Tasks; using rm.FeatureToggle; -using rm.Random2; namespace rm.DelegatingHandlers; @@ -16,15 +15,19 @@ public class ShortCircuitingResponseWithProbabilityHandler : DelegatingHandler { private readonly IShortCircuitingResponseWithProbabilityHandlerSettings shortCircuitingResponseWithProbabilityHandlerSettings; - private readonly IProbability probability = new Probability(rng); - private static readonly Random rng = RandomFactory.GetThreadStaticRandom(); + private readonly IProbability probability; /// public ShortCircuitingResponseWithProbabilityHandler( - IShortCircuitingResponseWithProbabilityHandlerSettings shortCircuitingResponseWithProbabilityHandlerSettings) + IShortCircuitingResponseWithProbabilityHandlerSettings shortCircuitingResponseWithProbabilityHandlerSettings, + Random rng) { this.shortCircuitingResponseWithProbabilityHandlerSettings = shortCircuitingResponseWithProbabilityHandlerSettings ?? throw new ArgumentNullException(nameof(shortCircuitingResponseWithProbabilityHandlerSettings)); + _ = rng + ?? throw new ArgumentNullException(nameof(rng)); + + probability = new Probability(rng); } protected override Task SendAsync( diff --git a/src/rm.DelegatingHandlers/ThrowingWithProbabilityHandler.cs b/src/rm.DelegatingHandlers/ThrowingWithProbabilityHandler.cs index 2f38190..603d469 100644 --- a/src/rm.DelegatingHandlers/ThrowingWithProbabilityHandler.cs +++ b/src/rm.DelegatingHandlers/ThrowingWithProbabilityHandler.cs @@ -3,7 +3,6 @@ using System.Threading; using System.Threading.Tasks; using rm.FeatureToggle; -using rm.Random2; namespace rm.DelegatingHandlers; @@ -15,18 +14,22 @@ public class ThrowingWithProbabilityHandler : DelegatingHandler private readonly double probabilityPercentage; private readonly Exception exception; - private readonly IProbability probability = new Probability(rng); - private static readonly Random rng = RandomFactory.GetThreadStaticRandom(); + private readonly IProbability probability; /// public ThrowingWithProbabilityHandler( double probabilityPercentage, - Exception exception) + Exception exception, + Random rng) { this.probabilityPercentage = probabilityPercentage; // funny, no? perhaps, 20% funny? #aurora this.exception = exception ?? throw new ArgumentNullException(nameof(exception)); + _ = rng + ?? throw new ArgumentNullException(nameof(rng)); + + probability = new Probability(rng); } protected override Task SendAsync( diff --git a/src/rm.DelegatingHandlers/rm.DelegatingHandlers.csproj b/src/rm.DelegatingHandlers/rm.DelegatingHandlers.csproj index 53f7217..922fe5a 100644 --- a/src/rm.DelegatingHandlers/rm.DelegatingHandlers.csproj +++ b/src/rm.DelegatingHandlers/rm.DelegatingHandlers.csproj @@ -33,7 +33,6 @@ - diff --git a/tests/rm.DelegatingHandlersTest/FaultWindowSignalingHandlerTests.cs b/tests/rm.DelegatingHandlersTest/FaultWindowSignalingHandlerTests.cs index e853c79..5b66e9b 100644 --- a/tests/rm.DelegatingHandlersTest/FaultWindowSignalingHandlerTests.cs +++ b/tests/rm.DelegatingHandlersTest/FaultWindowSignalingHandlerTests.cs @@ -2,12 +2,15 @@ using AutoFixture.AutoMoq; using NUnit.Framework; using rm.DelegatingHandlers; +using rm.Random2; namespace rm.DelegatingHandlersTest; [TestFixture] public class FaultWindowSignalingHandlerTests { + private static readonly Random rng = RandomFactory.GetThreadStaticRandom(); + [Test] public async Task Signals_Fault() { @@ -20,7 +23,8 @@ public async Task Signals_Fault() ProbabilityPercentage = 100d, FaultDuration = TimeSpan.FromMilliseconds(10), SignalProperty = triggerProperty, - }); + }, + rng); using var invoker = HttpMessageInvokerFactory.Create( fixture.Create(), faultWindowSignalingHandler); @@ -45,7 +49,8 @@ public async Task Does_Not_Signal_Fault() ProbabilityPercentage = 0d, FaultDuration = TimeSpan.FromMilliseconds(10), SignalProperty = triggerProperty, - }); + }, + rng); using var invoker = HttpMessageInvokerFactory.Create( fixture.Create(), faultWindowSignalingHandler); diff --git a/tests/rm.DelegatingHandlersTest/FaultWindowSignalingOnConditionHandlerTests.cs b/tests/rm.DelegatingHandlersTest/FaultWindowSignalingOnConditionHandlerTests.cs index f8a42cf..273c009 100644 --- a/tests/rm.DelegatingHandlersTest/FaultWindowSignalingOnConditionHandlerTests.cs +++ b/tests/rm.DelegatingHandlersTest/FaultWindowSignalingOnConditionHandlerTests.cs @@ -2,12 +2,15 @@ using AutoFixture.AutoMoq; using NUnit.Framework; using rm.DelegatingHandlers; +using rm.Random2; namespace rm.DelegatingHandlersTest; [TestFixture] public class FaultWindowSignalingOnConditionHandlerTests { + private static readonly Random rng = RandomFactory.GetThreadStaticRandom(); + [Test] public async Task Signals_Fault() { @@ -20,7 +23,8 @@ public async Task Signals_Fault() ProbabilityPercentage = 100d, FaultDuration = TimeSpan.FromMilliseconds(10), SignalProperty = triggerProperty, - }); + }, + rng); using var invoker = HttpMessageInvokerFactory.Create( fixture.Create(), faultWindowSignalingOnConditionHandler); @@ -48,7 +52,8 @@ public async Task Does_Not_Signal_Fault_If_Condition_Not_True() ProbabilityPercentage = 100d, FaultDuration = TimeSpan.FromMilliseconds(10), SignalProperty = triggerProperty, - }); + }, + rng); using var invoker = HttpMessageInvokerFactory.Create( fixture.Create(), faultWindowSignalingOnConditionHandler); @@ -73,7 +78,8 @@ public async Task Does_Not_Signal_Fault() ProbabilityPercentage = 0d, FaultDuration = TimeSpan.FromMilliseconds(10), SignalProperty = triggerProperty, - }); + }, + rng); using var invoker = HttpMessageInvokerFactory.Create( fixture.Create(), faultWindowSignalingOnConditionHandler); diff --git a/tests/rm.DelegatingHandlersTest/ProcrastinatingWithProbabilityHandlerTests.cs b/tests/rm.DelegatingHandlersTest/ProcrastinatingWithProbabilityHandlerTests.cs index c6857cf..dc154c3 100644 --- a/tests/rm.DelegatingHandlersTest/ProcrastinatingWithProbabilityHandlerTests.cs +++ b/tests/rm.DelegatingHandlersTest/ProcrastinatingWithProbabilityHandlerTests.cs @@ -3,12 +3,15 @@ using AutoFixture.AutoMoq; using NUnit.Framework; using rm.DelegatingHandlers; +using rm.Random2; namespace rm.DelegatingHandlersTest; [TestFixture] public class ProcrastinatingWithProbabilityHandlerTests { + private static readonly Random rng = RandomFactory.GetThreadStaticRandom(); + [Retry(5)] [Test] public async Task Procrastinates() @@ -21,7 +24,8 @@ public async Task Procrastinates() { ProbabilityPercentage = 100d, DelayInMilliseconds = delayInMilliseconds, - }); + }, + rng); using var invoker = HttpMessageInvokerFactory.Create( fixture.Create(), procrastinatingWithProbabilityHandler); @@ -46,7 +50,8 @@ public async Task Does_Not_Procrastinate() { ProbabilityPercentage = 0d, DelayInMilliseconds = delayInMilliseconds, - }); + }, + rng); using var invoker = HttpMessageInvokerFactory.Create( fixture.Create(), procrastinatingWithProbabilityHandler); diff --git a/tests/rm.DelegatingHandlersTest/ScenarioTests.cs b/tests/rm.DelegatingHandlersTest/ScenarioTests.cs index 3ec98dc..ff93cd2 100644 --- a/tests/rm.DelegatingHandlersTest/ScenarioTests.cs +++ b/tests/rm.DelegatingHandlersTest/ScenarioTests.cs @@ -4,12 +4,15 @@ using NUnit.Framework; using rm.Clock; using rm.DelegatingHandlers; +using rm.Random2; namespace rm.DelegatingHandlersTest; [TestFixture] public class ScenarioTests { + private static readonly Random rng = RandomFactory.GetThreadStaticRandom(); + [Test] public void Retry_To_Fix_Infrequent_TaskCanceledException_Using_HttpMessageInvoker() { @@ -168,7 +171,8 @@ public void Cause_Fault_Window() ProbabilityPercentage = 100d, FaultDuration = TimeSpan.FromMilliseconds(10), SignalProperty = typeof(ThrowingOnConditionHandler).FullName - }); + }, + rng); using var invoker = HttpMessageInvokerFactory.Create( faultWindowSignalingHandler, throwingOnConditionHandler); @@ -192,7 +196,8 @@ public void Cause_Fault_Window_On_Condition() ProbabilityPercentage = 100d, FaultDuration = TimeSpan.FromMilliseconds(10), SignalProperty = typeof(ThrowingOnConditionHandler).FullName - }); + }, + rng); var faultOnConditionHandler = new FaultOnConditionHandler(); using var invoker = HttpMessageInvokerFactory.Create( diff --git a/tests/rm.DelegatingHandlersTest/ShortCircuitingResponseWithProbabilityHandlerTests.cs b/tests/rm.DelegatingHandlersTest/ShortCircuitingResponseWithProbabilityHandlerTests.cs index 469e8ca..101b51b 100644 --- a/tests/rm.DelegatingHandlersTest/ShortCircuitingResponseWithProbabilityHandlerTests.cs +++ b/tests/rm.DelegatingHandlersTest/ShortCircuitingResponseWithProbabilityHandlerTests.cs @@ -3,12 +3,15 @@ using AutoFixture.AutoMoq; using NUnit.Framework; using rm.DelegatingHandlers; +using rm.Random2; namespace rm.DelegatingHandlersTest; [TestFixture] public class ShortCircuitingResponseWithProbabilityHandlerTests { + private static readonly Random rng = RandomFactory.GetThreadStaticRandom(); + [Test] public async Task ShortCircuits() { @@ -22,7 +25,8 @@ public async Task ShortCircuits() ProbabilityPercentage = 100d, StatusCode = statusCode, Content = content, - }); + }, + rng); using var invoker = HttpMessageInvokerFactory.Create( shortCircuitingResponseWithProbabilityHandler); @@ -48,7 +52,8 @@ public async Task Does_Not_ShortCircuit() ProbabilityPercentage = 0d, StatusCode = statusCode, Content = content, - }); + }, + rng); using var invoker = HttpMessageInvokerFactory.Create( fixture.Create(), shortCircuitingResponseWithProbabilityHandler); diff --git a/tests/rm.DelegatingHandlersTest/ThrowingWithProbabilityHandlerTests.cs b/tests/rm.DelegatingHandlersTest/ThrowingWithProbabilityHandlerTests.cs index 4519b8d..ad640f0 100644 --- a/tests/rm.DelegatingHandlersTest/ThrowingWithProbabilityHandlerTests.cs +++ b/tests/rm.DelegatingHandlersTest/ThrowingWithProbabilityHandlerTests.cs @@ -2,18 +2,21 @@ using AutoFixture.AutoMoq; using NUnit.Framework; using rm.DelegatingHandlers; +using rm.Random2; namespace rm.DelegatingHandlersTest; [TestFixture] public class ThrowingWithProbabilityHandlerTests { + private static readonly Random rng = RandomFactory.GetThreadStaticRandom(); + [Test] public void Throws() { var fixture = new Fixture().Customize(new AutoMoqCustomization()); - var throwingWithProbabilityHandler = new ThrowingWithProbabilityHandler(100d, new TurnDownForWhatException()); + var throwingWithProbabilityHandler = new ThrowingWithProbabilityHandler(100d, new TurnDownForWhatException(), rng); using var invoker = HttpMessageInvokerFactory.Create( throwingWithProbabilityHandler); @@ -30,7 +33,7 @@ public void Does_Not_Throw() { var fixture = new Fixture().Customize(new AutoMoqCustomization()); - var throwingWithProbabilityHandler = new ThrowingWithProbabilityHandler(0d, new TurnDownForWhatException()); + var throwingWithProbabilityHandler = new ThrowingWithProbabilityHandler(0d, new TurnDownForWhatException(), rng); using var invoker = HttpMessageInvokerFactory.Create( fixture.Create(), throwingWithProbabilityHandler); diff --git a/tests/rm.DelegatingHandlersTest/rm.DelegatingHandlersTest.csproj b/tests/rm.DelegatingHandlersTest/rm.DelegatingHandlersTest.csproj index 850e300..41f7615 100644 --- a/tests/rm.DelegatingHandlersTest/rm.DelegatingHandlersTest.csproj +++ b/tests/rm.DelegatingHandlersTest/rm.DelegatingHandlersTest.csproj @@ -16,6 +16,7 @@ +