Skip to content

Commit

Permalink
Merge pull request #17 from jlandheer/master
Browse files Browse the repository at this point in the history
Duration of exceptions is measured too
  • Loading branch information
haacked committed Feb 9, 2016
2 parents 46d4ef4 + 394171f commit 189ab22
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
25 changes: 25 additions & 0 deletions UnitTests/ScientistTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,30 @@ public void RunsBothBranchesOfTheExperimentAndReportsSuccessWithDurations()
Assert.True(((InMemoryObservationPublisher)Scientist.ObservationPublisher).Observations.First(m => m.Name == "success").ControlDuration.Ticks > 0);
Assert.True(((InMemoryObservationPublisher)Scientist.ObservationPublisher).Observations.First(m => m.Name == "success").CandidateDuration.Ticks > 0);
}

[Fact]
public void AnExceptionReportsDuration()
{
var candidateRan = false;
var controlRan = false;

// We introduce side effects for testing. Don't do this in real life please.
// Do we do a deep comparison?
Func<int> control = () => { controlRan = true; return 42; };
Func<int> candidate = () => { candidateRan = true; throw new InvalidOperationException(); };

var result = Scientist.Science<int>("failure", experiment =>
{
experiment.Use(control);
experiment.Try(candidate);
});

Assert.Equal(42, result);
Assert.True(candidateRan);
Assert.True(controlRan);
Assert.True(((InMemoryObservationPublisher)Scientist.ObservationPublisher).Observations.First(m => m.Name == "failure").Success == false);
Assert.True(((InMemoryObservationPublisher)Scientist.ObservationPublisher).Observations.First(m => m.Name == "failure").ControlDuration.Ticks > 0);
Assert.True(((InMemoryObservationPublisher)Scientist.ObservationPublisher).Observations.First(m => m.Name == "failure").CandidateDuration.Ticks > 0);
}
}
}
9 changes: 5 additions & 4 deletions src/Scientist/Internals/Experiment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task<T> Run()

// TODO: We need to compare that thrown exceptions are equivalent too https://github.com/github/scientist/blob/master/lib/scientist/observation.rb#L76
// TODO: We're going to have to be a bit more sophisticated about this.
bool success =
bool success =
controlResult.Result == null && candidateResult.Result == null
|| controlResult.Result != null && controlResult.Result.Equals(candidateResult.Result)
|| controlResult.Result == null && candidateResult.Result != null;
Expand All @@ -68,19 +68,20 @@ public async Task<T> Run()

static async Task<ExperimentResult> Run(Func<Task<T>> experimentCase)
{
var sw = new Stopwatch();
sw.Start();
try
{
// TODO: Refactor this into helper function?
Stopwatch sw = new Stopwatch();
sw.Start();
var result = await experimentCase();
sw.Stop();

return new ExperimentResult(result, new TimeSpan(sw.ElapsedTicks));
}
catch (Exception e)
{
return new ExperimentResult(e, TimeSpan.Zero);
sw.Stop();
return new ExperimentResult(e, new TimeSpan(sw.ElapsedTicks));
}
}

Expand Down

0 comments on commit 189ab22

Please sign in to comment.