public void ExecuteAction_When_Called_Given_That_Action_Throws_Does_Not_Swallow_Exception() { // Arrange int actionCallTimes = 0; var exception = new InvalidOperationException(); AsyncActionWithStopwatch action = async(sw) => { sw.Start(); actionCallTimes++; await Task.FromException <Exception>(exception); }; AsyncManualStopwatchStrategy measurementStrategy = new AsyncManualStopwatchStrategy(action); Stopwatch stopwatch = new Stopwatch(); // Assert var aex = Assert.Catch <AggregateException>(() => { // Act measurementStrategy.ExecuteAction(stopwatch); }); Assert.AreEqual(exception, aex.InnerException); Assert.AreEqual(1, actionCallTimes); Assert.AreNotEqual(0, stopwatch.ElapsedTicks); Assert.IsFalse(stopwatch.IsRunning); }
public void OfAsyncAction_Taking_AsyncActionWithSW_When_Called_Does_Not_Calls_The_Action() { // Arrange Measurement measurement = Measurement.Create(); bool wasCalled = false; AsyncActionWithStopwatch action = async(sw) => { wasCalled = true; }; // Act measurement.OfAsyncAction(action); // Assert Assert.IsFalse(wasCalled); }
public void Run_When_Called_Calls_Main_Action_Specified_Times4() { // Arrange int timesOfRun = 15; int timesMainActionCalled = 0; AsyncActionWithStopwatch mainAction = async(sw) => { timesMainActionCalled++; }; Measurement measurement = Measurement.Create() .OfAsyncAction(mainAction); // Act measurement.Run(timesOfRun); // Assert Assert.AreEqual(timesOfRun, timesMainActionCalled); }
public void OfAsyncAction_Taking_AsyncActionWithSW_When_Called_Sets_Appropriate_Stopwatch_Strategy() { // Arrange Measurement measurement = Measurement.Create(); AsyncActionWithStopwatch action = async(sw) => { }; // Act measurement.OfAsyncAction(action); // Assert Assert.IsInstanceOf <AsyncManualStopwatchStrategy>(measurement.MeasurementStrategy); var strategy = (AsyncManualStopwatchStrategy)measurement.MeasurementStrategy; Assert.AreEqual(action, strategy.Action); }
public void ExecuteAction_When_Called_Calls_Action() { // Arrange int actionCallTimes = 0; AsyncActionWithStopwatch action = async(sw) => { sw.Start(); actionCallTimes++; await Task.CompletedTask; }; AsyncManualStopwatchStrategy measurementStrategy = new AsyncManualStopwatchStrategy(action); Stopwatch stopwatch = new Stopwatch(); // Act measurementStrategy.ExecuteAction(stopwatch); // Assert Assert.AreEqual(1, actionCallTimes); Assert.AreNotEqual(0, stopwatch.ElapsedTicks); Assert.IsFalse(stopwatch.IsRunning); }
public void Run_When_Called_Accumulates_Stop_Watch_Values_To_Create_A_Result_Value2() { // Arrange int timesOfRun = 10; long stopWatchAccumulatedTicks = 0; AsyncActionWithStopwatch mainAction = async(sw) => { sw.Start(); await Task.Delay(new Random().Next(50, 100)); sw.Stop(); stopWatchAccumulatedTicks += sw.ElapsedTicks; }; Measurement measurement = Measurement.Create() .OfAsyncAction(mainAction); // Act var result = measurement.Run(timesOfRun); // Assert Assert.AreEqual(stopWatchAccumulatedTicks, result.TotalElapsedTicks); }
public AsyncManualStopwatchStrategy(AsyncActionWithStopwatch action) { _action = action; }
/// <summary> /// Adds an async action taking an instance of <c>System.Diagnostics.Stopwatch</c>. /// </summary> /// <param name="action"></param> /// <returns>Self.</returns> public Measurement OfAsyncAction(AsyncActionWithStopwatch action) { ValidateActionAndMeasurement(action); _measurementStrategy = new AsyncManualStopwatchStrategy(action); return(this); }