Created
November 11, 2010 19:50
-
-
Save gamlerhart/673065 to your computer and use it in GitHub Desktop.
ComplexBusinessOperations-1.cs
This file contains 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 Awaiter | |
{ | |
private readonly MyPrimitiveSynchronisationContext syncContext; | |
internal Awaiter(MyPrimitiveSynchronisationContext syncContext) | |
{ | |
this.syncContext = syncContext; | |
} | |
public void WaitFor(IAsyncResult toWaitOn) | |
{ | |
WaitFor(() => toWaitOn.IsCompleted); | |
} | |
// Our new wait method which can wait for anything | |
// Continues when the condition is true | |
public void WaitFor(Func<bool> toWaitOn) | |
{ | |
while (!toWaitOn()) | |
{ | |
syncContext.RunOneRound(); | |
} | |
} | |
} |
This file contains 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 event Action GoodsArrived; | |
public void SendGoodsToCostumers(string theGoods) | |
{ | |
var package = PreparePackage(theGoods); | |
SendPackage(package); | |
GoodsArrived(); | |
} | |
private string SendPackage(string package) | |
{ | |
// This operation takes a while | |
Thread.Sleep(50); | |
return "Send: " + package; | |
} | |
private string PreparePackage(string theGoods) | |
{ | |
// This operation takes a while | |
Thread.Sleep(50); | |
return "Wrapped: " + theGoods; | |
} |
This file contains 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 event Action GoodsArrived; | |
public async void SendGoodsToCostumersAsync(string theGoods) | |
{ | |
var package = await PreparePackageAsync(theGoods); | |
var sentPackage = await SendPackageAsync(package); | |
GoodsArrived(); | |
} | |
private Task<string> SendPackageAsync(string package) | |
{ | |
// 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(50); | |
return "Send: " + package; | |
}); | |
} | |
private Task<string> PreparePackageAsync(string theGoods) | |
{ | |
// 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(50); | |
return "Wrapped:: " + theGoods; | |
}); | |
} |
This file contains 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
// Return a Taks instead of void to make testing easier | |
public async Task SendGoodsToCostumersAsync(string theGoods) | |
{ | |
var package = await PreparePackageAsync(theGoods); | |
var sentPackage = await SendPackageAsync(package); | |
GoodsArrived(); | |
} |
This file contains 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 SendGoodsToCostumers() | |
{ | |
var toTest = new ComplexBusinessOperations(); | |
var wasSent = false; | |
toTest.GoodsArrived += () => wasSent = true; | |
toTest.SendGoodsToCostumers("A Nice Toy"); | |
Assert.IsTrue(wasSent); | |
} |
This file contains 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 SendGoodsToCostumers() | |
{ | |
var toTest = new ComplexBusinessOperations(); | |
var wasSent = false; | |
toTest.GoodsArrived += () => wasSent = true; | |
toTest.SendGoodsToCostumersAsync("A Nice Toy").Wait(); | |
Assert.IsTrue(wasSent); | |
} |
This file contains 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 SendGoodsToCostumers() | |
{ | |
TestSyncContext.Run((awaiter)=> | |
{ | |
var toTest = new ComplexBusinessOperations(); | |
var wasSent = false; | |
toTest.GoodsArrived += () => wasSent = true; | |
toTest.SendGoodsToCostumersAsync("A Nice Toy"); | |
awaiter.WaitFor(()=>wasSent); | |
Assert.IsTrue(wasSent); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment