Skip to content

Commit

Permalink
random: Take Random as dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
rmandvikar committed Nov 6, 2023
1 parent 8c2b61b commit 0c2fa56
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 42 deletions.
12 changes: 7 additions & 5 deletions src/rm.DelegatingHandlers/FaultWindowSignalingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading;
using System.Threading.Tasks;
using rm.FeatureToggle;
using rm.Random2;

namespace rm.DelegatingHandlers;

Expand All @@ -13,19 +12,22 @@ namespace rm.DelegatingHandlers;
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;

/// <inheritdoc cref="FaultWindowSignalingHandler" />
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<HttpResponseMessage> SendAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading;
using System.Threading.Tasks;
using rm.FeatureToggle;
using rm.Random2;

namespace rm.DelegatingHandlers;

Expand All @@ -13,19 +12,22 @@ namespace rm.DelegatingHandlers;
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;

/// <inheritdoc cref="FaultWindowSignalingOnConditionHandler" />
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<HttpResponseMessage> SendAsync(
Expand Down
8 changes: 5 additions & 3 deletions src/rm.DelegatingHandlers/ProcrastinatingGaussianHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading;
using System.Threading.Tasks;
using rm.Extensions;
using rm.Random2;

namespace rm.DelegatingHandlers;

Expand All @@ -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;

/// <inheritdoc cref="ProcrastinatingGaussianHandler" />
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading;
using System.Threading.Tasks;
using rm.FeatureToggle;
using rm.Random2;

namespace rm.DelegatingHandlers;

Expand All @@ -13,16 +12,19 @@ namespace rm.DelegatingHandlers;
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;

/// <inheritdoc cref="ProcrastinatingWithProbabilityHandler" />
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<HttpResponseMessage> SendAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading;
using System.Threading.Tasks;
using rm.FeatureToggle;
using rm.Random2;

namespace rm.DelegatingHandlers;

Expand All @@ -15,16 +14,19 @@ namespace rm.DelegatingHandlers;
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;

/// <inheritdoc cref="ShortCircuitingResponseWithProbabilityHandler" />
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<HttpResponseMessage> SendAsync(
Expand Down
12 changes: 7 additions & 5 deletions src/rm.DelegatingHandlers/ThrowingWithProbabilityHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading;
using System.Threading.Tasks;
using rm.FeatureToggle;
using rm.Random2;

namespace rm.DelegatingHandlers;

Expand All @@ -14,19 +13,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;

/// <inheritdoc cref="ThrowingWithProbabilityHandler" />
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<HttpResponseMessage> SendAsync(
Expand Down
1 change: 0 additions & 1 deletion src/rm.DelegatingHandlers/rm.DelegatingHandlers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<PackageReference Include="rm.Clock" Version="1.0.1" />
<PackageReference Include="rm.Extensions" Version="2.8.2" />
<PackageReference Include="rm.FeatureToggle" Version="1.1.2" />
<PackageReference Include="rm.Random2" Version="3.0.1" />
<PackageReference Include="Serilog" Version="2.7.1" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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<HttpMessageHandler>(), faultWindowSignalingHandler);
Expand All @@ -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<HttpMessageHandler>(), faultWindowSignalingHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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<HttpMessageHandler>(), faultWindowSignalingOnConditionHandler);
Expand Down Expand Up @@ -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<HttpMessageHandler>(), faultWindowSignalingOnConditionHandler);
Expand All @@ -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<HttpMessageHandler>(), faultWindowSignalingOnConditionHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -21,7 +24,8 @@ public async Task Procrastinates()
{
ProbabilityPercentage = 100d,
DelayInMilliseconds = delayInMilliseconds,
});
},
rng);

using var invoker = HttpMessageInvokerFactory.Create(
fixture.Create<HttpMessageHandler>(), procrastinatingWithProbabilityHandler);
Expand All @@ -46,7 +50,8 @@ public async Task Does_Not_Procrastinate()
{
ProbabilityPercentage = 0d,
DelayInMilliseconds = delayInMilliseconds,
});
},
rng);

using var invoker = HttpMessageInvokerFactory.Create(
fixture.Create<HttpMessageHandler>(), procrastinatingWithProbabilityHandler);
Expand Down
9 changes: 7 additions & 2 deletions tests/rm.DelegatingHandlersTest/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -22,7 +25,8 @@ public async Task ShortCircuits()
ProbabilityPercentage = 100d,
StatusCode = statusCode,
Content = content,
});
},
rng);

using var invoker = HttpMessageInvokerFactory.Create(
shortCircuitingResponseWithProbabilityHandler);
Expand All @@ -48,7 +52,8 @@ public async Task Does_Not_ShortCircuit()
ProbabilityPercentage = 0d,
StatusCode = statusCode,
Content = content,
});
},
rng);

using var invoker = HttpMessageInvokerFactory.Create(
fixture.Create<HttpMessageHandler>(), shortCircuitingResponseWithProbabilityHandler);
Expand Down
Loading

0 comments on commit 0c2fa56

Please sign in to comment.