Skip to content

Instantly share code, notes, and snippets.

@gamlerhart
Created November 3, 2010 19:52
Show Gist options
  • Save gamlerhart/661599 to your computer and use it in GitHub Desktop.
Save gamlerhart/661599 to your computer and use it in GitHub Desktop.
async&unit-test: Simple Result
public string AwesomeBusinessOperation()
{
var firstPart = TakesALongTimeToProcess("Tons");
var secondPart = TakesALongTimeToProcess(firstPart+" of ");
var result = TakesALongTimeToProcess(secondPart+"money");
return result;
}
private string TakesALongTimeToProcess(string word)
{
// This operation takes a while
Thread.Sleep(1000);
return word;
}
public async Task<string> AwesomeBusinessOperationAsync()
{
var firstPart = await TakesALongTimeToProcessAsync("Tons");
var secondPart = await TakesALongTimeToProcessAsync(firstPart + " to ");
var result = await TakesALongTimeToProcessAsync(secondPart + "money");
return result;
}
private Task<string> TakesALongTimeToProcessAsync(string word)
{
// Remember, this is just a simulation
// Usually you would use some other async API here
return TaskEx.Run(() =>
{
// This operation takes a while
Thread.Sleep(1000);
return word;
});
}
[Test]
public void ExpectTonsOfMoney(){
var toTest = new MyBusinessLogic();
var result = toTest.AwesomeBusinessOperation();
Assert.AreEqual("Tons of money",result);
}
[Test]
public void ExpectTonsOfMoney(){
var toTest = new MyBusinessLogic();
var result = toTest.AwesomeBusinessOperationAsync();
Assert.AreEqual("Tons of money",result.Result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment