Bytes2you.Validation.UnitTests.Testing.Helpers.Ensure.ActionRunsInExpectedTime C# (CSharp) Method

ActionRunsInExpectedTime() public static method

public static ActionRunsInExpectedTime ( System.Action action, int repeatCount, int expectedTimeInMilliseconds ) : void
action System.Action
repeatCount int
expectedTimeInMilliseconds int
return void
        public static void ActionRunsInExpectedTime(Action action, int repeatCount, int expectedTimeInMilliseconds)
        {
            if (action == null)
            {
                throw new ArgumentNullException("method");
            }

            if (repeatCount <= 0)
            {
                throw new ArgumentException("The argument is less than or equal to 0.", "repeatCount");
            }

            if (expectedTimeInMilliseconds <= 0)
            {
                throw new ArgumentException("The argument is less than or equal to 0.", "expectedTimeInMilliseconds");
            }

            Stopwatch watch = new Stopwatch();

            for (int i = 0; i < repeatCount; i++)
            {
                watch.Start();

                action();

                watch.Stop();
            }

            string assertionMessage = string.Format(
                "The given action does not perform as expected. repeatCount: {0}; expectedTimeInMilliseconds: {1}; actualTimeInMilliseconds: {2}", 
                repeatCount, 
                expectedTimeInMilliseconds, 
                watch.ElapsedMilliseconds);

            Assert.IsTrue(watch.ElapsedMilliseconds < expectedTimeInMilliseconds, assertionMessage);
        }
    }