Created
November 8, 2010 20:49
-
-
Save gamlerhart/668230 to your computer and use it in GitHub Desktop.
Unit Testing, Part II, Synchronisation Issues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ComplexBusinessOperations | |
{ | |
private int moneyEarnedSoFar; | |
public int MoneyEarnedSoFar | |
{ | |
get { return moneyEarnedSoFar; } | |
} | |
public void EarnMoney(int investment) | |
{ | |
EarnMoneySubTaks(investment); | |
EarnMoneySubTaks(investment); | |
} | |
private void EarnMoneySubTaks(int investment) | |
{ | |
for (int i = 0; i < investment; i++) | |
{ | |
var result = EarnOneDollar(); | |
moneyEarnedSoFar += result; | |
} | |
} | |
private int EarnOneDollar() | |
{ | |
// This operation takes a while | |
Thread.Sleep(50); | |
return 1; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ComplexBusinessOperations | |
{ | |
private int moneyEarnedSoFar; | |
public int MoneyEarnedSoFar | |
{ | |
get { return moneyEarnedSoFar; } | |
} | |
public async Task EarnMoneyAsync(int investment) | |
{ | |
var firstMoneyEarner = EarnMoneySubTaskAsync(investment); | |
var secondMoneyEarner = EarnMoneySubTaskAsync(investment); | |
await TaskEx.WhenAll(firstMoneyEarner, secondMoneyEarner); | |
} | |
private async Task EarnMoneySubTaskAsync(int investment) | |
{ | |
for (int i = 0; i < investment; i++) | |
{ | |
var result = await EarnOneDollarAsync(); | |
moneyEarnedSoFar += result; | |
} | |
} | |
private Task<int> EarnOneDollarAsync() | |
{ | |
return TaskEx.Run( | |
() => | |
{ | |
// This operation takes a while | |
Thread.Sleep(50); | |
return 1; | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void EarnMoney() | |
{ | |
var toTest = new ComplexBusinessOperations(); | |
toTest.EarnMoney(200); | |
Assert.AreEqual(400, toTest.MoneyEarnedSoFar); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void EarnMoney() | |
{ | |
var toTest = new ComplexBusinessOperations(); | |
var task = toTest.EarnMoneyAsync(200); | |
task.Wait(); | |
Assert.AreEqual(400, toTest.MoneyEarnedSoFar); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment