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:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | using LightBDD.Framework; using LightBDD.Framework.Scenarios; using LightBDD.XUnit2; using System; using System.Threading.Tasks; using Xunit; [assembly: LightBddScope] namespace NetCoreRetarderTest { [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