Retarder–Fixed and Random strategy and Tests-part 4
Now I want to make a second strategy : the random strategy between a min and a max value. This is only to minimize the impact on the extension that uses the Random – it is good to be hidden inside. For Random Strategy it is better to re-use the Fixed Strategy – and this is the code:
using System; using System.Threading.Tasks; namespace NetCoreRetarderCore { public class StrategyAwaitRandom : IStrategyAwait { StrategyAwaitFixed sf; public StrategyAwaitRandom(int min,int max) { if (min > max) { var x = min; min = max; max = min; } var random = new Random(); var awaitTime = random.Next(min, max); sf = new StrategyAwaitFixed(awaitTime); } public Task<IStrategyAwait> AwaitDelayAfter() { return sf.AwaitDelayAfter(); } public Task<IStrategyAwait> AwaitDelayBefore() { return sf.AwaitDelayBefore(); } } }
Also, I want to make some tests to verify the awaits. So I choose https://github.com/LightBDD/LightBDD and my tests looks like this:
using LightBDD.Framework; using LightBDD.Framework.Scenarios; using LightBDD.XUnit2; using System; using System.Threading.Tasks; using Xunit; [assembly: LightBddScope] namespace NetCoreRetarderTest { //https://github.com/LightBDD/LightBDD/wiki/Formatting-Parameterized-Steps [FeatureDescription( @"This will test just how will await a determined number of time ")] public partial class TestStrategyAwait { [Scenario] [Label("wait times fixed")] [Trait("Category", "Wait")] [InlineData(3)] [InlineData(1)] [InlineData(0)] [InlineData(-1)] public async Task WaitTime(int nrSeconds) { await Runner.RunScenarioAsync( _ => When_Choosing_Fixed_Await_For_NRSECONDS_Seconds(nrSeconds), _=> And_Await(), _=> Then_Time_Ellapsed_Will_Be_NRSECONDS_Seconds(Math.Max(nrSeconds, 0)) ) ; } [Scenario] [Label("wait times random")] [ScenarioCategory("Wait")] [InlineData(3,1)] [InlineData(1,2)] [InlineData(0,1)] [InlineData(-1,5)] public async Task WaitTimeRandom(int min,int max) { await Runner.RunScenarioAsync( _ => When_Choosing_Random_Await_Between_MIN_And_MAX_Seconds(min,max), _ => And_Await(), _ => Then_Time_Ellapsed_Will_Be_Between_MIN_And_MAX_Seconds(Math.Max(min, 0),Math.Max(max,0)) ) ; } } }
You can find the code at https://github.com/ignatandrei/NetCoreRetarder/commits/version4_tests