Skip to content

Instantly share code, notes, and snippets.

@gamlerhart
Created November 11, 2010 19:50
Show Gist options
  • Save gamlerhart/673065 to your computer and use it in GitHub Desktop.
Save gamlerhart/673065 to your computer and use it in GitHub Desktop.
ComplexBusinessOperations-1.cs
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();
}
}
}
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;
}
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;
});
}
// 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();
}
[Test]
public void SendGoodsToCostumers()
{
var toTest = new ComplexBusinessOperations();
var wasSent = false;
toTest.GoodsArrived += () => wasSent = true;
toTest.SendGoodsToCostumers("A Nice Toy");
Assert.IsTrue(wasSent);
}
[Test]
public void SendGoodsToCostumers()
{
var toTest = new ComplexBusinessOperations();
var wasSent = false;
toTest.GoodsArrived += () => wasSent = true;
toTest.SendGoodsToCostumersAsync("A Nice Toy").Wait();
Assert.IsTrue(wasSent);
}
[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